Developers will frequently want to localize their application, or in other words, translate it to work for various languages.
This is by no means a comprehensive guide, but rather what I have learned about localization.
- Localizing your application
- In your Xcode project, choose File ▸ New File...
- From the "Other" category, add a new "Strings File" called "Localizable.strings" to your project.
- Populate Localizable.strings with Key-Value pairs in the following format:
"Key" = "Value";
- Right click on "Localizable.strings" in your project tree and choose "Get Info"
- In the "General" tab, click the button at the bottom, called "Make File Localizable". This will jump you to the "Targets" tab.
- Go back to the "General" tab, and click the "Add Localization" button at the bottom.
- Name the new localization after the two letter language abbreviation that corresponds to the desired language. (Ex: Spanish = es, Portuguese = pt, French = fr, etc)
- You'll see that Localizable.strings is now a group in your project tree. Expanding it reveals the added localizations. Choose one.
- The localization has been populated with the values that you added in Localizable.strings. Simply translate the Value to the localized version. For example, in your English version, you might have:
"Greeting" = "Hello";
In your "es" (Español) file, you would have:
"Greeting" = "Hola";
- Now, in your code, whenever you want to have a localized greeting, simply used the
NSLocalizedStringmacro, like so:
[greetingTitle setTitle:NSLocalizedString(@"Greeting", "greeting to use")];
greetingTitlewill display the appropriate string ("Hello" versus "Hola") depending on what language the user has set as their default!
This same procedure also allows you to localize more than just string values. You can also localize your Info.plist file, .xib/.nib files, and more. One caveat is that in order to localize your application's name (generally contained in Info.plist), you should first create a strings file entitled "InfoPlist.strings" and put in the line:
CFBundleDisplayName = "Your App Name";
Then localize the "InfoPlist.strings" file as explained above.
For further information, check out the Internationalization Programming Guide on the Apple Developer Connection.