|
28 | 28 | import Foundation
|
29 | 29 |
|
30 | 30 | public struct SwiftGenerator {
|
31 |
| - public static func generate(structs: [Struct], keyValues: [String:String]) -> String { |
| 31 | + public static func generate(structs: [Struct], keyValues: [String:String], packageName: String?) -> String { |
32 | 32 | var swift = "// This file is autogenerated by Lingo from your localized strings file.\n\n"
|
33 | 33 | swift += "import Foundation\n\n"
|
34 | 34 | swift += "private class BundleLocator {\n"
|
35 | 35 | swift += " static let bundle: Bundle = {\n"
|
36 |
| - swift += " #if SWIFT_PACKAGE\n" |
37 |
| - swift += " return Bundle.module\n" |
38 |
| - swift += " #else\n" |
39 |
| - swift += " return Bundle(for: BundleLocator.self)\n" |
40 |
| - swift += " #endif\n" |
| 36 | + swift += " #if SWIFT_PACKAGE\n" |
| 37 | + swift += " \(modulde(packageName))\n" |
| 38 | + swift += " #else\n" |
| 39 | + swift += " return Bundle(for: BundleLocator.self)\n" |
| 40 | + swift += " #endif\n" |
41 | 41 | swift += " }()\n"
|
42 | 42 | swift += "}\n\n"
|
43 | 43 | swift += "public struct Lingo {\n"
|
@@ -65,4 +65,35 @@ public struct SwiftGenerator {
|
65 | 65 | swift += "\n }"
|
66 | 66 | return swift
|
67 | 67 | }
|
| 68 | + |
| 69 | + private static func modulde(_ packageName: String?) -> String { |
| 70 | + if let packageName = packageName { |
| 71 | + return """ |
| 72 | + let bundleName = "\(packageName)_\(packageName)" |
| 73 | + let candidates = [ |
| 74 | + /* Bundle should be present here when the package is linked into an App. */ |
| 75 | + Bundle.main.resourceURL, |
| 76 | + /* Bundle should be present here when the package is linked into a framework. */ |
| 77 | + Bundle(for: BundleLocator.self).resourceURL, |
| 78 | + /* For command-line tools. */ |
| 79 | + Bundle.main.bundleURL, |
| 80 | + /* Bundle should be present here when the package is used in UI Tests. */ |
| 81 | + Bundle(for: BundleLocator.self).resourceURL?.deletingLastPathComponent(), |
| 82 | + /* Bundle should be present here when running previews from a different package (this is the path to "…/Debug-iphonesimulator/"). */ |
| 83 | + Bundle(for: BundleLocator.self).resourceURL?.deletingLastPathComponent().deletingLastPathComponent() |
| 84 | + ] |
| 85 | +
|
| 86 | + for candidate in candidates { |
| 87 | + let bundlePath = candidate?.appendingPathComponent(bundleName + ".bundle") |
| 88 | + if let bundle = bundlePath.flatMap(Bundle.init(url:)) { |
| 89 | + return bundle |
| 90 | + } |
| 91 | + } |
| 92 | +
|
| 93 | + fatalError("unable to find bundle named \\(bundleName)") |
| 94 | + """ |
| 95 | + } else { |
| 96 | + return "return Bundle.module" |
| 97 | + } |
| 98 | + } |
68 | 99 | }
|
0 commit comments