brain
brain

Reputation: 95

SwiftUI localization doesn't work shows only keys Xcode

i am working on app using SwiftUI in Xcode, and i want to localise it, but i have an issue. I already done all steps from youtube video

  1. Added localizations in Project -> info -> localizations
  2. Created 2 localization files
  3. Filled it like "hll" = "Hello"; etc.

and when i build an app i have only keys instead values (hll instead Hello), so i tried Text("hll") and Text(LocalizedStringKey("hll")). What am i missing?

Upvotes: 0

Views: 3487

Answers (2)

siki
siki

Reputation: 33

extension Text {
public init<S>(_ text: S) where S : StringProtocol {
    let text = String(text)
    
    let path = Bundle.main.path(forResource: "en", ofType: "lproj")
    let bundle = Bundle(path: path!)
    
    let translated = NSLocalizedString(text, tableName: "Localizable", bundle: bundle!, value: "", comment: "")

    self.init(verbatim: translated)
}

Upvotes: 0

lorem ipsum
lorem ipsum

Reputation: 29242

Make sure you have the Localizable.strings file in your project. There is where LocalizedStringKey looks for the keys/values as a default.

https://developer.apple.com/documentation/swiftui/localizedstringkey

If you want to define a different file use the

Text(LocalizedStringKey, tableName: String?, bundle: Bundle?, comment: StaticString?)

initializer and the tableName should match your .strings file name.

Upvotes: 3

Related Questions