0

I have a problem with my localized translation. Handing a String to a view that has a translation in the localizable.strings file, will not get translated.

How can you pass a String to a View and translate it with localizable.strings.

TextFieldtView(label:"email", exampleInput: "beispielemail@web.de", paddingLeading: 30, paddingTrailing: 30, secure: false, widthLabel: 60, binding: $id)
Rectangle()
    .frame(width:widthLabel, height: 25)
    .offset(x:-110, y: -10)
    .foregroundColor(Color(.white))
    .overlay(Text(LocalizedStringKey("\(label)"))
    .foregroundColor(Color(.black)).opacity(1)
    .frame(height: 25)
    .padding(.bottom)
    .background(Rectangle().fill(.white).padding(.vertical, 10))
    .offset(x:-110, y: -4)
    )
Text(LocalizedStringKey("\(label)")
Text("\(label)")

Is how I tried to call it and pass it to the view but without success.

The label still shows as the name of the localized string not the translation itself. Thank you for your help!

burnsi
  • 6,194
  • 13
  • 17
  • 27
DoMaxBa
  • 29
  • 3
  • What I also don't understand is: what is the difference between a Text View showing a string like Text("example") and: var example: String = "example", Text(example) ? The second one doesn't get translated even if the String value of it is included in the Localizable.strings. Is there a way to get the string from a variable that represents a string such that it gets handled the same way as the the input of Text("example"?) – DoMaxBa Feb 22 '23 at 08:43
  • The answer to your question in the comment is type inference. If you are passing a String variable in, the type checker will assume it really is a string and not a localized string key. What you always can do is to explicitly use `Text(NSLocalizedString("your-key", comment: "comment"))` to get the localized string and do whatever, like putting it into a Text view. – JanMensch Feb 22 '23 at 08:46
  • 1
    I now found the answer: Text(LocalizedStringKey(stringLiteral: variable)) solves it:) – DoMaxBa Feb 22 '23 at 08:53

0 Answers0