Information about linguistic, cultural, and technological conventions for use in formatting data for presentation.
`Locale` encapsulates 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 formatting conventions for dates and times.
Apps use locales to provide, format, and interpret information about and according to the user's customs and preferences. Data formatting APIs commonly make use of locales to present data in a locale-appropriate way.
You can create a `Locale` from a common identifier like `en-US`, or by specifying its components. More commonly, you access the current system locale with the ``current`` or ``autoupdatingCurrent`` static variables.
### Working with locale components
A `Locale` exposes its various traits — the appropriate measurement system, currency symbols, date and time conventions, and more — as strongly-typed properties like ``currency``, `numberingSystem`, and `firstDayOfWeek`.
In addition, the ``language`` property allows you examine traits of languages, through the ``Language`` type, in contast with `NSLocale`, where `NSLocale.languageCode` is just a string identifier. You can use a locale's language to compare whether two locales use the same language, or if one language is a parent of another.
The following example creates a `Locale` from the identifier `zh-CN`, for Chinese. It then accesses this locale's ``language`` to get the language's ``Language/script``, and uses a US English locale to get a localized string describing the script: "Simplified Han". With the locale `zh-Hant-CN`, for Traditional Chinese, the script would be "Traditional Han" instead.
```swift
let zhCN = Locale(identifier: "zh-CN")
if let script = zhCN.language.script {
let enUS = Locale(identifier: "en-US")
let localizedScript = enUS.localizedString(forScript: script) // "Simplified Han"
}
```
### Creating custom locales from components
You can create a custom locale by creating a `Locale` instance from a customized ``Components``. Do this when you want to tweak specific aspects of a locale. The following example creates a locale that uses language conventions of British English (language region `GB`), but otherwise uses US conventions for things like currency and measurement.
```swift
var components = Locale.Components(languageCode: "en", languageRegion: "GB")
components.region = Locale.Region("US")
let en_GB_US = Locale(components: components)
```
Creating a custom locale like this isn't necessarily common in apps, but can be useful in unit testing your app's localizations.
Creates a locale from the given components.
Use this initializer to create a locale with a unique combination of components, beyond the defaults provided by a language and country code.
For example, you can create a ``Locale/Components`` instance that uses UK language conventions, but US regional conventions for traits like currency and measurement. You then use the components to create a new `Locale` instance, like this:
```swift
var components = Locale.Components(languageCode: "en", languageRegion: "GB")
components.region = Locale.Region("US")
let en_GB_US = Locale(components: components)
```
- Parameter components: A ``Locale/Components`` instance that provides the components to create a customized locale.
Constructs an identifier from a dictionary of components.
init · iOS 8.0+
init
TypeScript
static initIdentifier(identifier: string): Locale
Swift declaration
init(identifier: String)
Creates a locale with the specified identifier.
- Parameter identifier: A BCP-47 language identifier such as `en_US` or `en-u-nu-thai-ca-buddhist`, or an ICU-style identifier such as `en@calendar=buddhist;numbers=thai`.
static func identifier(_ type: Locale.IdentifierType, from string: String) -> String
Returns the identifier conforming to the specified standard for the specified string.
- Parameters:
- type: The identifier type used by `string`, such as ``Locale/IdentifierType/icu`` or ``Locale/IdentifierType/bcp47``.
- string: An identifier string that complies with the standard indicated by `type`.
- Returns: A locale identifier.
Returns the locale identifier, in the specified standard format.
- Parameter type: The standard locale identifier format to use for the returned string.
- Returns: The locale identifier, formatted in accordance with the specified identifier type.
get · iOS 8.0+ · Deprecated in iOS 16.0
regionCode
TypeScript
get regionCode(): (string) | null
Swift declaration
var regionCode: String? { get }
The region code of the locale, or `nil` if it has none.
For example, for the locale "zh-Hant-HK", returns "HK".
get · iOS 8.0+ · Deprecated in iOS 16.0
scriptCode
TypeScript
get scriptCode(): (string) | null
Swift declaration
var scriptCode: String? { get }
The script code of the locale, or `nil` if has none.
For example, for the locale "zh-Hant-HK", returns "Hant".
get · iOS 8.0+
description
TypeScript
get description(): string
Swift declaration
var description: String { get }
A textual representation of this instance.
Calling this property directly is discouraged. Instead, convert an
instance of any type to a string by using the `String(describing:)`
initializer. This initializer works with any type, and uses the custom
`description` property for types that conform to
`CustomStringConvertible`:
struct Point: CustomStringConvertible {
let x: Int, y: Int
var description: String {
return "(\(x), \(y))"
}
}
let p = Point(x: 21, y: 30)
let s = String(describing: p)
print(s)
// Prints "(21, 30)"
The conversion of `p` to a string in the assignment to `s` uses the
`Point` type's `description` property.
get · iOS 16.0+
subdivision
TypeScript
get subdivision(): (Locale_Subdivision) | null
Swift declaration
var subdivision: Locale.Subdivision? { get }
The optional subdivision of the region used by this locale.
This property corresponds to the `sd` key of the Unicode BCP 47 extension.
get · iOS 8.0+ · Deprecated in iOS 16.0
variantCode
TypeScript
get variantCode(): (string) | null
Swift declaration
var variantCode: String? { get }
The variant code for the locale, or `nil` if it has none.
For example, for the locale "en_POSIX", returns "POSIX".
get · iOS 8.0+ · Deprecated in iOS 16.0
currencyCode
TypeScript
get currencyCode(): (string) | null
Swift declaration
var currencyCode: String? { get }
The currency code of the locale.
For example, for "zh-Hant-HK", returns "HKD".
get · iOS 8.0+
customMirror
TypeScript
get customMirror(): SDKHandle
Swift declaration
var customMirror: Mirror { get }
The custom mirror for this instance.
If this type has value semantics, the mirror should be unaffected by
subsequent mutations of the instance.
Creates a locale with the specified language code, script, and region identifier.
- Parameters:
- languageCode: A language code, typically created from a two- or three-letter language code specified by ISO 639.
- script: The script to use for the new locale components instance.
- languageRegion: A language region, typically created from a two-letter BCP 47 region subtag like `US`.
get · iOS 8.0+ · Deprecated in iOS 16.0
languageCode
TypeScript
get languageCode(): (string) | null
Swift declaration
var languageCode: String? { get }
The language code of the locale, or `nil` if has none.
For example, for the locale "zh-Hant-HK", returns "zh".
Returns the line direction for a specified language code.
get · iOS 8.0+
currencySymbol
TypeScript
get currencySymbol(): (string) | null
Swift declaration
var currencySymbol: String? { get }
The currency symbol of the locale.
For example, for "zh-Hant-HK", returns "HK$".
get · iOS 16.0+
firstDayOfWeek
TypeScript
get firstDayOfWeek(): Locale_Weekday
Swift declaration
var firstDayOfWeek: Locale.Weekday { get }
The first day of the week as represented by this locale.
This value is the preferred first day of the week to show in a calendar view. It isn't necessarily the same as the first day after the weekend; don't try to determine a first-day-of-week value from weekend information.
This property corresponds to the `fw` key of the Unicode BCP 47 extension.
For locale instances created with the `fw` specifier (such as `en-US@fw=mon`), or with a custom ``Locale/Components``, this property represents the custom day. Otherwise, it represents the locale's default first day of the week.
Returns a localized string for a specified ISO 4217 currency code.
For example, in the "en" locale, the result for `"USD"` is `"US Dollar"`.
- SeeAlso: `Locale.isoCurrencyCodes`
Returns a localized string for a specified ICU collation identifier.
For example, in the "en" locale, the result for `"phonebook"` is `"Phonebook Sort Order"`.
Returns a localized string for a specified calendar.
For example, in the "en" locale, the result for `.buddhist` is `"Buddhist Calendar"`.
get · iOS 16.0+
numberingSystem
TypeScript
get numberingSystem(): Locale_NumberingSystem
Swift declaration
var numberingSystem: Locale.NumberingSystem { get }
The numbering system used by the locale.
This property corresponds to the `nu` key of the Unicode BCP 47 extension.
For locale instances created with the `nu` specifier (such as `en-US@nu=jpanfin`), or with a custom ``Locale/Components``, this property represents the custom numbering system. Otherwise, it represents the locale's default numbering system.
get · iOS 8.0+
debugDescription
TypeScript
get debugDescription(): string
Swift declaration
var debugDescription: String { get }
A textual representation of this instance, suitable for debugging.
Calling this property directly is discouraged. Instead, convert an
instance of any type to a string by using the `String(reflecting:)`
initializer. This initializer works with any type, and uses the custom
`debugDescription` property for types that conform to
`CustomDebugStringConvertible`:
struct Point: CustomDebugStringConvertible {
let x: Int, y: Int
var debugDescription: String {
return "(\(x), \(y))"
}
}
let p = Point(x: 21, y: 30)
let s = String(reflecting: p)
print(s)
// Prints "(21, 30)"
The conversion of `p` to a string in the assignment to `s` uses the
`Point` type's `debugDescription` property.
get · iOS 8.0+
decimalSeparator
TypeScript
get decimalSeparator(): (string) | null
Swift declaration
var decimalSeparator: String? { get }
The decimal separator of the locale.
For example, for "en_US", returns ".".
get · iOS 8.0+ · Deprecated in iOS 16.0
isoCurrencyCodes
TypeScript
static get isoCurrencyCodes(): Array<string>
Swift declaration
static var isoCurrencyCodes: [String] { get }
A list of available currency codes.
get · iOS 8.0+ · Deprecated in iOS 16.0
isoLanguageCodes
TypeScript
static get isoLanguageCodes(): Array<string>
Swift declaration
static var isoLanguageCodes: [String] { get }
A list of available language codes.
get · iOS 26.0+
preferredLocales
TypeScript
static get preferredLocales(): Array<Locale>
Swift declaration
static var preferredLocales: [Locale] { get }
Returns a list of the user’s preferred locales, as specified in Language & Region settings, taking into account any per-app language overrides.
get · iOS 8.0+ · Deprecated in iOS 16.0
usesMetricSystem
TypeScript
get usesMetricSystem(): boolean
Swift declaration
var usesMetricSystem: Bool { get }
A Boolean that is true if the locale uses the metric system.
- SeeAlso: `MeasurementFormatter`
get · iOS 8.0+
groupingSeparator
TypeScript
get groupingSeparator(): (string) | null
Swift declaration
var groupingSeparator: String? { get }
The grouping separator of the locale.
For example, for "en_US", returns ",".
get · iOS 16.0+
measurementSystem
TypeScript
get measurementSystem(): Locale_MeasurementSystem
Swift declaration
var measurementSystem: Locale.MeasurementSystem { get }
The measurement system used by the locale, like metric or the US system.
When called on the special `Locale` instances ``current`` or ``autoupdatingCurrent``, if the user overrode the default measurement system, this property provides the user's preference.
This property corresponds to the `ms` key of the Unicode BCP 47 extension.
For locale instances created with the `ms` specifier (such as `en-US@ms=metric`), or with a custom ``Locale/Components``, this property represents the custom measurement system. Otherwise, it represents the locale's default measurement system.
Creates a locale from the given language components.
- Parameter languageComponents: A ``Locale/Language/Components`` instance that provides language components that identify a locale.
get · iOS 8.0+
preferredLanguages
TypeScript
static get preferredLanguages(): Array<string>
Swift declaration
static var preferredLanguages: [String] { get }
A list of the user's preferred languages.
Returns a list of the user's preferred languages, as specified in Language & Region settings, taking into account any per-app language overrides.
- Note: `Bundle` is responsible for determining the language that your application will run in, based on the result of this API and combined with the languages your application supports.
- SeeAlso: `Bundle.preferredLocalizations(from:)`
- SeeAlso: `Bundle.preferredLocalizations(from:forPreferences:)`
- SeeAlso: `Locale.preferredLocales`
get · iOS 8.0+
autoupdatingCurrent
TypeScript
static get autoupdatingCurrent(): Locale
Swift declaration
static var autoupdatingCurrent: Locale { get }
A locale which tracks the user's current preferences.
This value represents the locale currently used by the app, based on the following:
* The current system locale.
* Any app-specific locale choice made in the Settings app.
* The availability of the preferred locale in the app. For example, if the person using an app has set their device to use a Spanish-language locale, but the app only supports English, this value returns an English locale.
Use this property when you want a locale that always reflects the latest configuration settings. When the person using the app changes settings, reading properties from a locale instance obtained from this property provides the latest values. If you need to rely on a locale that does not change, use the locale given by the ``Locale/current`` property instead.
Although the locale obtained here automatically follows the latest language and region settings, it provides no indication when the settings change. To receive notification of locale changes in Swift, add an observer for ``Locale/CurrentLocaleDidChangeMessage``. In Objective-C, you can add your object as an observer of `NSCurrentLocaleDidChangeNotification`.
If mutated, this `Locale` no longer tracks the user's preferences.
- Note: The autoupdating `Locale` only compares as equal to another autoupdating `Locale`.
Returns a canonical identifier from the given string.
get · iOS 8.0+ · Deprecated in iOS 16.0
collationIdentifier
TypeScript
get collationIdentifier(): (string) | null
Swift declaration
var collationIdentifier: String? { get }
The collation identifier for the locale, or `nil` if it has none.
For example, for the locale "en_US@collation=phonebook", returns "phonebook".
get · iOS 8.0+
availableIdentifiers
TypeScript
static get availableIdentifiers(): Array<string>
Swift declaration
static var availableIdentifiers: [String] { get }
A list of available identifiers.
get · iOS 8.0+
exemplarCharacterSet
TypeScript
get exemplarCharacterSet(): (CharacterSet) | null
Swift declaration
var exemplarCharacterSet: CharacterSet? { get }
The exemplar character set for the locale, or `nil` if has none.
get · iOS 8.0+
quotationEndDelimiter
TypeScript
get quotationEndDelimiter(): (string) | null
Swift declaration
var quotationEndDelimiter: String? { get }
The quotation end delimiter of the locale.
For example, returns `\u{201D}` for “en_US”, and `\u{300D}` for “zh-Hant-HK”.
get · iOS 16.0+
commonISOCurrencyCodes
TypeScript
static get commonISOCurrencyCodes(): Array<string>
Swift declaration
static var commonISOCurrencyCodes: [String] { get }
A list of common currency codes.
get · iOS 8.0+
quotationBeginDelimiter
TypeScript
get quotationBeginDelimiter(): (string) | null
Swift declaration
var quotationBeginDelimiter: String? { get }
The quotation begin delimiter of the locale.
For example, returns `\u{201C}` for “en_US”, and `\u{300C}` for “zh-Hant-HK”.
get · iOS 16.0+
availableNumberingSystems
TypeScript
get availableNumberingSystems(): Array<Locale_NumberingSystem>
Swift declaration
var availableNumberingSystems: [Locale.NumberingSystem] { get }
An array containing all the valid numbering systems for the locale.
The following snippet creates a locale for Arabic as used in United Arab Emirates. For this locale, there are two numbering systems available: `latn` (Latin digits) and `arab` (Arabic-Indic digits).
```swift
let uae = Locale(identifier: "ar-AE") // Arabic / U.A.E.
let numberingSystems = uae.availableNumberingSystems
print("\(numberingSystems.map{$0.identifier})") // ["latn","arab"]
```
Returns a canonical language identifier from the given string.
get · iOS 8.0+
alternateQuotationEndDelimiter
TypeScript
get alternateQuotationEndDelimiter(): (string) | null
Swift declaration
var alternateQuotationEndDelimiter: String? { get }
The alternate quotation end delimiter of the locale.
For example, returns `\u{2019}` for "en_US", and `\u{300F}` for "zh-Hant-HK".
get · iOS 8.0+
alternateQuotationBeginDelimiter
TypeScript
get alternateQuotationBeginDelimiter(): (string) | null
Swift declaration
var alternateQuotationBeginDelimiter: String? { get }
The alternate quotation begin delimiter of the locale.
For example, returns `\u{2018}` for "en_US", and `\u{300E}` for "zh-Hant-HK".
get · iOS 16.0+
region
TypeScript
get region(): (Locale_Region) | null
Swift declaration
var region: Locale.Region? { get }
The region used by the locale.
This property corresponds to the `rg` key of the Unicode BCP 47 extension.
For locale instances created with the `rg` specifier (such as `en-GB@rg=US`), or with a custom ``Locale/Components``, this property represents the custom region. Otherwise, it represents the language's region.
get · iOS 8.0+
current
TypeScript
static get current(): Locale
Swift declaration
static var current: Locale { get }
A locale representing the user's region settings at the time the property is read.
This value represents the locale currently used by the app, based on the following:
* The current system locale.
* Any app-specific locale choice made in the Settings app.
* The availability of the preferred locale in the app. For example, if the person using an app has set their device to use a Spanish-language locale, but the app only supports English, this value returns an English locale.
Use this property when you need to rely on a consistent locale. A locale instance obtained this way does not change even when the person using the device changes language or region settings. If you want a locale instance that always reflects the current configuration, use the one provided by the ``Locale/autoupdatingCurrent`` property instead.
To receive notification of locale changes in Swift, add an observer for ``Locale/CurrentLocaleDidChangeMessage``. In Objective-C, you can add your object as an observer of ``NSLocale/currentLocaleDidChangeNotification``.
get · iOS 16.0+
variant
TypeScript
get variant(): (Locale_Variant) | null
Swift declaration
var variant: Locale.Variant? { get }
An optional variant used by the locale.
This property corresponds to the `va` key of the Unicode BCP 47 extension.
For locale instances created with the `va` specifier (such as `en-US@va=posix`), or with a custom ``Locale/Components``, this property represents the custom variant. Otherwise, it represents the locale's default variant.
get · iOS 8.0+
calendar
TypeScript
get calendar(): Calendar
Swift declaration
var calendar: Calendar { get }
The calendar for the locale, or the Gregorian calendar as a fallback.
get · iOS 16.0+
currency
TypeScript
get currency(): (Locale_Currency) | null
Swift declaration
var currency: Locale.Currency? { get }
The currency used by the locale.
This property corresponds to the `cu` key of the Unicode BCP 47 extension.
For locale instances created with the `cu` specifier (such as `en-US@cu=cad`), or with a custom ``Locale/Components``, this property represents the custom currency. Otherwise, it represents the locale's default currency.
get · iOS 16.0+
language
TypeScript
get language(): Locale_Language
Swift declaration
var language: Locale.Language { get }
The language of the locale.
get · iOS 16.0+
timeZone
TypeScript
get timeZone(): (TimeZone) | null
Swift declaration
var timeZone: TimeZone? { get }
The time zone associated with the locale, if any.
get · iOS 16.0+
collation
TypeScript
get collation(): Locale_Collation
Swift declaration
var collation: Locale.Collation { get }
The string sort order of the locale.
This property corresponds to the `co` key of the Unicode BCP 47 extension.
For locale instances created with the `co` specifier (such as `en-US@co=phonetic`), or with a custom ``Locale/Components``, this property represents the custom collation. Otherwise, it represents the locale's default sort order.
get · iOS 16.0+
hourCycle
TypeScript
get hourCycle(): Locale_HourCycle
Swift declaration
var hourCycle: Locale.HourCycle { get }
The hour cycle used by the locale, like one-to-twelve or zero-to-twenty-three.
When called on the special `Locale` instances ``current`` or ``autoupdatingCurrent``, if the user overrode the default hour cycle, this property provides the user's preference.
This property corresponds to the `hc` key of the Unicode BCP 47 extension.
For locale instances created with the `hc` specifier (such as `en-US@hc=h23`), or with a custom ``Locale/Components``, this property represents the custom hour cycle. Otherwise, it represents the locale's default hour cycle.