— SwiftUI, LocalizedStringKey, Localization, User Interface — 1 min read
LocalizedStringKey is a type in SwiftUI that allows you to look up localized strings. It's commonly used for providing localized content in your app's user interface. Here are the key points about LocalizedStringKey:
Implicit Localization:
ExpressibleByStringLiteral:
String Variables:
Creating a LocalizedStringKey:
1let localizedStringKey: LocalizedStringKey = "Hello"
Example: Suppose your app is localized into Japanese, and your Localizable.strings file contains the following translation:
1"Today" = "今日";
When you use a literal string like this:
1Section(header: Text("Today")) {2 // ...3}
SwiftUI localizes the "Today" section header. However, if you use string variables directly, they won't be localized:
1ForEach(messageStore.today) { message in2 Text(message.title) // Uses the string value verbatim3}
Remember that LocalizedStringKey looks for a corresponding translation in your localization files. If it finds one, it replaces the key with the translated string; otherwise, it uses the key as a dummy string value.