Skip to content

Structure

Locale

iOS 8.0+ iPadOS 8.0+ Mac Catalyst 8.0+ macOS 10.10+ tvOS 9.0+ watchOS 2.0+
Information about linguistic, cultural, and technological conventions for use in formatting data for presentation.
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 Collation
    A type that represents the string sort order used by the locale.
  • struct Components
    A type that represents the components of a locale, for use when creating a locale with specific overrides.
  • struct Currency
    A type that represents the currency system used by a locale, like dollars or euros.
  • struct Language
    A type that represents a language, as used in a locale.
  • struct LanguageCode
    An alphabetical code associated with a language.
  • struct MeasurementSystem
    A type that represents the measurement system used by a locale, like metric or the US system.
  • struct NumberingSystem
    A type that represents the numbering system used in a locale.
  • struct Region
    A type that represents a geographic region, for use in specifying a locale or language.
  • struct Script
    The written script used with a given language.
  • struct Subdivision
    A type that represents a subdivision of a region, such as a state in the US or a province in Canada.
  • struct Variant
    A type that represents a locale’s language variant.

Operators

Initializers

Instance Properties

Instance Methods

Type Properties

Type Methods

Enumerations

Default Implementations

Relationships

Conforms To

  • Swift.Copyable
  • Swift.CustomDebugStringConvertible
  • Swift.CustomReflectable
  • Swift.CustomStringConvertible
  • Swift.Decodable
  • Swift.Encodable
  • Swift.Equatable
  • Swift.Escapable
  • Swift.Hashable
  • Swift.Sendable
  • Swift.SendableMetatype