Extracting & localising .m class strings (GENSTRINGS)
In an ideal world, the possibility of future internationalization should be on our mind every time we set up a new Xcode project. In the real world, it very rarely is. Localization is often an after thought and the further we go through an app’s development and the more strings we add to our .m class files, the more likely we are to be put off by what seems such a massive undertaking.
It doesn’t have to be. Converting all those @”lonely unlocalizable strings” to NSLocalizedStrings is a very simple two step process.
Step 1: Adding the NSLocalizedString function
Open your .m class files and search for all your strings. They should look something like this:
You now want to change this string to the following format, utilising NSLocalizedString:
NSLocalizedString(@"Open Link", @"Prompts user to open link")
So what exactly are you doing here? Well first we’ve added the function NSLocalizedString to our string. We’ve defined the string @”Open Link” and we’ve also added the comment @”Prompts user to open link”.
What? Comments? Oh no! I hear you sigh. Commenting is good practice and can help translators with strings. Use some common sense, a string like the example above probably doesn’t need an extensive comment, but a button like “When you are finished, click confirm” may need good commenting. It’s difficult for a translator to understand a lonely string not in context. “What has finished? Where was the confirm button? How do I make sure the same confirm word is being used?” (Side note: If you use Applingua to do your translation, we check buttons to make sure this doesn’t happen.)
Step 2: Extracting and creating your very own Localizable.strings file
The next step after processing your .m strings is to generate a Localizable.strings file. Simply fire open Terminal, navigate to your project folder and type:
genstrings -o en.lproj *.m
Replace * with your .m class name. This will create the all important Localizable.strings file, containing all your strings and comments ready for translation. Send the file off to your translators along with your other resource files. The hardest bit is over.
In the meantime, you can set up your localized resource folders. Your fr.lproj, de.lproj, etc in your project’s resources folder. When you receive the localized Localizable.strings, add it to the correct language resource folder and you’re ready to go.
How does this work?
Each time your app hits an NSLocalizedString function, it calls the string from the Localizable.strings file. When you generate your Localizable.strings file, you will notice each string is assigned a unique key. This key and the coupled string are called from the resource folder associated with the language you have launched the app. For example, if you are running your app in French then the app will call the key and string from the Resources/fr.lproj/Localizable.strings file. It’s really that simple.
We’d really appreciate it if you could rate this post to let us know how useful you found it! :) Thanks!