Search code examples
swiftstringxcodeswiftuilocalizable.strings

Localization of Text in SwiftUI does not work consistently


I have been trying to localize a demo Sneaker App i am making for practice, i tried to localize some strings and they worked just fine, some others did not work but i managed to make them work using LocalizedStringKey(_). However this one line does not localize whatsoever, i cannot understand why.

here is the Text in the View:

Text("Size: \(orderItem.sneakerSize) | Color: \(orderItem.sneakerColor.colorToString())")

here is the localization for English in the Localizable.strings file:

"Size: %@ | Color: %@" = "Size: %@ | Color: %@";

and here is the localization for Arabic:

"Size: %@ | Color: %@" = "حجم: %@ | لون: %@";

yet when i try testing in the preview mode or in the actual build mode, it does not localize that string, can anyone explain??


Solution

  • I understand from your additional comment that my suggestions helped. So I'll make them into an answer:

    1. Check that the data types correspond correctly with the placeholders.
    1. For the arabic version, check that there is no forwards/backwards mix-up. The example in your question includes %@ as well as @%.

    2. It seems that your localization keys are the same as the localized English versions. This might be making it difficult to tell, whether the English localizations are actually working or not. It might help to use simpler keys, such as "SUMMARY %lld %@" = "Size: %lld | Color: %@"; so that you notice immediately if the English localization is not working either.

    3. For the additional case that you added in your comment, you might need to be more specific with the formatting. I was able to get it working for English like this:

    Text("DISCOUNT \(orderItem.sneakerItem.discount, specifier: "%.1f")")
    
    "DISCOUNT %.1f" = "You get %.1f%% off";