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
- Create a new file called Localizable.strings
- 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.
- 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.
- 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”,””).
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
- Go to your Project file or Workspace File
- Click on your Project (listed above your Targets)
- Click on the Info Tab
- 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.
- 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!
- If you haven’t done it before, you may need to select the Localizable.strings File
- Open the property inspector (the right panel)
- Under Localization section, slick Localize…
- 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.
- Delete the project from your iOS Simulator
- 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.
- Launch the iOS Simulator
- Go to Settings -> General -> Language & Region -> iPhone Language
- Change to whatever language you want, be aware that it’ll change your iOS language on the device
- 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.
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:
- Go to Editor -> Export for Localization
- 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.
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.