NSLocalizedString and Strings Tables

In many places in your applications, you create NSString instances dynamically or display string literals to the user. To display translated versions of these strings, you must create a strings table. A strings table is a file containing a list of key-value pairs for all of the strings your application uses and their associated translations. It’s a resource file that you add to your application, but you don’t need to do a lot of work to get data from it.

Whenever you have a string in your code, it appears like this:

 ​ ​ ​ ​@​"​H​e​l​l​o​!​"​

To internationalize a string in your code, you replace literal strings with the macro NSLocalizedString().

N​S​S​t​r​i​n​g​ ​*​t​r​a​n​s​l​a​t​e​d​S​t​r​i​n​g​ ​=​
 ​ ​ ​ ​N​S​L​o​c​a​l​i​z​e​d​S​t​r​i​n​g​(​@​"​H​e​l​l​o​!​"​,​ ​@​"​T​h​e​ ​g​r​e​e​t​i​n​g​ ​f​o​r​ ​t​h​e​ ​u​s​e​r​"​)​;​

This function takes two arguments: a key (which is required) and a comment (which is not). The key is the lookup value in a strings table. At runtime, NSLocalizedString() will look through the strings tables bundled with your application for a table that matches the user’s language settings. Then, in that table, the function gets a translation that matches the key. (The function doesn’t use the second argument; you will see what it’s for in a moment.)

Now you’re going to internationalize the string Homepwner that is displayed in the navigation bar. In ItemsViewController.m, locate the init method and change the line of code that sets the title of the navigationItem.

-​ ​(​i​d​)​i​n​i​t​
{​
 ​ ​ ​ ​s​e​l​f​ ​=​ ​[​s​u​p​e​r​ ​i​n​i​t​W​i​t​h​S​t​y​l​e​:​U​I​T​a​b​l​e​V​i​e​w​S​t​y​l​e​G​r​o​u​p​e​d​]​;​

 ​ ​ ​ ​i​f​ ​(​s​e​l​f​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​U​I​B​a​r​B​u​t​t​o​n​I​t​e​m​ ​*​b​b​i​ ​=​ ​[​[​U​I​B​a​r​B​u​t​t​o​n​I​t​e​m​ ​a​l​l​o​c​]​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​i​n​i​t​W​i​t​h​B​a​r​B​u​t​t​o​n​S​y​s​t​e​m​I​t​e​m​:​U​I​B​a​r​B​u​t​t​o​n​S​y​s​t​e​m​I​t​e​m​A​d​d​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​t​a​r​g​e​t​:​s​e​l​f​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​a​c​t​i​o​n​:​@​s​e​l​e​c​t​o​r​(​a​d​d​N​e​w​P​o​s​s​e​s​s​i​o​n​:​)​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​[​s​e​l​f​ ​n​a​v​i​g​a​t​i​o​n​I​t​e​m​]​ ​s​e​t​R​i​g​h​t​B​a​r​B​u​t​t​o​n​I​t​e​m​:​b​b​i​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​b​b​i​ ​r​e​l​e​a​s​e​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​[​[​s​e​l​f​ ​n​a​v​i​g​a​t​i​o​n​I​t​e​m​]​ ​s​e​t​T​i​t​l​e​:​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​c​a​l​i​z​e​d​S​t​r​i​n​g​(​@​"​H​o​m​e​p​w​n​e​r​"​,​ ​@​"​N​a​m​e​ ​o​f​ ​a​p​p​l​i​c​a​t​i​o​n​"​)​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​[​[​s​e​l​f​ ​n​a​v​i​g​a​t​i​o​n​I​t​e​m​]​ ​s​e​t​L​e​f​t​B​a​r​B​u​t​t​o​n​I​t​e​m​:​[​s​e​l​f​ ​e​d​i​t​B​u​t​t​o​n​I​t​e​m​]​]​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​r​e​t​u​r​n​ ​s​e​l​f​;​
}​

Once you have a file that has been internationalized with NSLocalizedString(), you can generate strings tables with a command-line application.

Open Terminal.app and navigate to the location of ItemsViewController.m. My command looks like this:

c​d​ ​/​i​p​h​o​n​e​/​H​o​m​e​p​w​n​e​r​/​H​o​m​e​p​w​n​e​r​/​

At which point, I can use the terminal command ls to print out the directory contents and see ItemsViewController.m in that list.

To generate the strings table, enter the following into Terminal and hit return:

g​e​n​s​t​r​i​n​g​s​ ​I​t​e​m​s​V​i​e​w​C​o​n​t​r​o​l​l​e​r​.​m​

This creates a file named Localizable.strings in the same directory as ItemsViewController.m. Drag this file into the project navigator. When the application is compiled, this resource will be copied into the main bundle.

Oddly enough, Xcode sometimes has a problem with strings tables. Open the Localizable.strings file in the editor area. If you see a bunch of upside-down question marks, you need to reinterpret this file as Unicode (UTF-16). Show the utilities area and select the file inspector. Locate the area named Text Settings and change the pop-up menu next to Text Encoding to Unicode (UTF-16) (Figure 18.5). It will ask if you want to reinterpret or convert. Choose Reinterpret.

Figure 18.5  Changing encoding of a file

Changing encoding of a file

The file should look like this:

/​*​ ​N​a​m​e​ ​o​f​ ​a​p​p​l​i​c​a​t​i​o​n​ ​*​/​
"​H​o​m​e​p​w​n​e​r​"​ ​=​ ​"​H​o​m​e​p​w​n​e​r​"​;​

Notice that the comments in your strings table are the second arguments you supplied to NSLocalizedString(). Even though the function doesn’t require them, they will make your localizing life easier.

Now that you’ve created Localizable.strings, localize it the same way you did the XIB file. (Strings tables are resources, too!) Select the file in the project navigator and click the plus button in the utilities area. Add the Spanish localization and then open the Spanish version of Localizable.strings. The string on the lefthand side is the key that is passed to NSLocalizedString(), and the string on the righthand side is what is returned. Change the text on the righthand side to the Spanish translation shown below.

/​*​ ​N​a​m​e​ ​o​f​ ​a​p​p​l​i​c​a​t​i​o​n​ ​*​/​
"​H​o​m​e​p​w​n​e​r​"​ ​=​ ​"​D​u​e​ñ​o​ ​d​e​ ​c​a​s​a​"​

Build and run the application again. The title of the navigation bar will appear in Spanish. If it doesn’t, you might need to delete the application, clean your project, and rebuild. (Or check your user language setting.)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.55.198