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")
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)
}
}
}

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! 🌎")
}
}

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

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.