Externalizing Strings

Posted on

Hard-coded strings such as error messages and UI elements must be externalized from your class file in order to translate your app. Xcode provides a macro called NSLocalizedStrings() which works with a file called Localizable.strings to facilitate the externalization and localization of these strings.

  • Open your Xcode project.
  • If you do not have a file called Localizable.strings, you need to create one.
    • Create a new file in Xcode.
    • Choose Resource -> Strings File
    • Name the file Localizable.strings and store it in the root of your project.
  • Select Localizable.strings in the Project navigator.
  • Click on the File Inspector for Localizable.strings.
  • Look in the Localization panel.
  • If the panel displays “No Localizations”, click on the “+” button to make the file localizable. English will now appear in the list of languages.
  • Click on the “+” button and add the languages you wish to translate your app into.
  • Localizable.strings is now set up for translation.

Now you need to externalize all of the localizable content from your class files into the file Localizable.strings.

  • Open any class file that contains content to be translated.
  • For every strings literal in the file, you need to wrap the string with a call to the macro NSLocalizedString()
  • There are 2 approaches you can take as to how you manage the externalization and maintenance of these localizable UI strings
  1. Use the actual value of the string as the key in the file Localizable.strings. This will allow you to use the tool genstrings() supplied as part of Xcode to externalize the strings from your files.
  2. Replace the text in your file with a hand-generated key, and then create a key-value pair in Localizable.strings with the new key and the localizable.text.

Using genstrings()

  • Identify the text in your class file to be externalized
  • Wrap this text with a call to NSLocalizedString() as follows:

Change

self.title = @”Gallery”;

to

self.title = NSLocalizedString(@”Gallery”, “This the name of the tab in the home screen that will take you to the gallery sections of the app”);

  • Note that LocalizedString() takes 2 paramaters
  • key = the name of the key in Localizable.strings that will be loaded when the call is invoked
  • comment = developer comment that is used to convey context to the translator, and to other developer who will work on your code

Tags: ,


Leave a Reply

Your email address will not be published. Required fields are marked *