Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning iOS5 Development.pdf
Скачиваний:
7
Добавлен:
09.05.2015
Размер:
15.6 Mб
Скачать

CHAPTER 21: Application Localization

693

Trying Out LocalizeMe

You can use either the simulator or a device to test LocalizeMe. The simulator does seem to cache some language and region settings, so you may want to run the application on the device if you have that option. Once the application launches, it should look like Figure 21–2.

Figure 21–2. The language running under the authors’ base language. Our application is set up for localization but is not yet localized.

By using the NSLocalizedString macros instead of static strings, we are ready for localization, but we are not localized yet. If you use the Settings application on the simulator or on your iPhone to change to another language or region, the results look essentially the same, except for the label at the top of the view (see Figure 21–3).

www.it-ebooks.info

694

CHAPTER 21: Application Localization

Figure 21–3. The nonlocalized application run on an iPhone set to use the French language in France

Localizing the Nib

Now, let’s localize the nib file. The basic process for localizing any file is the same. In Xcode, single-click BIDViewController.xib, and then select View Utilities Show File Inspector to bring up the file inspector to see detailed information for the nib file.

CAUTION: Xcode will allow you to localize pretty much any file in the navigator. Just because you can, doesn’t mean you should. Never localize a source code file. Doing so will cause compile

errors, as multiple object files with the same name will be created.

Look for the Localization section of the file inspector. You’ll see that it shows a single localization: English. Click the plus (+) button at the bottom of the Localization section, and select French (fr) from the popup list that appears (see Figure 21–4).

www.it-ebooks.info

CHAPTER 21: Application Localization

695

Figure 21–4. The file inspector showing localization and other information for BIDViewController.xib

After adding a localization, take a look at the project navigator. Notice that the BIDViewController.xib file now has a disclosure triangle next to it, as if it were a group or folder. Expand it, and take a look (see Figure 21–5).

Figure 21–5. Localizable files have a disclosure triangle and a child value for each language or region you add.

In our project, BIDViewController.xib is now shown as a group containing two children: one tagged as English and one as French. The English version was created automatically when you created the project, and it represents your development base language.

www.it-ebooks.info

696

CHAPTER 21: Application Localization

Each of these files lives in a separate folder, one called en.lproj and one called fr.lproj. Go to the Finder, and open the LocalizeMe folder within your LocalizeMe project folder. In addition to all your project files, you should see folders named en.lproj and fr.lproj (see Figure 21–6).

Figure 21–6. From the outset, our Xcode project included a language project folder for our base language. When we chose to make a file localizable, Xcode created a language project folder for the language we selected as well.

Note that the en.lproj folder was there all along, with its copy of BIDViewController.xib inside it all the while. When Xcode finds a resource that has exactly one localized version, it displays it as a single item. As soon as a file has two or more localized versions, they’re displayed as a group.

TIP: When dealing with locales, language codes are lowercase, but country codes are uppercase. So, the correct name for the French language project is fr.lproj, but the project for Parisian French (French as spoken by people in France) is fr_FR.lproj, not fr_fr.lproj or

FR_fr.lproj. The iOS file system is case-sensitive, so it is important to match case correctly.

When you asked Xcode to create the French localization, Xcode created a new localization project in your project folder called fr.lproj and placed a copy of

BIDViewController.xib in that folder. In Xcode’s project navigator, BIDViewController.xib should now have two children: English and French. Select French to open the nib file that will be shown to French speakers.

The nib file that opens in Interface Builder will look exactly like the one you built earlier, because the nib file you just created is a copy of the earlier one. Any changes you make to this file will be shown to people who speak French. Double-click each of the labels on the left side and change them from One, Two, Three, Four, and Five to Un, Deux, Trois, Quatre, and Cinq. Then save the nib.

Your nib is now localized in French. Compile and run the program. After it launches, tap the home button.

If you’ve already changed your Settings to the French region and language, you should see your translated labels on the left. For those folks who are a bit unsure about how to make those changes, we’ll walk you through it.

www.it-ebooks.info

CHAPTER 21: Application Localization

697

In the simulator, go to the Settings application, and select the General row and then the row labeled International. From here, you’ll be able to change your language and region preferences (see Figure 21–7).

Figure 21–7. Changing the language and region—the two settings that affect the user’s locale

You want to change the Region Format first, because once you change the language, iOS will reset and return to the home screen. Change the Region Format from United States to France (first select French, then select France from the new table that appears), and then change Language from English to Français. Click the Done button, and the simulator will reset its language. Now, your phone is set to use French.

Run your app again. This time, the words down the left-hand side should show up in French (see Figure 21–8). But the flag and right column of text are still wrong. We’ll take care of the flag first.

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]