Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
Add support for main bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
hectr committed Oct 14, 2019
1 parent 83e4271 commit a233ec3
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 25 deletions.
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -116,7 +130,7 @@ public enum RichText: String, Unstringified {
public enum RichFormat: Unstringified {
public typealias StringType = NSAttributedString
case 👻()
case 👻(Void)
}
```

Expand Down
14 changes: 3 additions & 11 deletions Sources/Unstringified/Internal/Unstringified+localizedString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: "")
}
}
3 changes: 2 additions & 1 deletion Sources/Unstringified/Unstringified.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions Sources/unstringify/Internal/parseKeys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 16 additions & 2 deletions Sources/unstringify/Internal/template.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down
6 changes: 5 additions & 1 deletion Tests/UnstringifiedTests/UnstringifiedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion Tests/UnstringifyFrameworkTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]
}

Expand Down
2 changes: 1 addition & 1 deletion Unstringified.podspec
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion Unstringify.podspec
Original file line number Diff line number Diff line change
@@ -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'
Expand Down

0 comments on commit a233ec3

Please sign in to comment.