2

I am currently localising a quite large Swift project and I have changed all strings in the .swift files to NSLocalizedString. Now it seems that I have to create a .strings file manually and add every string to this file for a base localisation.

Is there a way to generate this file? As far as I can see there are about 4000 strings in the code and I cannot believe that this has to be done manually. I found the genstrings script in the Apple documentation but it seems that it was designed for objc only so it has no Swift support.

2
  • 2
    You are looking for genstrings. Commented Dec 15, 2017 at 15:52
  • Hm it really seems that genstrings also works with swift and the apple docs are a little bit outdated. Thanks! Commented Dec 15, 2017 at 16:03

1 Answer 1

4

With genstrings command line tool that's packaged with Xcode, we use that to extract all of the strings from our "NSLocalizedString" macros and compile a strings file.

The first thing that I need to create a new file in the root of our project directory.

Add a new strings file and call it "Localizable", that's the default title and put it in our base project folder. So here we have an empty "Localizable.strings" file. So now switch over to Terminal and run this command is your project directory not folder.

Use this command to import strings from Swift files only

find . -type f -name \*.swift -print0 | xargs -0 genstrings -o Base.lproj

use the genstrings command line tool to output our strings into the localizable strings file right here in our base folder.If you use objC project, run genstrings command below

Use this command to import strings from Objective-C implementation files only

find . -type f -name \*.m -print0 | xargs -0 genstrings -o Base.lproj

It didn't take long at all for your project and it extracted all of the strings.

Use this command to import strings from both Objective-C implementation files and Swift files

find . -type f \( -name \*.m -o -name \*.swift \) -print0 | xargs -0 genstrings -o Base.lproj
Sign up to request clarification or add additional context in comments.

1 Comment

Be careful, genstrings generates utf16 encoded files, which are recognised by GitHub as binary files and so if you use this it is suggested that you convert them back to utf8 encoded so they are still shown correctly in diff tools (including GitHub): stackoverflow.com/questions/37542388/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.