跳转至
Structure

LocalizedStringKey

The key used to look up a string in a strings file or strings dictionary file.

Declaration

@frozen struct LocalizedStringKey : Equatable, ExpressibleByStringInterpolation

Overview

To make the text in your app appear as many different languages depending on the user's preference, use this structure in place of String.

Essentially the way localization works is that rather than using strings directly throughout your app, you use this structure instead. This lets you look up string keys which are translated to all the app's supported languages in a .strings file.

To learn about creating this file and the process of setting up your project for localization, check out these sources:

Creating a localized string key

The most straightforward way to create a localized string key is by using its initializer:

let hello = LocalizedStringKey("Hello")
You can also create a localized string key from a string literal, but you must specify the type explicitly or else it will be interpreted as a String:

let hello1 = "Hello" //Type String
let hello2: LocalizedStringKey = "Hello" //Type LocalizedStringKey

Using a localized string key

Many views in SwiftUI, like Button and Text accept localized string keys directly through their initializers by default.

struct UsingLocalizationView: View {
    let text: LocalizedStringKey = "Hello 🙋‍♀️"

    var body: some View {
        VStack {
            Button(text) { }
            Text(text)
        }
    }
}
A screenshot displaying a button and a text view in a VStack each reading "Hola" followed by a man emoji.

Also, since SwiftUI is localization-first, if you pass a string literal to these initializers, they will be interpreted as localized string keys!

struct WithStringLiteralView: View {
    var body: some View {
        Text("This gets localized! 🌎")
    }
}
A screenshot displaying a single text view that reads "¡Esto se localiza!" the Spanish translation of the text that is passed to the view.

However, if your variable is already a string, the initializer will not localize the string:

struct ContentView: View {
    let s = "Hello 🙋‍♀️"

    var body: some View {
        VStack {
            Text(s) //Not localized
            Text("Hello 🙋‍♀️") //Localized!
        }
    }
}
A screenshot displaying two text views in a VStack; the first does not use a localized string key and displays as "Hello" followed by a woman emoji, but the second does use a localized string key and instead displays as "Hola" followed by a man emoji.

Availability

iOS 13.0+

macOS 10.15+

tvOS 13.0+

watchOS 6.0+

Topics


Type Alias

ExtendedGraphemeClusterLiteralType A type that represents an extended grapheme cluster literal.

StringLiteralType A type that represents a string literal.

UnicodeScalarLiteralType A type that represents a Unicode scalar literal.


Initializer

init(_:) Creates a localized string key from a String value.

init(stringInterpolation:) Creates an instance from a string interpolation.

init(stringLiteral:) Creates an instance initialized to the given string value.


Structure

StringInterpolation The type each segment of a string literal containing interpolations should be appended to.


Type Method

==(a:b:) Returns a Boolean value indicating whether two values are equal.