Structure
Locale
struct Locale
Overview
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 Locale.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 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.
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 Locale.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.
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.
Topics
Structures
struct CollationA type that represents the string sort order used by the locale.struct ComponentsA type that represents the components of a locale, for use when creating a locale with specific overrides.struct CurrencyA type that represents the currency system used by a locale, like dollars or euros.struct LanguageA type that represents a language, as used in a locale.struct LanguageCodeAn alphabetical code associated with a language.struct MeasurementSystemA type that represents the measurement system used by a locale, like metric or the US system.struct NumberingSystemA type that represents the numbering system used in a locale.struct RegionA type that represents a geographic region, for use in specifying a locale or language.struct ScriptThe written script used with a given language.struct SubdivisionA type that represents a subdivision of a region, such as a state in the US or a province in Canada.struct VariantA type that represents a locale’s language variant.
Operators
Initializers
init(components: Locale.Components)Creates a locale from the given components.init(identifier: String)Creates a locale with the specified identifier.init(languageCode: Locale.LanguageCode?, script: Locale.Script?, languageRegion: Locale.Region?)Creates a locale with the specified language code, script, and region identifier.init(languageComponents: Locale.Language.Components)Creates a locale from the given language components.
Instance Properties
var alternateQuotationBeginDelimiter: String?The alternate quotation begin delimiter of the locale.var alternateQuotationEndDelimiter: String?The alternate quotation end delimiter of the locale.var availableNumberingSystems: [Locale.NumberingSystem]An array containing all the valid numbering systems for the locale.var calendar: CalendarThe calendar for the locale, or the Gregorian calendar as a fallback.var collation: Locale.CollationThe string sort order of the locale.var collationIdentifier: String?The collation identifier for the locale, ornilif it has none.var collatorIdentifier: String?The collator identifier of the locale.var currency: Locale.Currency?The currency used by the locale.var currencyCode: String?The currency code of the locale.var currencySymbol: String?The currency symbol of the locale.var decimalSeparator: String?The decimal separator of the locale.var firstDayOfWeek: Locale.WeekdayThe first day of the week as represented by this locale.var groupingSeparator: String?The grouping separator of the locale.var hourCycle: Locale.HourCycleThe hour cycle used by the locale, like one-to-twelve or zero-to-twenty-three.var identifier: StringThe identifier of the locale.var language: Locale.LanguageThe language of the locale.var languageCode: String?The language code of the locale, ornilif has none.var measurementSystem: Locale.MeasurementSystemThe measurement system used by the locale, like metric or the US system.var numberingSystem: Locale.NumberingSystemThe numbering system used by the locale.var quotationBeginDelimiter: String?The quotation begin delimiter of the locale.var quotationEndDelimiter: String?The quotation end delimiter of the locale.var region: Locale.Region?The region used by the locale.var regionCode: String?The region code of the locale, ornilif it has none.var scriptCode: String?The script code of the locale, ornilif has none.var subdivision: Locale.Subdivision?The optional subdivision of the region used by this locale.var timeZone: TimeZone?The time zone associated with the locale, if any.var usesMetricSystem: BoolA Boolean that is true if the locale uses the metric system.var variant: Locale.Variant?An optional variant used by the locale.var variantCode: String?The variant code for the locale, ornilif it has none.
Instance Methods
func hash(into: inout Hasher)func identifier(Locale.IdentifierType) -> StringReturns the locale identifier, in the specified standard format.func localizedString(for: Calendar.Identifier) -> String?Returns a localized string for a specified calendar.func localizedString(forCollationIdentifier: String) -> String?Returns a localized string for a specified ICU collation identifier.func localizedString(forCollatorIdentifier: String) -> String?Returns a localized string for a specified ICU collator identifier.func localizedString(forCurrencyCode: String) -> String?Returns a localized string for a specified ISO 4217 currency code.func localizedString(forIdentifier: String) -> String?Returns a localized string for a specified locale identifier.func localizedString(forLanguageCode: String) -> String?Returns a localized string for a specified language code.func localizedString(forRegionCode: String) -> String?Returns a localized string for a specified region code.func localizedString(forScriptCode: String) -> String?Returns a localized string for a specified script code.func localizedString(forVariantCode: String) -> String?Returns a localized string for a specified variant code.
Type Properties
static var autoupdatingCurrent: LocaleA locale which tracks the user’s current preferences.static var availableIdentifiers: [String]A list of available identifiers.static var commonISOCurrencyCodes: [String]A list of common currency codes.static var current: LocaleA locale representing the user’s region settings at the time the property is read.static var preferredLanguages: [String]A list of the user’s preferred languages.static var preferredLocales: [Locale]Returns a list of the user’s preferred locales, as specified in Language & Region settings, taking into account any per-app language overrides.
Type Methods
static func canonicalIdentifier(from: String) -> StringReturns a canonical identifier from the given string.static func canonicalLanguageIdentifier(from: String) -> StringReturns a canonical language identifier from the given string.static func identifier(Locale.IdentifierType, from: String) -> StringReturns the identifier conforming to the specified standard for the specified string.static func identifier(fromComponents: [String : String]) -> StringConstructs an identifier from a dictionary of components.static func identifier(fromWindowsLocaleCode: Int) -> String?Returns the locale identifier from a given Windows locale code, ornilif it could not be converted.static func windowsLocaleCode(fromIdentifier: String) -> Int?Returns the Windows locale code from a given identifier, ornilif it could not be converted.
Enumerations
enum HourCycleA type that represents the hour cycle used in a locale, like one-to-twelve or zero-to-twenty-three.enum IdentifierTypeA type that indicates the standard that defines a locale’s identifier.enum LanguageDirectionenum WeekdayA type that represents weekdays, used for indicating a locale’s first day of the week.
Default Implementations
Relationships
Conforms To
Swift.CopyableSwift.CustomDebugStringConvertibleSwift.CustomReflectableSwift.CustomStringConvertibleSwift.DecodableSwift.EncodableSwift.EquatableSwift.EscapableSwift.HashableSwift.SendableSwift.SendableMetatype