Tag Archives: translations

How to encode / convert a .strings file into UTF-16LE in Xcode

When you create a new .strings file in Xcode it will be encoded as Unicode UTF-8. That’s sufficient to hold all the special characters you can dream of.

Some translation services such as ICanLocalize require .strings files to be submitted in UTF-16LE though. Puzzled about what this even means I found out that UTF-16LE can hold languages such as Chinese and Hindu in smaller sizes than UTF-8 could. It’s a bit theoretical, but the good news is that Xcode 5 can change this very easily:

  • select your file in Xcode
  • open the right hand pane and select the File Inspector (little piece of paper icon)
  • under Text Settings, find a drop down menu that lets you select a new encoding method for your file

Screen Shot 2013-11-24 at 19.37.05

Xcode will offer to either re-interpret the current file, or to convert it. Choose the latter and save your file (CMD+S), and all is well again with the world.

You can now choose to upload your file for a free quote:

Xcode only builds with UTF-8

Note that once you change this format, Xcode may complain that it can no longer build your app, with an error message such as
Copy .strings Error – Read failed: The data couldn’t be read because it isn’t in the correct format.

In which case, just change it back to UTF-8, choose convert and build again successfully.

Further reading:

How to retrieve the current User Locale

The following method will return a two letter code of which language is set on the iOS device in question:

[emember_protected]

There’s a list of language codes on Wikipedia: http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes

Note that to compare the current value against a list of languages you support we need use the isEqualToString method. Here’s an example:

[/emember_protected]

The following articles have helped me figure this out:

How to localize your project in Xcode 4.4

Before we start we need to create a new file called Localizable.strings (from File – New – Resource – Strings File). Xcode needs one file per language. Next we create localized copies of our files:

[emember_protected]

  • head over to your Project Editor (the big blue icon)
  • select it again under Project (another blue icon)
  • make sure INFO is selected
  • find the Localizations box
  • hit the + button to add a language of your choice

Xcode will bring up a dialogue box and suggest files it can translate. Leave them all ticked (or uncheck the ones you don’t need) and hit finish.

Notice that Xcode has now created the translated files underneath the original files: Storyboards and Localizable.strings:

Technically with two languages there are only two files – the first one is just a heading to clarify. Prior to 4.4 Xcode would create a separate folder per language (like en.lproj) and add the files there but that’s not the case anymore.

Storyboard files can be translated “by hand”, whereas string files can be translated using key/value pairs in your Localizable.strings file. Here’s an example: to print the text in a label, instead of

we’d use the following:

In a Localizable.strings file for English we define

and Xcode (or rather the iPhone) will print out this text when our phone is set to English (under Settings – General – International – Language). In the Localizable.strings file for any other language we can define translations with the same key, for example

This would be displayed when the iPhone is set to German.

[/emember_protected]

You can add other translations to files such as Localizable.strings simply by selecting the file on the left, and then adding a translation on the right hand side under “Localizations” in the File Inspector: