Skip to content

String(localized:) and NSLocalizedString Returns Wrong Value #5

@muhasturk

Description

@muhasturk

Do you have any idea why the following codes are not working? You can reproduce on your example project by inserting below code snipped somewhere and you are going to see that the result string is based on device language not the set Locale.

Not Working 1

String(localized: "Hello")

Not Working 2

NSLocalizedString("Hello", comment: "")

Working

Text("Hello")

MainView.swift Reproduce

struct MainView: View {

    // MARK: - State properties

    @State private var isPresented = false

    // MARK: - body

    var body: some View {
        NavigationView {
            VStack {
                Image("logo")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: 240, height: 100)
                Text("Hello")
                    .fontWeight(.bold)
                    .padding()
                Text("About")
                    .padding()
                Button("Change the language") {
                    isPresented = true
                }
            }
            .navigationTitle(String(localized: "Hello"))
        }
        .buttonStyle(AppButtonStyle())
        .fullScreenCover(isPresented: $isPresented) {
            LangaugeView()
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions