I'm trying to figure out how to use NSLocalizedString with variables.
For example, if I want to output "by Peter and Larry", in my Localizable.strings
file, should I have the following?
"account.by_user" = "by %@ and %@";
How would I call NSLocalizedString("account.by_user", comment: "")
with if there are 2 variables name1
and name2
where name1
= Peter and name2
= Larry?
yes, you should have "account.by_user" = "by %@ and %@";
and take this:
let localizedString = NSLocalizedString("account.by_user", comment: "any comment") let wantedString = String(format: localizedString, "Peter","Larry")
This is another way and how I do it.
let myString = String.localizedStringWithFormat(NSLocalizedString("by %@ and %@", comment: "yourComment"), name1, name2)
basically, the main idea of Localized String with format is like this:
let math = "Math" let science = "Science" String.localizedStringWithFormat(NSLocalizedString("I love %@ and %@", comment: "loved Subjects"), math, science)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With