Contents

Locale

Information about linguistic, cultural, and technological conventions for use in formatting data for presentation.

Declaration

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 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

Creating a locale by identifier

Creating a locale by components

Getting the user’s locale

Getting known identifiers and codes

Converting between identifiers

Getting locale components

Getting language components

Getting date and time components

Getting measurement and counting components

Getting region components

Getting ordering components

Getting information about a locale

Getting display information about a locale

Getting the user’s preferred languages

Getting line and character direction for a language

Working with notification messages

Using reference types

Structures

Type Properties