Home > Mobile >  SwiftUI Localization one string is not getting translated
SwiftUI Localization one string is not getting translated

Time:10-15

I have this one string word "Every " which is stored like below that doesn't get translated.

        ForEach(viewModel.repeatTimes, id: \.self) { repeatedDay in
            Button(action: {
                if repeatTime.contains(repeatedDay) {
                    repeatTime.removeAll(where: { repeatedDay == $0 })
                } else {
                    repeatTime.append(repeatedDay)
                }
            }, label: {
                HStack {
                    Text("Every "   repeatedDay.day)
                        .foregroundColor(Color(.label))
                    Spacer()
                    if repeatTime.contains(repeatedDay) {
                        Image(systemName: "checkmark")
                    }
                }
            })
        }

While buttons, navigation titles other Text objects get translated without problems I can't get this to translate. In my localized.strings file this is how it is accessed.

"Every " = "Every ";

I've even tried deleting the whole file and copy pasting the strings so there wouldn't be any typo but still I can't get this to work and don't know what I am missing.

This is how app looks like on a simulator translated to Turkish

enter image description here

CodePudding user response:

you could try this, or a combination of such (as per @jnpdx suggestion):

Text(LocalizedStringKey("Every "   repeatedDay.day))

CodePudding user response:

Thanks for a solution @jnpdx however .foreground color wasn't working so I just changed it to this.

HStack {
Text("Every")
    .foregroundColor(Color(.label))
Text(repeatedDay.day)
    .foregroundColor(Color(.label))
}

Edit: A better way to do this:

HStack {
  Text("Every \(repeatedDay.day)")
     .foregroundColor(Color(.label))
}

in the localized.strings file:

"Every %@" = "Every %@"; // for EN
"Every %@" = "Her %@"; // for TR

since HStack adds it's own spacing it might cause some issues with localized string parameter '%@' it's much simpler and cleaner/

  • Related