Graphics Programs Reference
In-Depth Information
NSLocalizedString and Strings Tables
In many places in your applications, you create NSString instances dynamically or dis-
play string literals to the user. To display translated versions of these strings, you must cre-
ate a strings table . A strings table is a file containing a list of key-value pairs for all of the
strings that 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:
@"Hello!"
To internationalize a string in your code, you replace literal strings with the function
NSLocalizedString .
NSString *translatedString =
NSLocalizedString(@"Hello!", @"The greeting for the user");
This function takes two arguments: a key and a comment that describes the string's use.
The key is the lookup value in a strings table. At runtime, the NSLocalizedString
function 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 the translated
string that matches the key.
Now you're going to internationalize the string “Homepwner” that is displayed in the nav-
igation bar. In ItemsViewController.m , locate the init method and change the line
of code that sets the title of the navigationItem .
- (id)init
{
// Call the superclass's designated initializer
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
UINavigationItem *n = [self navigationItem];
[n setTitle:@"Homepwner"];
[n setTitle:NSLocalizedString(@"Homepwner", @"Name of application")];
Once you have a file that has been internationalized with the NSLocalizedString
function, 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:
cd /iphone/Homepwner/Homepwner/
Search WWH ::




Custom Search