Internationalization (i18n)

Hello, World!
¡Hola, Mundo!
!أهﻼ بالعالم

Internationalize the app for English, Spanish, and Arabic. The International Organization for Standardization (ISO) has two-letter codes (ISO 639–1) for the natural languages: EN, ES, AR. (The above Arabic is pronounced “Ahalan bil-Alam!” I went to the Hello, World! page in Wikipedia and clicked on Arabic in the left panel. It’s right above Azərbaycanca.)

Internationalize the strings in the .m files

  1. In the Hello folder that contains the Hello, World! project, create subfolders named en.lproj, es.lproj, ar.lproj. Open the Hello folder, control-click under the last item, and select New Folder.
  2. In each of these three new folders, use TextEdit.app to create a file named Localizable.strings. The Localizable.strings in en.lproj will contain the following line.
    "Greeting" = "Hello, World!";	/* Don't forget the semicolon. */
    
    After you have typed this file into TextEdit.app, save it with the following commands.

    Format → Make Plain Text
    Convert this document to plain text? OK
    File → Save As…
    Save As: Localizable.strings
    Plain Text Encoding: Unicode (UTF-16)
    Save
    You have used the extension ".strings" at the end
    of the name. The standard extension is ".txt". Use strings.


    Look in the en.lproj folder and make sure you saved the file as Localizable.strings, not as Localizable.txt.
  3. The Localizable.strings in es.lproj will contain
    "Greeting" = "¡Hola, Mundo!";
    
    To get the upside down exclamation point ¡,
    Edit → Special Characters…
    View: Roman
    By Category
    Punctuation
  4. The Localizable.strings in ar.lproj will contain
    "Greeting" = "!أهﻼ بالعالم";
    
  5. Add the three Localizable.strings files to the Supporting Files folder of the project. See Icon for instructions on how to add a file to a project.
  6. In the drawRect: method of class View, change
    	NSString *string = @"Hello, World!";
    
    to
    	NSString *string = NSLocalizedString(@"Greeting", @"displayed with drawAtPoint:");
    
  7. In the iPhone Simulator, launch the Settings app.
    Settings → General → International → Language → Español
    Done

    To internationalize a date picker and the other exotic controls, you will also have to set
    Settings → General → International → Region Format → Spanish → Spain
    Done

    Ajustando idioma!
  8. Press Run.

Internationalize the strings in the Info.plist file

The name of the app is listed in the Hello-Info.plist file. Strings in this file have to be internationalized in a different way.

Open the Hello-Info.plist file in the Supporting Files folder of the Xcode Project Navigator. One of the keys has the user-friendly name Bundle display name. To see the real name of this key, control-click on the name and select Show Raw Keys/Values. Note that the name of the key changes to the single word CFBundleDisplayName. To change it back, unselect Show Raw Keys/Values.

  1. Create three UTF-16 files named InfoPlist.strings in the folders en.lproj, es.lproj, ar.lproj. They will each contain one line:
  2. CFBundleDisplayName = "Hello, World!";
    
    CFBundleDisplayName = "¡Hola, Mundo!";
    
    CFBundleDisplayName = "!أهﻼ بالعالم";
    
  3. Add the three InfoPlist.strings files to the project.
  4. Press Run. Press the Home button in the iPhone Simulator to terminate your app. You should see the internationalized name under the app’s icon.

Things to try

  1. Change the greeting to “Wait n minutes.”, where n is a variable in the app. Change the Localizable.strings files to
    "Greeting" = "Wait %d minutes.";
    
    "Greeting" = "Espere %d minutos.";
    
    "Greeting" = "دقائق %d انتظري";
    
    For the %d, see the list of formats. In the drawRect: method of class View, change the string to
    	int n = 5;	//minutes
    	NSString *format = NSLocalizedString(@"Greeting", @"displayed with drawAtPoint:");
    	NSString *string = [NSString stringWithFormat: format, n];
    
    See Formatting String Resources.