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
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
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."; $
genstrings
does not work for any long-form (arguably best practice) usage: NSLocalizedString("checkout-okbtn", value: "Ok", comment: "in cart checkout, the button that pays")
radar issue –
Cookhouse NSLocalizedString
with string literals only. For example, this message is not found: localizedName = NSLocalizedString("Popular Now", comment: "Name of the menu item to select content that is popular now")
-- This is a really big deal. I must find a solution for this a.s.a.p. I'll be looking into philipp's recommendation to try out github.com/KeepSafe/genstrings_swift –
Emptyhanded cd ~/Workspace/ios-ecentral-app/ECentral/;find ./ -type f \( -iname \*.m -o -iname \*.swift \) | xargs genstrings -o ECentral/localization/en.lproj/'
–
Emptyhanded 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 .
find . -name \*.swift -print0 | xargs -0 genstrings -o .
, no need for replacing anything with tr
. –
Providential 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
genstrings
tool only finds the message from the Objective-C source code. This genstrings_swift tool, can it be run on a mixed-code project like mine and find all the messages in one go, or do I need to follow a sequence of steps? –
Emptyhanded cd ~/Workspace/ios-ecentral-app/ECentral/;find . -name \*.m | xargs genstrings -o ECentral/localization/en.lproj/
–
Emptyhanded alias genstringsECentral='cd ~/Workspace/ios-ecentral-app/ECentral/;find ./ -type f \( -iname \*.m -o -iname \*.swift \) | xargs genstrings -o ECentral/localization/en.lproj/'
-- works like a charm now. –
Emptyhanded 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.
There is a similar question here: How to use genstrings across multiple directories?
find ./ -name "*.m" -print0 | xargs -0 genstrings -o en.lproj
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
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
© 2022 - 2024 — McMap. All rights reserved.