Code Based Localisation

Friday, August 14th, 2009

Code Based Localisation in xCode 3.0+ SDK

Code based localisation focuses on any strings you may have in your code. Finding these strings and making them dynamic. This is a contrast to how localisation is done in say Flash where the localisation methods are normally set up by the developer. Kind of like Flex.

If you are after NIB Localisation checkout this post here

Abstract:
A localised project needs a local specific folder that follows a name convention made up of 2 parts. First is the language like “English”, “French” or “Japanese”. The second is the local project abbreviation “.lproj” putting them together you get folders by the name “English.lproj”, “French.lproj” or “Japanese.lproj”. All the local based copy and assets will end up in the respective local folder. You can create these folders manually but I like to get xCode to create them using the localisation functionality built into the IDE.

Basically any text that is set via code needs to be set using NSLocalizedString this takes 2 strings. The first is the the copy that needs localisation. The second string it takes is the comment that appears above the name value pair.  see example:

[c]//use this to set the default copy to "Please enter user name:" in English
self.userNameFeild.text = NSLocalizedString(@"Please Enter your user nsme;", @"user name subbmision copy");
//use this to set the default copy to "Please enter user name:" in french
self.userNameFeild.text = NSLocalizedString(@"Veuillez écrire le nom d’utilisateur:",@"Veuillez écrire le nom d’utilisateur");[/c]

Once you have written your code using NSLocalizedString with all the necessary default copy you will run a terminal script that will pull out the default strings strings to create a .strings file which you would then localise. “genstrings” is the command line tool used to interrogate your .m files to find what needs to be localised.

Once you have your localised .strings file you simply copy it to the different local folders updated the copy to the respective local and bobs  your uncle, its done.

Implementation:

  1. create a new project in xcode it should not matter what type but I will use an iPhone View-Based Application.
  2. add the bellow code to the viewControler of the newly created project to quickly add some uilabels
    [c]- (void)viewDidLoad {
    UILabel *uiLableFirstName =  [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 150, 50)];
    uiLableFirstName.text = NSLocalizedString(@"First name:", @"first Name");
    [self.view addSubview:uiLableFirstName];

    UILabel *uiLableSurName =  [[UILabel alloc] initWithFrame:CGRectMake(50, 150, 150, 50)];
    uiLableSurName.text = NSLocalizedString(@"Last name:", @"sure Name to use");
    [self.view addSubview:uiLableSurName];

    [super viewDidLoad];
    }[/c]

  3. Open the terminal window and navigate too the root of you project folder. This is the folder that contains your Classes folder.
  4. Run “genstrings ./Classes/*.m” in the terminal which will scan every .m file in your Classes folder for any references to NSLocalizedString and added them to a Localizable.strings file which should appear in the root of your project folder.
  5. In the finder navigate to the root of the project folder and then drag and drop Localizable.strings from the Finder to the resource folder in XCode remembering to insure that the “Copy items into…” check box is unchecked. Also make sure that Text Encoding is set to UTF-16 to insure all languages are covered.
  6. Finally press add.
  7. Right-Click the newly added Localizable.strings then select “Get Info”.
  8. Select the General tab from the “Get Info” panel
  9. Click “Make file Localizable”
  10. Click back on the general tab
  11. Click “Add Localization”
  12. Enter your new Locals “French”(from the English name section of the link) and or see here for full list: http://www.loc.gov/standards/iso639-2/php/English_list.php
  13. Once you have added the Locals for Localizable.strings close the “Get Info” window
  14. Run your app go to the settings panel>>Genral>>International change your local and it should display your localised text.

Leave a Reply

Your email address will not be published. Required fields are marked *