iOS Reference Library Apple Developer
Search

Internationalizing Other Resources

Some user-interface objects may localize their content automatically when they parse or format data. For example, if you have a Mac OS X text field configured with a number formatter, the formatter automatically accounts for the user’s locale preference when formatting the number. However, other objects might require you to specify the current locale explicitly.

Resources and Core Foundation

If you are using the Core Foundation framework and need to format or scan date or number information, use the CFDateFormatterRef and CFNumberFormatterRef types. These types use the current locale information to ensure that dates and numbers are formatted correctly. For more information, see CFDateFormatter Reference and CFNumberFormatter Reference.

Resources and Cocoa

If you are formatting or scanning dates or numbers yourself using low-level objects such as NSString, NSDate, or NSScanner, you should use the current locale information if the resulting text will be seen by the user. The NSLocale class in the Foundation framework also makes it easier to get locale information. In addition to that class, several Foundation framework classes offer methods that provide an explicit locale argument:

A locale is represented as a dictionary, using key/value pairs to store information about how the localization should be performed. Some of the possible keys in this dictionary are listed in Foundation/NSUserDefaults.h. Typically you pass either nil or the dictionary built from the standard user defaults. To create the dictionary from the user preferences, you would use the following code:

[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]

This code returns a dictionary with a flattened view of the user's defaults and languages in order of user preference. The user's defaults take precedence, so any language-specific information might be overridden by entries in defaults (which are typically set from user's preferences).

The Application Kit framework makes localization easier by providing cover methods that ask for a “localized” version of an object, such as:

- (id)initWithFormat:(NSString *)format locale:(NSDictionary *)dict, ...;
+ (id)localizedStringWithFormat:(NSString *)format, ...;

The second method calls the first one with the dictionary representation of the user’s current defaults:

[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]

In versions of these methods without a locale argument, the processing is done in a non-localized manner.




Last updated: 2009-08-07

Did this document help you? Yes It's good, but... Not helpful...