1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mike Nathas
  • 1,247
  • 2
  • 11
  • 29
  • 2
    You are looking for [genstrings](https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man1/genstrings.1.html). – Tamás Sengel Dec 15 '17 at 15:52
  • Hm it really seems that genstrings also works with swift and the apple docs are a little bit outdated. Thanks! – Mike Nathas Dec 15 '17 at 16:03

1 Answers1

3

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
Durul Dalkanat
  • 7,266
  • 4
  • 35
  • 36
  • 1
    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): https://stackoverflow.com/questions/37542388/character-encoding-of-localizable-strings-generated-by-genstrings – simonthumper Jun 30 '22 at 08:14