diff --git a/README.md b/README.md index f0c8a4c..9a3fec9 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ s.dependency 'Unstringified' Add a Build Phase that generates the code for the `Unstringified` enumerations: ```bash -"$PODS_ROOT/Unstringify/unstringify" "$SRCROOT/path/to/module/en.lproj/Localizable.strings" "$SRCROOT/path/to/module/Unstringified.generated.string" +"$PODS_ROOT/Unstringify/unstringify" "$SRCROOT/path/to/module/en.lproj/Localizable.strings" "$SRCROOT/path/to/module/Unstringified.generated.swift" ``` #### Swift Package Manager @@ -60,7 +60,7 @@ dependencies: [ Execute the CLI passing as arguments the *input strings file* and the *path to generated output file*: ```sh -swift run unstringify path/to/module/en.lproj/Localizable.string path/to/module/Unstringified.generated.string +swift run unstringify path/to/module/en.lproj/Localizable.string path/to/module/Unstringified.generated.swift ``` In order to use the generated file, you will need to *link* `Unstringified` to the module that contains the generated file and the original strings files. @@ -94,8 +94,22 @@ import Unstringified private final class _Unstringified {} extension Unstringified { - public var localizableStringsBundle: Bundle { - return Bundle(for: _Unstringified.self) + public var localizableStringsTableName: String? { + return nil + } + + public var localizableStringsBundle: Bundle? { + let _UnstringifiedBundle = Bundle(for: _Unstringified.self) + guard _UnstringifiedBundle.bundleIdentifier != Bundle.main.bundleIdentifier else { + return Bundle.main + } + let bundleURL = _UnstringifiedBundle.bundleURL + let bundleName = bundleURL.lastPathComponent + let resource = (bundleName as NSString).deletingPathExtension + guard let path = _UnstringifiedBundle.path(forResource: resource, ofType: "bundle") else { + return nil + } + return Bundle(path: path) } } @@ -116,7 +130,7 @@ public enum RichText: String, Unstringified { public enum RichFormat: Unstringified { public typealias StringType = NSAttributedString - case 👻() + case 👻(Void) } ``` diff --git a/Sources/Unstringified/Internal/Unstringified+localizedString.swift b/Sources/Unstringified/Internal/Unstringified+localizedString.swift index 6215d8d..6b63795 100755 --- a/Sources/Unstringified/Internal/Unstringified+localizedString.swift +++ b/Sources/Unstringified/Internal/Unstringified+localizedString.swift @@ -5,16 +5,8 @@ extension Unstringified { guard !isRunningTests else { return key } - let _UncrustifiedBundle = localizableStringsBundle - let bundleURL = _UncrustifiedBundle.bundleURL - let bundleName = bundleURL.lastPathComponent - let resource = (bundleName as NSString).deletingPathExtension - guard let path = _UncrustifiedBundle.path(forResource: resource, ofType: "bundle") else { - return key - } - guard let bundle = Bundle(path: path) else { - return key - } - return NSLocalizedString(key, bundle: bundle, comment: "") + let bundle = localizableStringsBundle ?? Bundle.main + let tableName = localizableStringsTableName + return NSLocalizedString(key, tableName: tableName, bundle: bundle, comment: "") } } diff --git a/Sources/Unstringified/Unstringified.swift b/Sources/Unstringified/Unstringified.swift index bf73476..1dfa849 100755 --- a/Sources/Unstringified/Unstringified.swift +++ b/Sources/Unstringified/Unstringified.swift @@ -5,7 +5,8 @@ public protocol Unstringified { var string: StringType { get } var uppercased: StringType { get } - var localizableStringsBundle: Bundle { get } + var localizableStringsTableName: String? { get } + var localizableStringsBundle: Bundle? { get } } extension Unstringified where Self: RawRepresentable, Self.RawValue == String, StringType == String { diff --git a/Sources/unstringify/Internal/parseKeys.swift b/Sources/unstringify/Internal/parseKeys.swift index 69328c8..e6cb045 100755 --- a/Sources/unstringify/Internal/parseKeys.swift +++ b/Sources/unstringify/Internal/parseKeys.swift @@ -33,13 +33,13 @@ func parseKeys(_ content: String) throws -> Keys { standardKeys.append("👻") } if formattedKeys.isEmpty { - formattedKeys.append(FormatKey(key: "👻", specifiers: [])) + formattedKeys.append(FormatKey(key: "👻", specifiers: ["Void"])) } if richKeys.isEmpty { richKeys.append("👻") } if richFormattedKeys.isEmpty { - richFormattedKeys.append(FormatKey(key: "👻", specifiers: [])) + richFormattedKeys.append(FormatKey(key: "👻", specifiers: ["Void"])) } return Keys(standard: standardKeys, diff --git a/Sources/unstringify/Internal/template.swift b/Sources/unstringify/Internal/template.swift index 50e0664..a305a82 100755 --- a/Sources/unstringify/Internal/template.swift +++ b/Sources/unstringify/Internal/template.swift @@ -11,8 +11,22 @@ func template(keys: [String], formattedKeys: [FormatKey], richKeys: [String], ri private final class _Unstringified {} extension Unstringified { - \tpublic var localizableStringsBundle: Bundle { - \t\treturn Bundle(for: _Unstringified.self) + \tpublic var localizableStringsTableName: String? { + \t\treturn nil + \t} + + \tpublic var localizableStringsBundle: Bundle? { + \t\tlet _UnstringifiedBundle = Bundle(for: _Unstringified.self) + \t\tguard _UnstringifiedBundle.bundleIdentifier != Bundle.main.bundleIdentifier else { + \t\t\treturn Bundle.main + \t\t} + \t\tlet bundleURL = _UnstringifiedBundle.bundleURL + \t\tlet bundleName = bundleURL.lastPathComponent + \t\tlet resource = (bundleName as NSString).deletingPathExtension + \t\tguard let path = _UnstringifiedBundle.path(forResource: resource, ofType: "bundle") else { + \t\t\treturn nil + \t\t} + \t\treturn Bundle(path: path) \t} } diff --git a/Tests/UnstringifiedTests/UnstringifiedTests.swift b/Tests/UnstringifiedTests/UnstringifiedTests.swift index 3210a28..535ad96 100644 --- a/Tests/UnstringifiedTests/UnstringifiedTests.swift +++ b/Tests/UnstringifiedTests/UnstringifiedTests.swift @@ -6,8 +6,12 @@ final class UnstringifiedTests: XCTestCase { private enum Foo: String, Unstringified { typealias StringType = String + case foo - var localizableStringsBundle: Bundle { return Bundle(for: _Foo.self) } + + var localizableStringsTableName: String? { return nil } + + var localizableStringsBundle: Bundle? { return Bundle(for: _Foo.self) } } func testString() throws { diff --git a/Tests/UnstringifyFrameworkTests/XCTestManifests.swift b/Tests/UnstringifyFrameworkTests/XCTestManifests.swift index f54bf70..4361cbc 100644 --- a/Tests/UnstringifyFrameworkTests/XCTestManifests.swift +++ b/Tests/UnstringifyFrameworkTests/XCTestManifests.swift @@ -6,7 +6,14 @@ extension UnstringifyFrameworkTests { // `swift test --generate-linuxmain` // to regenerate. static let __allTests__UnstringifyFrameworkTests = [ - ("test", test), + ("testContainsHTML", testContainsHTML), + ("testFormatSpecifiers", testFormatSpecifiers), + ("testIsINfoPlistValueLine", testIsINfoPlistValueLine), + ("testIsKeyValueLine", testIsKeyValueLine), + ("testLocalizableKey", testLocalizableKey), + ("testMatches", testMatches), + ("testParseKeys", testParseKeys), + ("testRemovingCharacterAt", testRemovingCharacterAt), ] } diff --git a/Unstringified.podspec b/Unstringified.podspec index e94a41e..6ccf1af 100644 --- a/Unstringified.podspec +++ b/Unstringified.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'Unstringified' - s.version = '0.2.0' + s.version = '0.3.0' s.summary = 'Strong-typed localizable strings static code.' s.homepage = 'https://github.com/metrolab/Unstringify' s.license = 'MIT' diff --git a/Unstringify.podspec b/Unstringify.podspec index 3527699..2e704d0 100644 --- a/Unstringify.podspec +++ b/Unstringify.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'Unstringify' - s.version = '0.2.0' + s.version = '0.3.0' s.summary = 'Code generator for strong-typing localizable strings.' s.homepage = 'https://github.com/metrolab/Unstringify' s.license = 'MIT'