|
I'm learning about localization using a Strings file. I'm past the basics and am now learning about using keys that contain argument placeholders. I want the argument values to be translated too. For example, for English I have
and for French I have:
In my code I want to translate the keys "sun" and "tomorrow" and use the results to pass for the "Annie" translation like this:
I don't get the strings I expect from |
|
Interestingly |
|
I've narrowed this down a bit. I can use either of these approaches to get a translation in code:
These get a translation based on the locale selected in the Settings app.
However, neither of these honor the locale specified with the
The same issue exists with retrieving localized images from an "Image Set". I wonder if this is considered a bug in SwiftUI. |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure and A/B test your entire paywall UI without any code changes or app updates. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
Okay, from my understanding
matches but isn't translated in the process. Personally I use an extension for String:
Then try
|
|
@Hatsushira I tried your suggestions. I added this:
and I changed my code to this where the comments indicate the output when the locale is "fr-FR":
I get the correct translations from Also see how I'm using Do you see anything I'm doing wrong? |
|
As
I don't know how the localization internally works. So I'm just guessing here. After reading your initial post again I'm guessing you don't have sun and tomorrow in your English strings file. You should have all keys in all translation files. Perhaps this could be the problem. So add "sun" = "sun" and "tomorrow"="tomorrow" in your English strings file. I don't know if you know about "Edit Scheme > Options" and check the "Show non-localized strings" box. Then in your debug sessions all missing strings are shown in capital letters. |
|
@Hatsushira You said "You should have all keys in all translation files." I feel 99% certain this is not true. It's my understanding that when a translation is missing it just uses the key. It does that for me. That's a big benefit of making the keys be the English text you want to see. But I did try adding the following in the English translations and it didn't help.
I was not aware of the "Show non-localized strings" option. Thanks for telling me about that! Interestingly that causes lots of errors to appear in the debug console because it attempts to translate every string passed to the Going back to my earlier message, the key thing to notice is that |
|
Do your translations work on a real device? Perhaps this is one of the quirks in the Simulator/Preview and we try to fix something we're just not able to. I can guarantee you that |
|
@Hatsushira I get the same results on my iPhone.
I have a segmented But changing the In my app I want to allow the user to select a language without changing their preferred language in the Settings app. So the issue I'm facing is that |
|
Okay, what I understand is that you want to change the locale dynamically inside a view and use the environment modifier for it. It could be this simply doesn't work. From the documentation
I don't read from this that it should not but I really don't know. |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure and A/B test your entire paywall UI without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.