SwiftGenStrings
SwiftGenStrings 是一个命令行应用程序,可以用作 Swift 源代码中标准 genstrings
命令的替代。后者只支持 NSLocalizedString
函数的短形式,但只要使用除 key
和 comment
以外的任何参数就会崩溃,如下所示:
NSLocalizedString("DATE_RANGE", value: "%@ – %@", comment: "A range of dates")
上游问题在此跟踪:这里。
用法
SwiftGenStrings files
SwiftGenStrings [-s <routine>] [-o <outputDir>] files
SwiftGenStrings [-h|--help]
OPTIONS
-h|--help
(Optional) Print help.
-s routine
(Optional) Substitute routine for NSLocalizedString, useful when different macro is used.
-o outputDir
(Optional) Specifies what directory Localizable.strings table is created in.
Not specifying output directory will print script output content to standard output (console).
files
List of files, that are used as source of Localizable.strings generation.
为了收集当前目录中的字符串,运行以下命令:
$ find . -name "*.swift" | xargs SwiftGenStrings
导出二进制文件
项目提供了一个 Makefile
,要导出二进制文件,请运行以下命令:
$ make release
导出的二进制文件可以在 Products/SwiftGenStrings
中找到
测试
Xcode 9.2 似乎在对 macOS 目标的测试运行中存在一个错误,幸运的是,xcodebuild
命令运行正常。
$ make test
需求
- Xcode 9.2
- Swift 4.0.2
局限性
- SwiftGenStrings目前不支持多个表,只支持默认的
。Localizable.strings
- 在字符串插值中无法使用
NSLocalizedString
,例如:let hello = "--- \(NSLocalizedString("Hello world!", comment: ""))"
将不会提取本地化字符串。