Getting Started with Localizations!

I recently had to setup localizations in an iOS app and I was surprised by how little content there actually was out there on how to do this in a way that works. That is, there have been a few changes in how it works over time, and some articles are dated and others are not. So along that train of thought, I figured I’d make yet another article about the topic to contribute to the body of general knowledge. Here goes….

Collecting All Your Strings

  1. Create a new file called Localizable.strings
    1. Go to New File -> Source -> String Localizable.strings File Creation
  2. Your Localizable.string filename is special, don’t change it. Macros defined later on will rely upon this specific filename in order to do a proper lookup.
  3. Go through our app (this is where it got tricky with the other online resources), and look for places where you have strings defined – either in plain text or whatever else. Your “Localizable.strings” file is really just a listing of keys, values, and delimiters. Go ahead and copy down your strings as show below, and also assign them unique variable names. Don’t forget the semicolon or you’ll get a compile error.
    1. Localizable.Strings Contents
  4. In the same places where you had those strings in your code, replace the actual string with the variable name defined in the Localizable.strings file. That is, if we have “DISCOVERY_MUSIC” = “Discovery Music” in the Localizable.strings file, replace “Discovery Music” in your code with NSLocalizedString(“DISCOVERY_MUSIC”,””).
    1. The second argument in this macro is more useful if you have a systematic way of obtaining translations (through a 3rd party or some such). This is because this comment is used to get some context about where this specific string shows up in the app.
    2. NSLocalized String Example

 

Great – so now you’ve collected your strings in one place. What about your storyboards? What about things in your nibs? More on that in a second, we’re going to do a bit more configuration in Xcode. What we’ve done so far is only setup for creating our localizations.

Modifying the Project

  1. Go to your Project file or Workspace File
  2. Click on your Project (listed above your Targets)
  3. Click on the Info Tab
    1. Project Modification
  4. You’ll notice the “+” signs on the botton. Go ahead and click it and then select an accompanying language you’d like to localize to. Go ahead and click it and then follow it through to the next screen. Here you’re selecting the files you’d like to localize, go ahead and click OK.
    1. Project Modification, Adding a Localization
  5. At this point you’ll notice your storyboard has Main.strings files underneath it. This means that now your storyboard has a Localizable.strings equivalent, and now you can replace the text with the appropriate language as necessary.

But what about my Localizable.strings file?

Hey it got ignored!

  1. If you haven’t done it before, you may need to select the Localizable.strings File
  2. Open the property inspector (the right panel)
  3. Under Localization section, slick Localize…
  4. Select the languages you want to add a localization to and click OK

I don’t think you actually need to do this super often because once you localize this file, overtime you add a localization as per the previous section, it’ll just go ahead and auto generate these localizations.

But is everything working? Testing Your Localizations

Okay – so far so good. Now we need to make sure it works. There’s a few ways to go about testing this.

Option 1: Change the region on the iOS device

Option 2: Change the build Setting

Before we go there, let’s do a few things for cleanup sake – it’ll also save from a few esoteric bugs.

  1. Delete the project from your iOS Simulator
  2. Run Clean

Both of these things should clean up any non-localized builds or caches you had previously. This is good because sometimes Xcode will error out about random things because we just made some hefty changes to your project file.

Option 1

  1. Launch the iOS Simulator
  2. Go to Settings -> General -> Language & Region -> iPhone Language
  3. Change to whatever language you want, be aware that it’ll change your iOS language on the device
  4. Launch the iOS App

In this option, the language should just automatically take without any configuration. It’s nice because it keeps you from messing with schemes. It sucks cause you have to click through a bunch of random things.

Option 2

  1. Go to Product -> Scheme -> Edit Scheme in XCode
  2. Change the Application Language as below.
    1. Change the Application Language
  3. Build!

This option is nice simply because it’s faster. That said, remember to switch the scheme back to what it’s supposed to be at some point.

Importing and Exporting Translations

Almost done! You probably noticed, there’s a lot of translations. If you’re using a 3rd party and you have a high volume, we need a way to quickly import and export a bunch of localizations so we don’t have to sit around editing strings in XCode. That’s not very efficient. For that, we can export and import localization files by:

  1. Go to Editor -> Export for Localization
  2. Click OK

This will export an .xliff file – an XML file. This file will contain all the Localizable.strings as well as the Main.strings files.

Done!

That’s it! There may be neater/smarter ways to do this, but this is what I’ve been able to cobble together in terms of getting this thing to work. If the above wasn’t clear, check out: http://www.ibabbleon.com/iphone_app_localization.html for more information and help.