Reputation: 2406
genstrings
works well to extract localizable content from .m file as,
find . -name \*.m | xargs genstrings -o en.lproj
But, not working for .swift file as,
find . -name \*.swift | xargs genstrings -o en.lproj
Upvotes: 25
Views: 11815
Reputation: 4918
Xcode now includes a powerful tool for extracting localizations.
Just select your project on the left then Editor menu >> Export localizations.
You'll get a folder with all the text in your files as well as the Localizable.strings
and InfoPlist.strings
More details here: https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPInternational/LocalizingYourApp/LocalizingYourApp.html
Upvotes: 0
Reputation: 5972
The issue I was having with find/genstrings
was twofold:
find
), it would exit with an errorTo fix both those problems I'm using the following:
find Some/Path/ \( -name "*.swift" ! -name "MyExcludedFile.swift" \) | sed "s/^/'/;s/$/'/" | xargs genstrings -o . -s MyCustomLocalizedStringRoutine
To summarize, we use the find
command to both find and exclude your Swift files, then pipe the results into the sed
command which will wrap each file path in quotes, then finally pipe that result into the genstrings
command
Upvotes: 0
Reputation: 9
There is a similar question here: How to use genstrings across multiple directories?
find ./ -name "*.m" -print0 | xargs -0 genstrings -o en.lproj
Upvotes: 0
Reputation: 1907
I believe genstrings
works as intended, however Apple's xargs
approach to generate strings from all your project's files is flawed and does not properly parse paths containing spaces.
That might be the reason why it's not working for you.
Try using the following:
find . -name \*.swift | tr '\n' '\0' | xargs -0 genstrings -o .
Upvotes: 5
Reputation: 18523
There's an alternative tool called SwiftGenStrings
Hello.swift
NSLocalizedString("hello", value: "world", comment: "Hi!")
SwiftGenStrings:
$ SwiftGenStrings Hello.swift
/* Hi! */
"hello" = "world";
Apple genstrings:
$ genstrings Hello.swift
Bad entry in file Hello.swift (line = 1): Argument is not a literal string.
Disclaimer: I worked on SwiftGenStrings.
Upvotes: 2
Reputation: 80265
The genstrings
tool works fine with swift as far as I am concerned. Here is my test:
// MyClass.swift
let message = NSLocalizedString("This is the test message.", comment: "Test")
then, in the folder with the class
# generate strings for all swift files (even in nested directories) $ find . -name \*.swift | xargs genstrings -o . # See results $ cat Localizable.strings /* Test */ "This is the test message." = "This is the test message."; $
Upvotes: 36
Reputation: 4183
We wrote a command line tool that works for Swift files and merges the result of apples genstrings
tool.
It allows for key
and value
in NSLocalizedString
https://github.com/KeepSafe/genstrings_swift
Upvotes: 4