| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/Foundation.framework |
| Availability | Available in Mac OS X v10.4 and later. |
| Declared in | NSLocale.h |
| Companion guides | |
| Related sample code |
Locales encapsulate information about linguistic, cultural, and technological conventions and standards. Examples of information encapsulated by a locale include the symbol used for the decimal separator in numbers and the way dates are formatted.
Locales are typically used to provide, format, and interpret information about and according to the user’s customs and preferences. They are frequently used in conjunction with formatters (see Data Formatting Programming Guide for Cocoa). Although you can use many locales, you usually use the one associated with the current user.
NSLocale is “toll-free bridged” with its Core Foundation counterpart, CFLocale. This means that the Core Foundation type is interchangeable in function or method calls with the bridged Foundation object. Therefore, in a method where you see an NSLocale * parameter, you can pass a CFLocaleRef, and in a function where you see a CFLocaleRef parameter, you can pass an NSLocale instance (you cast one type to the other to suppress compiler warnings). See Interchangeable Data Types for more information on toll-free bridging.
+ availableLocaleIdentifiers
+ ISOCountryCodes
+ ISOCurrencyCodes
+ ISOLanguageCodes
+ commonISOCurrencyCodes
+ canonicalLocaleIdentifierFromString:
+ componentsFromLocaleIdentifier:
+ localeIdentifierFromComponents:
+ canonicalLanguageIdentifierFromString:
+ localeIdentifierFromWindowsLocaleCode:
+ windowsLocaleCodeFromLocaleIdentifier:
Returns the current logical locale for the current user.
+ (id)autoupdatingCurrentLocale
The current logical locale for the current user. The locale is formed from the settings for the current user’s chosen system locale overlaid with any custom settings the user has specified in System Preferences.
The object always reflects the current state of the current user's locale settings.
Settings you get from this locale do change as the user’s settings change (contrast with currentLocale).
Note that if you cache values based on the locale or related information, those caches will of course not be automatically updated by the updating of the locale object. You can recompute caches upon receipt of the notification (NSCurrentLocaleDidChangeNotification) that gets sent out for locale changes (see Notification Programming Topics for Cocoa to learn how to register for and receive notifications).
NSLocale.hReturns an array of NSString objects, each of which identifies a locale available on the system.
+ (NSArray *)availableLocaleIdentifiers
An array of NSString objects, each of which identifies a locale available on the system.
NSLocale.hReturns a canonical language identifier by mapping an arbitrary locale identification string to the canonical identifier.
+ (NSString *)canonicalLanguageIdentifierFromString:(NSString *)string
A string representation of an arbitrary locale identifier.
A string that represents the canonical language identifier for the specified arbitrary locale identifier.
NSLocale.hReturns the canonical identifier for a given locale identification string.
+ (NSString *)canonicalLocaleIdentifierFromString:(NSString *)string
A locale identification string.
The canonical identifier for an the locale identified by string.
NSLocale.hReturns the character direction for the specified ISO language code.
+ (NSLocaleLanguageDirection)characterDirectionForLanguage:(NSString *)isoLangCode
The ISO language code.
Returns the character direction for the language. See “NSLocaleLanguageDirection” for possible values. If the appropriate direction can’t be determined NSLocaleLanguageDirectionUnknown is returned.
NSLocale.hReturns an array of common ISO currency codes
+ (NSArray *)commonISOCurrencyCodes
An array of NSString objects that represents common ISO currency codes.
Common codes may include, for example, AED, AUD, BZD, DKK, EUR, GBP, JPY, KES, MXN, OMR, STD, USD, XCD, and ZWD.
NSLocale.hReturns a dictionary that is the result of parsing a locale ID.
+ (NSDictionary *)componentsFromLocaleIdentifier:(NSString *)string
A locale ID, consisting of language, script, country, variant, and keyword/value pairs, for example, "en_US@calendar=japanese".
A dictionary that is the result of parsing string as a locale ID. The keys are the constant NSString constants corresponding to the locale ID components, and the values correspond to constants where available. For the complete set of dictionary keys, see “Constants.”
For example: the locale ID "en_US@calendar=japanese" yields a dictionary with three entries: NSLocaleLanguageCode=en, NSLocaleCountryCode=US, and NSLocaleCalendar=NSJapaneseCalendar.
NSLocale.hReturns the logical locale for the current user.
+ (id)currentLocale
The logical locale for the current user. The locale is formed from the settings for the current user’s chosen system locale overlaid with any custom settings the user has specified in System Preferences.
This method may return a retained cached object.
Settings you get from this locale do not change as System Preferences are changed so that your operations are consistent. Typically you perform some operations on the returned object and then allow it to be disposed of. Moreover, since the returned object may be cached, you do not need to hold on to it indefinitely. Contrast with autoupdatingCurrentLocale.
NSLocale.hReturns an array of NSString objects that represents all known legal country codes.
+ (NSArray *)ISOCountryCodes
An array of NSString objects that represents all known legal country codes.
Note that many of country codes do not have any supporting locale data in Mac OS X.
NSLocale.hReturns an array of NSString objects that represents all known legal ISO currency codes.
+ (NSArray *)ISOCurrencyCodes
An array of NSString objects that represents all known legal ISO currency codes.
Note that some of the currency codes may not have any supporting locale data in Mac OS X.
NSLocale.hReturns an array of NSString objects that represents all known legal ISO language codes.
+ (NSArray *)ISOLanguageCodes
An array of NSString objects that represents all known legal ISO language codes.
Note that many of the language codes will not have any supporting locale data in Mac OS X.
NSLocale.hReturns the line direction for the specified ISO language code.
+ (NSLocaleLanguageDirection)lineDirectionForLanguage:(NSString *)isoLangCode
The ISO language code.
Returns the line direction for the language. See “NSLocaleLanguageDirection” for possible values. If the appropriate direction can’t be determined NSLocaleLanguageDirectionUnknown is returned.
NSLocale.hReturns a locale identifier from the components specified in a given dictionary.
+ (NSString *)localeIdentifierFromComponents:(NSDictionary *)dict
A dictionary containing components that specify a locale. For valid dictionary keys, see “Constants.”
A locale identifier created from the components specified in dict.
This reverses the actions of componentsFromLocaleIdentifier:, so for example the dictionary {NSLocaleLanguageCode="en", NSLocaleCountryCode="US", NSLocaleCalendar=NSJapaneseCalendar} becomes "en_US@calendar=japanese".
NSLocale.hReturns a locale identifier from a Windows locale code.
+ (NSString *)localeIdentifierFromWindowsLocaleCode:(uint32_t)lcid
The Windows locale code.
The locale identifier.
NSLocale.hReturns the user's language preference order as an array of strings.
+ (NSArray *)preferredLanguages
The user's language preference order as an array of NSString objects, each of which is a canonicalized IETF BCP 47 language identifier.
NSLocale.hReturns the “root”, canonical locale, that contains fixed “backstop” settings that provide values for otherwise undefined keys.
+ (id)systemLocale
The “root”, canonical locale, that contains fixed “backstop” settings that provide values for otherwise undefined keys.
NSLocale.hReturns a Window locale code from the locale identifier.
+ (uint32_t)windowsLocaleCodeFromLocaleIdentifier:(NSString *)localeIdentifier
The locale identifier.
The Windows locale code.
NSLocale.hReturns the display name for the given value.
- (NSString *)displayNameForKey:(id)key value:(id)value
Specifies which of the locale property keys value is (see “Constants”),
A value for key.
The display name for value.
Not all locale property keys have values with display name values.
You can use the NSLocaleIdentifier key to get the name of a locale in the language of another locale, as illustrated in the following examples. The first uses the fr_FR locale.
NSLocale *frLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"] autorelease]; |
NSString *displayNameString = [frLocale displayNameForKey:NSLocaleIdentifier value:@"fr_FR"]; |
NSLog(@"displayNameString fr_FR: %@", displayNameString); |
displayNameString = [frLocale displayNameForKey:NSLocaleIdentifier value:@"en_US"]; |
NSLog(@"displayNameString en_US: %@", displayNameString); |
returns
displayNameString fr_FR: français (France) |
displayNameString en_US: anglais (États-Unis) |
The following example uses the en_GB locale.
NSLocale *gbLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"] autorelease]; |
displayNameString = [gbLocale displayNameForKey:NSLocaleIdentifier value:@"fr_FR"]; |
NSLog(@"displayNameString fr_FR: %@", displayNameString); |
displayNameString = [gbLocale displayNameForKey:NSLocaleIdentifier value:@"en_US"]; |
NSLog(@"displayNameString en_US: %@", displayNameString); |
returns
displayNameString fr_FR: French (France) |
displayNameString en_US: English (United States) |
NSLocale.hInitializes the receiver using a given locale identifier.
- (id)initWithLocaleIdentifier:(NSString *)string
The identifier for the new locale.
The initialized locale.
NSLocale.hReturns the identifier for the receiver.
- (NSString *)localeIdentifier
The identifier for the receiver. This may not be the same string that the locale was created with, since NSLocale may canonicalize it.
Equivalent to sending objectForKey: with key NSLocaleIdentifier.
NSLocale.hReturns the object corresponding to the specified key.
- (id)objectForKey:(id)key
The key for which to return the corresponding value. For valid values of key, see “Constants.”
The object corresponding to key.
NSLocale.hThese constants describe the text direction for a language. Used by the methods lineDirectionForLanguage: and characterDirectionForLanguage:.
enum {
NSLocaleLanguageDirectionUnknown = kCFLocaleLanguageDirectionUnknown,
NSLocaleLanguageDirectionLeftToRight = kCFLocaleLanguageDirectionLeftToRight,
NSLocaleLanguageDirectionRightToLeft = kCFLocaleLanguageDirectionRightToLeft,
NSLocaleLanguageDirectionTopToBottom = kCFLocaleLanguageDirectionTopToBottom,
NSLocaleLanguageDirectionBottomToTop = kCFLocaleLanguageDirectionBottomToTop
};
typedef NSUInteger NSLocaleLanguageDirection;
NSLocaleLanguageDirectionUnknownThe direction of the language is unknown.
Available in Mac OS X v10.6 and later.
Declared in NSLocale.h.
NSLocaleLanguageDirectionLeftToRightThe language direction is from left to right.
Available in Mac OS X v10.6 and later.
Declared in NSLocale.h.
NSLocaleLanguageDirectionRightToLeftThe language direction is from right to left.
Available in Mac OS X v10.6 and later.
Declared in NSLocale.h.
NSLocaleLanguageDirectionTopToBottomThe language direction is from top to bottom.
Available in Mac OS X v10.6 and later.
Declared in NSLocale.h.
NSLocaleLanguageDirectionBottomToTopThe language direction is from bottom to top.
Available in Mac OS X v10.6 and later.
Declared in NSLocale.h.
The following constants specify keys used to retrieve components of a locale with objectForKey:.
NSString * const NSLocaleIdentifier; NSString * const NSLocaleLanguageCode; NSString * const NSLocaleCountryCode; NSString * const NSLocaleScriptCode; NSString * const NSLocaleVariantCode; NSString * const NSLocaleExemplarCharacterSet; NSString * const NSLocaleCalendar; NSString * const NSLocaleCollationIdentifier; NSString * const NSLocaleUsesMetricSystem; NSString * const NSLocaleMeasurementSystem; NSString * const NSLocaleDecimalSeparator; NSString * const NSLocaleGroupingSeparator; NSString * const NSLocaleCurrencySymbol; NSString * const NSLocaleCurrencyCode; NSString * const NSLocaleCollatorIdentifier; NSString * const NSLocaleQuotationBeginDelimiterKey; NSString * const NSLocaleQuotationEndDelimiterKey; NSString * const NSLocaleAlternateQuotationBeginDelimiterKey; NSString * const NSLocaleAlternateQuotationEndDelimiterKey;
NSLocaleIdentifierThe key for the locale identifier.
The corresponding value is an NSString object. An example value might be "es_ES_PREEURO".
Available in Mac OS X v10.4 and later.
Declared in NSLocale.h.
NSLocaleLanguageCodeThe key for the locale language code.
The corresponding value is an NSString object. An example value might be "es".
Available in Mac OS X v10.4 and later.
Declared in NSLocale.h.
NSLocaleCountryCodeThe key for the locale country code.
The corresponding value is an NSString object. An example value might be "ES".
Available in Mac OS X v10.4 and later.
Declared in NSLocale.h.
NSLocaleScriptCodeThe key for the locale script code.
The corresponding value is an NSString object.
Available in Mac OS X v10.4 and later.
Declared in NSLocale.h.
NSLocaleVariantCodeThe key for the locale variant code.
The corresponding value is an NSString object. An example value might be "PREEURO".
Available in Mac OS X v10.4 and later.
Declared in NSLocale.h.
NSLocaleExemplarCharacterSetThe key for the exemplar character set for the locale.
The corresponding value is an NSCharacterSet object.
Available in Mac OS X v10.4 and later.
Declared in NSLocale.h.
NSLocaleCalendarThe key for the calendar associated with the locale.
The corresponding value is an NSCalendar object.
Available in Mac OS X v10.4 and later.
Declared in NSLocale.h.
NSLocaleCollationIdentifierThe key for the collation associated with the locale.
The corresponding value is an NSString object.
Available in Mac OS X v10.4 and later.
Declared in NSLocale.h.
NSLocaleUsesMetricSystemThe key for the flag that indicates whether the locale uses the metric system.
The corresponding value is a Boolean NSNumber object. If the value is NO, you can typically assume American measurement units (for example, the statute mile).
Available in Mac OS X v10.4 and later.
Declared in NSLocale.h.
NSLocaleMeasurementSystemThe key for the measurement system associated with the locale.
The corresponding value is an NSString object containing a description of the measurement system used by the locale, for example “Metric” or “U.S.”.
Available in Mac OS X v10.4 and later.
Declared in NSLocale.h.
NSLocaleDecimalSeparatorThe key for the decimal separator associated with the locale.
The corresponding value is an NSString object.
Available in Mac OS X v10.4 and later.
Declared in NSLocale.h.
NSLocaleGroupingSeparatorThe key for the numeric grouping separator associated with the locale.
The corresponding value is an NSString object.
Available in Mac OS X v10.4 and later.
Declared in NSLocale.h.
NSLocaleCurrencySymbolThe key for the currency symbol associated with the locale.
The corresponding value is an NSString object.
Available in Mac OS X v10.4 and later.
Declared in NSLocale.h.
NSLocaleCurrencyCodeThe key for the currency code associated with the locale.
The corresponding value is an NSString object.
Available in Mac OS X v10.4 and later.
Declared in NSLocale.h.
NSLocaleCollatorIdentifierThe key for the collation identifier for the locale.
The corresponding value is an NSString object. If unknown, nil is returned.
Available in Mac OS X v10.6 and later.
Declared in NSLocale.h.
NSLocaleQuotationBeginDelimiterKeyThe key for the begin quotation symbol associated with the locale.
The corresponding value is an NSString object.
Available in Mac OS X v10.6 and later.
Declared in NSLocale.h.
NSLocaleQuotationEndDelimiterKeyThe key for the begin quotation symbol associated with the locale.
The corresponding value is an NSString object.
Available in Mac OS X v10.6 and later.
Declared in NSLocale.h.
NSLocaleAlternateQuotationBeginDelimiterKeyThe key for the alternating begin quotation symbol associated with the locale. In some locales, when quotations are nested, the quotation characters alternate. Thus, NSLocaleQuotationBeginDelimiterKey, then NSLocaleAlternateQuotationBeginDelimiterKey, etc.
The corresponding value is an NSString object.
Available in Mac OS X v10.6 and later.
Declared in NSLocale.h.
NSLocaleAlternateQuotationEndDelimiterKeyThe key for the alternating enda quotation symbol associated with the locale. In some locales, when quotations are nested, the quotation characters alternate. Thus, NSLocaleQuotationEndDelimiterKey, then NSLocaleAlternateQuotationEndDelimiterKey, etc.
The corresponding value is an NSString object.
Available in Mac OS X v10.6 and later.
Declared in NSLocale.h.
These constants identify NSCalendar instances.
NSString * const NSGregorianCalendar; NSString * const NSBuddhistCalendar; NSString * const NSChineseCalendar; NSString * const NSHebrewCalendar; NSString * const NSIslamicCalendar; NSString * const NSIslamicCivilCalendar; NSString * const NSJapaneseCalendar; NSString * const NSRepublicOfChinaCalendar; NSString * const NSPersianCalendar; NSString * const NSIndianCalendar; NSString * const NSISO8601Calendar;
NSGregorianCalendarIdentifier for the Gregorian calendar.
Available in Mac OS X v10.4 and later.
Declared in NSLocale.h.
NSBuddhistCalendarIdentifier for the Buddhist calendar.
Available in Mac OS X v10.4 and later.
Declared in NSLocale.h.
NSChineseCalendarIdentifier for the Chinese calendar (unsupported).
Note that the Chinese calendar is not supported in Mac OS X v10.4-10.5. Although you can create a calendar using this constant, the object will not function correctly.
Available in Mac OS X v10.4 and later.
Declared in NSLocale.h.
NSHebrewCalendarIdentifier for the Hebrew calendar.
Available in Mac OS X v10.4 and later.
Declared in NSLocale.h.
NSIslamicCalendarIdentifier for the Islamic calendar.
Available in Mac OS X v10.4 and later.
Declared in NSLocale.h.
NSIslamicCivilCalendarIdentifier for the Islamic civil calendar.
Available in Mac OS X v10.4 and later.
Declared in NSLocale.h.
NSJapaneseCalendarIdentifier for the Japanese calendar.
Available in Mac OS X v10.4 and later.
Declared in NSLocale.h.
NSRepublicOfChinaCalendarIdentifier for the Republic of China (Taiwan) calendar.
A Chinese calendar can be created, and one can do calendrical calculations with it, but it should not be used for formatting as the necessary underlying functionality is not functioning correctly yet.
Available in Mac OS X v10.6 and later.
Declared in NSLocale.h.
NSPersianCalendarIdentifier for the Persian calendar
Available in Mac OS X v10.6 and later.
Declared in NSLocale.h.
NSIndianCalendarIdentifier for the Indian calendar
Available in Mac OS X v10.6 and later.
Declared in NSLocale.h.
NSISO8601CalendarIdentifier for the ISO8601. The ISO8601 calendar is not yet implemented.
Available in Mac OS X v10.6 and later.
Declared in NSLocale.h.
You use these identifiers to initialize a new NSCalendar object, using initWithCalendarIdentifier:. You get one of these identifiers as the return value from calendarIdentifier.
NSLocale.hNotification that indicates that the user’s locale changed.
NSLocale.hLast updated: 2009-08-28