amitsbajaj
amitsbajaj

Reputation: 1314

Localization and User-Facing Text in Storyboards

I am learning from the iOS 9 AppCoda book on Swift (https://www.appcoda.com/swift/) and I am stuck in particular on the Localization section. So the author has gone through with changing some user-facing strings in code from just String to NSLocalizedString. That makes sense and the changes I made there were automatically updated appropriately with the XLIFF file. In the section about Localisation, the author mentions that you can also translate Storyboard user-facing Strings with the Export Localization feature of Xcode.

Because it's a book you work through, he provides an already translated XLIFF file into Chinese and German which includes the source code translated text and the Storyboard elements translated as well.

When I import the files into Xcode, I see three storyboards (Base and the Chinese/German.strings, etc) but when I run the app, none of the Storyboard elements are actually translated and only the elements from the source code.

When I click on the German Storyboard, I get the "no localized strings".

enter image description here

The app in his example works and the UI elements in storyboard are translated but they're not in my case. The entire app thus far has been followed with the exercises so there aren't really any differences. Or even if there are, the similarities themselves should be translated, but in the Storyboard elements, they're not.

Does anyone have any ideas on why the Storyboard elements wouldn't be updated with the Translated text in my case?

Any thoughts would be appreciated.

Upvotes: 0

Views: 1556

Answers (2)

Felipe Ferri
Felipe Ferri

Reputation: 3598

Here is my guess... Exporting localization strings from Storyboards works a little different from exporting those strings from code.

The localization file for strings from code gets the actual text from the NSLocalizedString macro and creates a mapping such as:

"Original string" = "Translated string".

So, you just have to use "Original string" in your code then Xcode knows it should translate it to "Translated string".

However, the storyboard localisation file uses the UI objects handles to set localized strings. For example:

/* Class = "UILabel"; text = "Ops..."; ObjectID = "fe4-zT-gjU"; */
"fe4-zT-gjU.text" = "Wait for it...";

In this example my storyboard has a UILabel, with the text "Ops...", and that label has an ObjectID equal to "fe4-zT-gjU".

The object IDs are kinda unique for each object in each project, so obviously the object IDs from the app coda tutorial will be unique for the project the author created; unless you have downloaded the author's original storyboard, your storyboard will have all different object IDs, so Xcode won't know how to associate the correct translations to the correct objects.

I don't think Xcode will import localisation for objects with objectIDs it doesn't recognise.

So what I think you should do is to perform an "Export for localisation" operation. This should update your storyboard .strings file. Then you copy each translation from the tutorial file into your .strings file using an xliff editor. Boring, I know. Welcome to the app development world. :-)

Upvotes: 2

D. Greg
D. Greg

Reputation: 1000

When you added your localization did you select the storyboard file?

enter image description here

If so, then check that your scheme is set to the correct language. This can be done under Product > Scheme > Edit Scheme

enter image description here

If that doesn't do it, then check that your simulator is set to the language you want to display. This can be done in the simulator's settings, same as an iPhone.

Hope this helps.

Upvotes: 0

Related Questions