Skip to content

Commit 526f56b

Browse files
authored
Merge pull request SwiftGen#716 from SwiftGen/feature/better-localizefunction
Strings: rename `localizeFunction` to `lookupFunction` (and impl. changes)
2 parents 2d87582 + 0e02c06 commit 526f56b

File tree

50 files changed

+84
-94
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+84
-94
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* XCAssets: the `colorAliasName` and `imageAliasName` template parameters are now deprecated and will be removed in the next major release.
1414
[David Jennes](https://github.com/djbe)
1515
[#614](https://github.com/SwiftGen/SwiftGen/pull/614)
16-
16+
1717
### Deprecations
1818

1919
* The use of `swiftgen <parser>` (e.g. `swiftgen strings`, `swiftgen xcassets`, …) command line for running individual parsers is now deprecated in favor of `swiftgen run <parser>`. See "New Features" below.
@@ -53,11 +53,12 @@
5353
[#609](https://github.com/SwiftGen/SwiftGen/issues/609)
5454
[#593](https://github.com/SwiftGen/SwiftGen/pull/593)
5555
[#610](https://github.com/SwiftGen/SwiftGen/pull/610)
56-
* Strings: the built-in templates now accept a parameter for customizing the localization function.
56+
* Strings: the built-in templates now accept a parameter `lookupFunction` for customizing the localization function, check the [template documentation](Documentation/templates/string/) for more information.
5757
[Steven Magdy](https://github.com/StevenMagdy)
5858
[426](https://github.com/SwiftGen/SwiftGen/issues/426)
5959
[468](https://github.com/SwiftGen/SwiftGen/issues/468)
6060
[573](https://github.com/SwiftGen/SwiftGen/pull/573)
61+
[716](https://github.com/SwiftGen/SwiftGen/pull/716)
6162
* Strings: templates to generate Objective-C. Please check [the template's documentation](Documentation/templates/string/objc.md) for more information.
6263
[Eric Slosser](https://github.com/Mr-Fixit)
6364
[SwiftGen/SwiftGen#378](https://github.com/SwiftGen/SwiftGen/pull/378)

Documentation/templates/strings/flat-swift4.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ You can customize some elements of this template by overriding the following par
2121
| `enumName` | `L10n` | Allows you to change the name of the generated `enum` containing all string tables. |
2222
| `noComments` | N/A | Setting this parameter will disable the comments describing the translation of a key. |
2323
| `publicAccess` | N/A | If set, the generated constants will be marked as `public`. Otherwise, they'll be declared `internal`. |
24-
| `localizeFunction` | `NSLocalizedString` | Allows you to set your own custom localization function. Your custom function must have the same signature as the one provided by `Foundation`, i.e. `yourFunctionName(_:tableName:bundle:comment:)` |
24+
| `lookupFunction` | N/A | Allows you to set your own custom localization function. The function needs to accepts 2 parameters: the localization key (`String`), and the localization table (`String`). If the function has named parameters, you must provide the complete function signature, including those named parameters – e.g. `yourFunctionName(forKey:table:)`. |
2525
| `forceFileNameEnum` | N/A | Setting this parameter will generate an `enum <FileName>` _even if_ only one FileName was provided as input. |
2626

2727
## Generated Code

Documentation/templates/strings/flat-swift5.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ You can customize some elements of this template by overriding the following par
2121
| `enumName` | `L10n` | Allows you to change the name of the generated `enum` containing all string tables. |
2222
| `noComments` | N/A | Setting this parameter will disable the comments describing the translation of a key. |
2323
| `publicAccess` | N/A | If set, the generated constants will be marked as `public`. Otherwise, they'll be declared `internal`. |
24-
| `localizeFunction` | `NSLocalizedString` | Allows you to set your own custom localization function. Your custom function must have the same signature as the one provided by `Foundation`, i.e. `yourFunctionName(_:tableName:bundle:comment:)` |
24+
| `lookupFunction` | N/A | Allows you to set your own custom localization function. The function needs to accepts 2 parameters: the localization key (`String`), and the localization table (`String`). If the function has named parameters, you must provide the complete function signature, including those named parameters – e.g. `yourFunctionName(forKey:table:)`. |
2525
| `forceFileNameEnum` | N/A | Setting this parameter will generate an `enum <FileName>` _even if_ only one FileName was provided as input. |
2626

2727
## Generated Code

Documentation/templates/strings/structured-swift4.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ You can customize some elements of this template by overriding the following par
2727
| `enumName` | `L10n` | Allows you to change the name of the generated `enum` containing all string tables. |
2828
| `noComments` | N/A | Setting this parameter will disable the comments describing the translation of a key. |
2929
| `publicAccess` | N/A | If set, the generated constants will be marked as `public`. Otherwise, they'll be declared `internal`. |
30-
| `localizeFunction` | `NSLocalizedString` | Allows you to set your own custom localization function. Your custom function must have the same signature as the one provided by `Foundation`, i.e. `yourFunctionName(_:tableName:bundle:comment:)` |
30+
| `lookupFunction` | N/A | Allows you to set your own custom localization function. The function needs to accepts 2 parameters: the localization key (`String`), and the localization table (`String`). If the function has named parameters, you must provide the complete function signature, including those named parameters – e.g. `yourFunctionName(forKey:table:)`. |
3131
| `forceFileNameEnum` | N/A | Setting this parameter will generate an `enum <FileName>` _even if_ only one FileName was provided as input. |
3232

3333
## Generated Code

Documentation/templates/strings/structured-swift5.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ You can customize some elements of this template by overriding the following par
2727
| `enumName` | `L10n` | Allows you to change the name of the generated `enum` containing all string tables. |
2828
| `noComments` | N/A | Setting this parameter will disable the comments describing the translation of a key. |
2929
| `publicAccess` | N/A | If set, the generated constants will be marked as `public`. Otherwise, they'll be declared `internal`. |
30-
| `localizeFunction` | `NSLocalizedString` | Allows you to set your own custom localization function. Your custom function must have the same signature as the one provided by `Foundation`, i.e. `yourFunctionName(_:tableName:bundle:comment:)` |
30+
| `lookupFunction` | N/A | Allows you to set your own custom localization function. The function needs to accepts 2 parameters: the localization key (`String`), and the localization table (`String`). If the function has named parameters, you must provide the complete function signature, including those named parameters – e.g. `yourFunctionName(forKey:table:)`. |
3131
| `forceFileNameEnum` | N/A | Setting this parameter will generate an `enum <FileName>` _even if_ only one FileName was provided as input. |
3232

3333
## Generated Code

SwiftGen.playground/Pages/Strings-Demo.xcplaygroundpage/Contents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ internal enum L10n {
6565

6666
extension L10n {
6767
private static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String {
68-
let format = NSLocalizedString(key, tableName: table, bundle: BundleToken.bundle, comment: "")
68+
let format = BundleToken.bundle.localizedString(forKey: key, value: nil, table: table)
6969
return String(format: format, locale: Locale.current, arguments: args)
7070
}
7171
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Foundation
22

3-
func XCTLocFunc(_ key: String, tableName: String? = nil, bundle: Bundle = Bundle.main, value: String = "", comment: String) -> String {
4-
return NSLocalizedString(key, tableName: tableName, bundle: bundle, value: value, comment: comment)
3+
func XCTLocFunc(forKey key: String, table tableName: String?) -> String {
4+
return NSLocalizedString(key, tableName: tableName, comment: "")
55
}

Tests/Fixtures/Generated/Strings/flat-swift4/compilation-configuration.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ common:
44
- 4
55
- 4.2
66
file_specific:
7-
localizable-localizeFunction.swift:
7+
localizable-lookupFunction.swift:
88
files:
99
- Strings-LocalizeFunction.swift

Tests/Fixtures/Generated/Strings/flat-swift4/localizable-customName.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ internal enum XCTLoc {
5252

5353
extension XCTLoc {
5454
private static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String {
55-
// swiftlint:disable:next nslocalizedstring_key
56-
let format = NSLocalizedString(key, tableName: table, bundle: BundleToken.bundle, comment: "")
55+
let format = BundleToken.bundle.localizedString(forKey: key, value: nil, table: table)
5756
return String(format: format, locale: Locale.current, arguments: args)
5857
}
5958
}

Tests/Fixtures/Generated/Strings/flat-swift4/localizable-forceFileNameEnum.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ internal enum L10n {
5454

5555
extension L10n {
5656
private static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String {
57-
// swiftlint:disable:next nslocalizedstring_key
58-
let format = NSLocalizedString(key, tableName: table, bundle: BundleToken.bundle, comment: "")
57+
let format = BundleToken.bundle.localizedString(forKey: key, value: nil, table: table)
5958
return String(format: format, locale: Locale.current, arguments: args)
6059
}
6160
}

0 commit comments

Comments
 (0)