New in iOS 15, we are invited to use this String initializer method to make localizable strings in our Swift code:
init(localized keyAndValue: String.LocalizationValue,
table: String? = nil, bundle: Bundle? = nil,
locale: Locale = .current, comment: StaticString? = nil)
The trouble is that the first parameter is, as the internal name suggests, used for both the key and the value. You can see that from this localized French strings file:
/* Alert message: Report a tap */
"You tapped me!" = "Vous m'avez tapé!";
That resulted from my saying
String(localized:"You tapped me!", comment: "Alert message: Report a tap")
and localizing for French.
That's totally wrong! This is supposed to be a list of key–value pairs; we shouldn't be using the English user-facing text as a key.
For one thing, if we now change the English text in our String(localized:comment:)
call, our French translation will break. Also, we would be unable to have different French translations for the same English text used in different contexts.
What are we supposed to do about this?
NSLocalizedString
shows using English as the key with the same behaviour. I don't see this as a "bug". Unless I am missing something there is nothing to stop you using codes instead of English and then treating the base locale the same as any other strings file. It is just a convention and how disciplined you are with your design and i8n workflows. – DunghillNSLocalizedString("key", value: "value", comment: "comment")
and then export and import a localization, the resulting Localizable.strings file has"key" = "<translation>"
, but the string shows up in the English interface as"value"
. That's how I've always done it. It's easy and convenient. So what I'm saying is, that's howString(localized:)
should work. – ForemanString(localized:...
? The official documentation still treats it as beta, with No overview available, 4 weeks after iOS 15 has been released. – Toehold.localizedStringWithFormat()
. The method it is supposed to replace has a very different signature. Are you referring to any more specific source? – Toehold