Initializer

init(stringInterpolation:)

Creates a localized string key from the given string interpolation.

Declaration 宣言

init(stringInterpolation: LocalizedStringKey.StringInterpolation)

Parameters パラメータ

stringInterpolation

The string interpolation to use as the localization key.

Discussion 議論

To create a localized string key from a string interpolation, use the \() string interpolation syntax. Swift matches the parameter types in the expression to one of the appendInterpolation methods in LocalizedStringKey.StringInterpolation. The interpolated types can include numeric values, Foundation types, and SwiftUI Text and Image instances.

The following example uses a string interpolation with two arguments: an unlabeled Date and a Text.DateStyle labeled style. The compiler maps these to the method appendInterpolation(_:style:) as it builds the string that it creates the LocalizedStringKey with.


let key = LocalizedStringKey("Date is \(company.foundedDate, style: .offset)")
let text = Text(key) // Text contains "Date is +45 years"

You can write this example more concisely, implicitly creating a LocalizedStringKey as the parameter to the Text initializer:


let text = Text("Date is \(company.foundedDate, style: .offset)")

See Also 参照

Creating a Key from an Interpolation