NSLocale Class Reference
| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/Foundation.framework |
| Availability | Available in iOS 2.0 and later. |
| Declared in | NSLocale.h |
| Companion guides | |
Overview
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 Guide). 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 “Toll-Free Bridging” for more information on toll-free bridging.
Tasks
Getting and Initializing Locales
Getting Information About a Locale
Getting System Locale Information
-
+ availableLocaleIdentifiers -
+ ISOCountryCodes -
+ ISOCurrencyCodes -
+ ISOLanguageCodes -
+ commonISOCurrencyCodes
Converting Between Identifiers
-
+ canonicalLocaleIdentifierFromString: -
+ componentsFromLocaleIdentifier: -
+ localeIdentifierFromComponents: -
+ canonicalLanguageIdentifierFromString: -
+ localeIdentifierFromWindowsLocaleCode: -
+ windowsLocaleCodeFromLocaleIdentifier:
Getting Preferred Languages
Getting Line and Character Direction For a Language
Class Methods
autoupdatingCurrentLocale
Returns the current logical locale for the current user.
Return Value
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.
Discussion
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 to learn how to register for and receive notifications).
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSLocale.havailableLocaleIdentifiers
Returns an array of NSString objects, each of which identifies a locale available on the system.
Return Value
An array of NSString objects, each of which identifies a locale available on the system.
Availability
- Available in iOS 2.0 and later.
Declared In
NSLocale.hcanonicalLanguageIdentifierFromString:
Returns a canonical language identifier by mapping an arbitrary locale identification string to the canonical identifier.
Parameters
- string
A string representation of an arbitrary locale identifier.
Return Value
A string that represents the canonical language identifier for the specified arbitrary locale identifier.
Availability
- Available in iOS 4.0 and later.
Declared In
NSLocale.hcanonicalLocaleIdentifierFromString:
Returns the canonical identifier for a given locale identification string.
Parameters
- string
A locale identification string.
Return Value
The canonical identifier for an the locale identified by string.
Availability
- Available in iOS 2.0 and later.
Declared In
NSLocale.hcharacterDirectionForLanguage:
Returns the character direction for the specified ISO language code.
Parameters
- isoLangCode
The ISO language code.
Return Value
Returns the character direction for the language. See “NSLocaleLanguageDirection” for possible values. If the appropriate direction can’t be determined NSLocaleLanguageDirectionUnknown is returned.
Availability
- Available in iOS 4.0 and later.
See Also
Declared In
NSLocale.hcommonISOCurrencyCodes
Returns an array of common ISO currency codes
Return Value
An array of NSString objects that represents common ISO currency codes.
Discussion
Common codes may include, for example, AED, AUD, BZD, DKK, EUR, GBP, JPY, KES, MXN, OMR, STD, USD, XCD, and ZWD.
Availability
- Available in iOS 2.0 and later.
Declared In
NSLocale.hcomponentsFromLocaleIdentifier:
Returns a dictionary that is the result of parsing a locale ID.
Parameters
- string
A locale ID, consisting of language, script, country, variant, and keyword/value pairs, for example,
"en_US@calendar=japanese".
Return Value
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.”
Discussion
For example: the locale ID "en_US@calendar=japanese" yields a dictionary with three entries: NSLocaleLanguageCode=en, NSLocaleCountryCode=US, and NSLocaleCalendar=NSJapaneseCalendar.
Availability
- Available in iOS 2.0 and later.
Declared In
NSLocale.hcurrentLocale
Returns the logical locale for the current user.
Return Value
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.
Discussion
Settings you get from this locale do not change as a user’s 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.
Availability
- Available in iOS 2.0 and later.
Declared In
NSLocale.hISOCountryCodes
Returns an array of NSString objects that represents all known legal country codes.
Return Value
An array of NSString objects that represents all known legal country codes.
Discussion
Note that many of country codes do not have any supporting locale data in OS X.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSLocale.hISOCurrencyCodes
Returns an array of NSString objects that represents all known legal ISO currency codes.
Return Value
An array of NSString objects that represents all known legal ISO currency codes.
Discussion
Note that some of the currency codes may not have any supporting locale data in OS X.
Availability
- Available in iOS 2.0 and later.
Declared In
NSLocale.hISOLanguageCodes
Returns an array of NSString objects that represents all known legal ISO language codes.
Return Value
An array of NSString objects that represents all known legal ISO language codes.
Discussion
Note that many of the language codes do not have any supporting locale data in OS X.
Availability
- Available in iOS 2.0 and later.
Declared In
NSLocale.hlineDirectionForLanguage:
Returns the line direction for the specified ISO language code.
Parameters
- isoLangCode
The ISO language code.
Return Value
Returns the line direction for the language. See “NSLocaleLanguageDirection” for possible values. If the appropriate direction can’t be determined NSLocaleLanguageDirectionUnknown is returned.
Availability
- Available in iOS 4.0 and later.
See Also
Declared In
NSLocale.hlocaleIdentifierFromComponents:
Returns a locale identifier from the components specified in a given dictionary.
Parameters
- dict
A dictionary containing components that specify a locale. For valid dictionary keys, see “Constants.”
Return Value
A locale identifier created from the components specified in dict.
Discussion
This reverses the actions of componentsFromLocaleIdentifier:, so for example the dictionary {NSLocaleLanguageCode="en", NSLocaleCountryCode="US", NSLocaleCalendar=NSJapaneseCalendar} becomes "en_US@calendar=japanese".
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSLocale.hlocaleIdentifierFromWindowsLocaleCode:
Returns a locale identifier from a Windows locale code.
Parameters
- lcid
The Windows locale code.
Return Value
The locale identifier.
Availability
- Available in iOS 4.0 and later.
Declared In
NSLocale.hpreferredLanguages
Returns the user's language preference order as an array of strings.
Return Value
The user's language preference order as an array of NSString objects, each of which is a canonicalized IETF BCP 47 language identifier.
Availability
- Available in iOS 2.0 and later.
Declared In
NSLocale.hsystemLocale
Returns the “root”, canonical locale, that contains fixed “backstop” settings that provide values for otherwise undefined keys.
Return Value
The “root”, canonical locale, that contains fixed “backstop” settings that provide values for otherwise undefined keys.
Availability
- Available in iOS 2.0 and later.
Declared In
NSLocale.hwindowsLocaleCodeFromLocaleIdentifier:
Returns a Window locale code from the locale identifier.
Parameters
- localeIdentifier
The locale identifier.
Return Value
The Windows locale code.
Availability
- Available in iOS 4.0 and later.
Declared In
NSLocale.hInstance Methods
displayNameForKey:value:
Returns the display name for the given value.
Parameters
- key
Specifies which of the locale property keys value is (see “Constants”),
- value
A value for key.
Return Value
The display name for value.
Discussion
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"]; |
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"]; |
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) |
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSLocale.hinitWithLocaleIdentifier:
Initializes the receiver using a given locale identifier.
Parameters
- string
The identifier for the new locale.
Return Value
The initialized locale.
Availability
- Available in iOS 2.0 and later.
Declared In
NSLocale.hlocaleIdentifier
Returns the identifier for the receiver.
Return Value
The identifier for the receiver. This may not be the same string that the locale was created with, since NSLocale may canonicalize it.
Discussion
Equivalent to sending objectForKey: with key NSLocaleIdentifier.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSLocale.hobjectForKey:
Returns the object corresponding to the specified key.
Parameters
- key
The key for which to return the corresponding value. For valid values of key, see “Constants.”
Return Value
The object corresponding to key.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
NSLocale.hConstants
NSLocaleLanguageDirection
These 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;
Constants
NSLocaleLanguageDirectionUnknownThe direction of the language is unknown.
Available in iOS 4.0 and later.
Declared in
NSLocale.h.NSLocaleLanguageDirectionLeftToRightThe language direction is from left to right.
Available in iOS 4.0 and later.
Declared in
NSLocale.h.NSLocaleLanguageDirectionRightToLeftThe language direction is from right to left.
Available in iOS 4.0 and later.
Declared in
NSLocale.h.NSLocaleLanguageDirectionTopToBottomThe language direction is from top to bottom.
Available in iOS 4.0 and later.
Declared in
NSLocale.h.NSLocaleLanguageDirectionBottomToTopThe language direction is from bottom to top.
Available in iOS 4.0 and later.
Declared in
NSLocale.h.
NSLocale Component Keys
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;
Constants
NSLocaleIdentifierThe key for the locale identifier.
The corresponding value is an
NSStringobject. An example value might be"es_ES_PREEURO".Available in iOS 2.0 and later.
Declared in
NSLocale.h.NSLocaleLanguageCodeThe key for the locale language code.
The corresponding value is an
NSStringobject. An example value might be"es".Available in iOS 2.0 and later.
Declared in
NSLocale.h.NSLocaleCountryCodeThe key for the locale country code.
The corresponding value is an
NSStringobject. An example value might be"ES".Available in iOS 2.0 and later.
Declared in
NSLocale.h.NSLocaleScriptCodeThe key for the locale script code.
The corresponding value is an
NSStringobject.Available in iOS 2.0 and later.
Declared in
NSLocale.h.NSLocaleVariantCodeThe key for the locale variant code.
The corresponding value is an
NSStringobject. An example value might be"PREEURO".Available in iOS 2.0 and later.
Declared in
NSLocale.h.NSLocaleExemplarCharacterSetThe key for the exemplar character set for the locale.
The corresponding value is an
NSCharacterSetobject.Available in iOS 2.0 and later.
Declared in
NSLocale.h.NSLocaleCalendarThe key for the calendar associated with the locale.
The corresponding value is an
NSCalendarobject.Available in iOS 2.0 and later.
Declared in
NSLocale.h.NSLocaleCollationIdentifierThe key for the collation associated with the locale.
The corresponding value is an
NSStringobject.Available in iOS 2.0 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
NSNumberobject. If the value isNO, you can typically assume American measurement units (for example, the statute mile).Available in iOS 2.0 and later.
Declared in
NSLocale.h.NSLocaleMeasurementSystemThe key for the measurement system associated with the locale.
The corresponding value is an
NSStringobject containing a description of the measurement system used by the locale, for example “Metric” or “U.S.”.Available in iOS 2.0 and later.
Declared in
NSLocale.h.NSLocaleDecimalSeparatorThe key for the decimal separator associated with the locale.
The corresponding value is an
NSStringobject.Available in iOS 2.0 and later.
Declared in
NSLocale.h.NSLocaleGroupingSeparatorThe key for the numeric grouping separator associated with the locale.
The corresponding value is an
NSStringobject.Available in iOS 2.0 and later.
Declared in
NSLocale.h.NSLocaleCurrencySymbolThe key for the currency symbol associated with the locale.
The corresponding value is an
NSStringobject.Available in iOS 2.0 and later.
Declared in
NSLocale.h.NSLocaleCurrencyCodeThe key for the currency code associated with the locale.
The corresponding value is an
NSStringobject.Available in iOS 2.0 and later.
Declared in
NSLocale.h.NSLocaleCollatorIdentifierThe key for the collation identifier for the locale.
The corresponding value is an
NSStringobject. If unknown,nilis returned.Available in iOS 4.0 and later.
Declared in
NSLocale.h.NSLocaleQuotationBeginDelimiterKeyThe key for the begin quotation symbol associated with the locale.
The corresponding value is an
NSStringobject.Available in iOS 4.0 and later.
Declared in
NSLocale.h.NSLocaleQuotationEndDelimiterKeyThe key for the end quotation symbol associated with the locale.
The corresponding value is an
NSStringobject.Available in iOS 4.0 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, thenNSLocaleAlternateQuotationBeginDelimiterKey, etc.The corresponding value is an
NSStringobject.Available in iOS 4.0 and later.
Declared in
NSLocale.h.NSLocaleAlternateQuotationEndDelimiterKeyThe key for the alternate end quotation symbol associated with the locale. In some locales, when quotations are nested, the quotation characters alternate. Thus,
NSLocaleQuotationEndDelimiterKey, thenNSLocaleAlternateQuotationEndDelimiterKey, etc.The corresponding value is an
NSStringobject.Available in iOS 4.0 and later.
Declared in
NSLocale.h.
NSLocale Calendar Keys
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;
Constants
NSGregorianCalendarIdentifier for the Gregorian calendar.
Available in iOS 2.0 and later.
Declared in
NSLocale.h.NSBuddhistCalendarIdentifier for the Buddhist calendar.
Available in iOS 2.0 and later.
Declared in
NSLocale.h.NSChineseCalendarIdentifier for the Chinese calendar.
Available in iOS 2.0 and later.
Declared in
NSLocale.h.NSHebrewCalendarIdentifier for the Hebrew calendar.
Available in iOS 2.0 and later.
Declared in
NSLocale.h.NSIslamicCalendarIdentifier for the Islamic calendar.
Available in iOS 2.0 and later.
Declared in
NSLocale.h.NSIslamicCivilCalendarIdentifier for the Islamic civil calendar.
Available in iOS 2.0 and later.
Declared in
NSLocale.h.NSJapaneseCalendarIdentifier for the Japanese calendar.
Available in iOS 2.0 and later.
Declared in
NSLocale.h.NSRepublicOfChinaCalendarIdentifier for the Republic of China (Taiwan) calendar.
A Chinese calendar can be created, and you can do calendrical calculations with it, but you should not use it for formatting as the necessary underlying functionality is not working correctly yet.
Available in iOS 4.0 and later.
Declared in
NSLocale.h.NSPersianCalendarIdentifier for the Persian calendar
Available in iOS 4.0 and later.
Declared in
NSLocale.h.NSIndianCalendarIdentifier for the Indian calendar
Available in iOS 4.0 and later.
Declared in
NSLocale.h.NSISO8601CalendarIdentifier for the ISO8601. The ISO8601 calendar is not yet implemented.
Available in iOS 4.0 and later.
Declared in
NSLocale.h.
Discussion
You use these identifiers to initialize a new NSCalendar object, using initWithCalendarIdentifier:. The calendarIdentifier method returns one of these values.
Notifications
NSCurrentLocaleDidChangeNotification
Availability
- Available in iOS 2.0 and later.
Declared In
NSLocale.h© 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-08-28)