SwiftGenStrings is a command line application that can be used as a drop-in replacement for the standard
genstrings command for Swift sources. The latter only supports the short form of the
NSLocalizedString function but breaks as soon as you use any parameters other than
comment as in
NSLocalizedString("DATE_RANGE", value: "%@ – %@", comment: "A range of dates")
The upstream issue is tracked here.
USAGE: SwiftGenStrings <files> ... [-s <s>] [-o <o>] [--exclude-comments] ARGUMENTS: <files> List of files, that are used as source of Localizable.strings generation. OPTIONS: -s <s> (Optional) Substitute for NSLocalizedString, useful when different macro is used. -o <o> (Optional) Specifies what directory Localizable.strings table is created in. Not specifying output directory will print script output content to standard output (console). -e, --exclude-comments (Optional) Formatted output does not include comments --version Show the version. -h, --help Show help information.
To gather strings in current directory, run:
$ find . -name "*.swift" | xargs SwiftGenStrings
If you have any 3rd party code like CocoaPods or Carthage in your project directory, you might want to exclude it from localization. To do that, run the following:
$ find . \( -name "*.swift" ! -path "./Carthage/*" ! -path "./Pods/*" \) | xargs SwiftGenStrings
The quickest and easiest way to install SwiftGenStrings is via Mint
$ mint install kayak/SwiftGenStrings
The project provides a
Makefile. To export a binary run:
$ make release
The exported binary can be found under
Products/SwiftGenStrings. Alternatively you can use
make install to install the compiled library directly into
Since SwiftGenStrings is a SPM package, running tests is easy:
$ swift test
NSLocalizedStringin string interpolation e.g.:
let hello = "--- \(NSLocalizedString("Hello world!", comment: ""))"will not pickup the localized string.