User-interface objects such as cells and text fields localize their content automatically when they parse or format data. Thus if you ask a cell for its numerical value, and the user's locale is French, “,” will be used as the decimal separator. Similarly, if you display a floating point number in cells, table views, and so on, they are localized automatically.
Resources and Core Foundation
Resources and Cocoa
If you are using Core Foundation 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.
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. In Mac OS X v10.4, the NSLocale
class was added to Cocoa to make it easier to get locale information. In addition to that class, several cocoa classes offer methods that provide an explicit locale argument:
NSString
:
- (id)initWithFormat:(NSString *)format locale:(NSDictionary *)dict, ...;
+ (id)stringWithFormat:(NSString *)format, ...;
+ (id)localizedStringWithFormat:(NSString *)format, ...;
NSScanner
:
- (void)setLocale:(NSDictionary *)dict;
+ (id)scannerWithString:(NSString *)string;
+ (id)localizedScannerWithString:(NSString *)string;
NSObject
:
- (NSString *)description;
- (NSString *)descriptionWithLocale:(NSDictionary *)locale;
NSDate
:
- (id)initWithString:(NSString *)description calendarFormat:(NSString *)format locale:(NSDictionary *)dict;
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 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 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 a locale argument of
[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] |
In versions of these methods without a locale argument, the processing is done in a non-localized manner.
© 2003, 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-01-06)