Finding Non-localized Strings in xcode
If you have ever done some localization in xcode, you might already know how hard is it to track the non-localized strings used in the code and in storyboard. From xcode 7, there is an easy way to do that. Let me show you that.
I am not going to explain how to do localization in iOS in this article.
Rushing through the basics, we can do localize the string in two ways.
- In code by using
NSLocalizedString("someStringToLocalize", tableName: nil, bundle: Bundle.main, value: "", comment: "").
- Use storyboard localizations.
It’s common that we add the
NSLocalizedString(: method in the code and to forget to add it in the
Localizable.strings file. Also, we might localize the storyboard file and will add a new label later on in the UI which you will forget to add in the localized storyboard strings file.
To track these kind of silly mistakes, xcode 7 and above provides an option.
In the scheme editor take a look at the Options tab. In the following screenshot I have “Show non-localized strings” enabled .
Show non-localized strings checkbox needs to be checked and we can select the application language as required. Then compile and run the app.
I have added a string as a label’s text (
calculatorDisplay is a UILabel):
calculatorDisplay.text = NSLocalizedString(“Some localized string”, comment: “”)
Also, the string
“Some localized string” is not added in the
Localizable.strings file. In this scenario, it will throw an error in the debugger.
Localizable strings in storyboard:
For the localizable strings in storyboard which are not added in the
<YOUR_STORYBOARD_NAME>.strings(YOUR_LANGUAGE) file, the debugger will throw error like:
Let’s check the first error:
GsZ-Y5–7R1mentioned in the error and search for it throughout the project.
It appears to be a UIButton’s Object ID and the localizable text is the UIButtons title . Click on the search result and it may highlight the object in storyboard.
The storyboard strings file normally looks like this:
GsZ-Y5–7R1 is the objectid of a UIButton. Now copy the
GsZ-Y5–7R1.normalTitle and add it to the strings file.
“GsZ-Y5–7R1.normalTitle" = "Localized title for button";
How to localize the strings file and localizing the storyboard files are not covered in this article. This process is pretty easy. I am planning to create an article on localization and internationlization in the near future.
Hope you learned something new today.
Note: Fabrizio Bernasconi mentioned a website to generate the localizable strings file. Just give it a try.
Generate Localizable.strings from XCode project:
If you enjoyed reading this post, please share and recommend it so others can find it 💚💚💚💚💚💚 !!!!
You can follow me on Medium for fresh articles. Also, connect with me on LinkedIn.
If you have any comment, question, or recommendation, feel free to post them in the comment section below!