netwire
netwire

Reputation: 7225

NSLocalizedString with variables Swift

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?

Upvotes: 33

Views: 25761

Answers (7)

Leszek Szary
Leszek Szary

Reputation: 10346

Note that while using "account.by_user" = "by %@ and %@"; as you key works fine, it is often a good idea to use positional specifiers such as %1$@, %2$@ for localized strings. The reason for that is that this allows to use a different order of variables for different languages. For this specific case from the question probably it does not matter, but in some cases in some languages it might be needed to use a different order of variables due to grammatical rules used in that language.

So for example instead of using "account.by_user" = "by %@ and %@"; you could use in one strings file this: "account.by_user" = "by %1$@ and %2$@"; and in strings file for a different language use this: "account.by_user" = "by %2$@ and %1$@"; and then the code below:

    let text = NSLocalizedString("account.by_user", comment: "")
    print("text: \(String(format: simple, "Peter", "Larry"))")

would print: text: by Peter and Larry for the first language and for the second language it would print: text: by Larry and Peter.

Upvotes: 0

r_19
r_19

Reputation: 1948

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)

Note that localizedStringWithFormat sets the returned String's local based on given first argument:

  • No, it does not translate anything.
  • This confusion comes from wrong naming of NSLocalizedString.
  • It should have been named NSTranslatedString, or NSTranslatable (instead of NSLocalizedString).

Upvotes: 25

Stanislav Redreiev
Stanislav Redreiev

Reputation: 31

I'm using extension for this.

extension String {
    var localized: String {
        return NSLocalizedString(self, comment: "\(self)_comment")
    }

    func localized(args: CVarArg...) -> String {
        return String(format: localized, arguments: args)
    }
}

For example

// Localizable.strings
// "account.by_user" = "by %@ and %@"

// How to use
account.by_user.localized(args: "arg_A", "arg_B")

Upvotes: 3

bauerMusic
bauerMusic

Reputation: 6176

Adding a small sample below as it took me some time to figure the Localizable.strings file formatting.

Example of adding variables to localized string:

In code:

let myVar: String = "My Var"

String(format: NSLocalizedString("translated_key %@",
      comment: "Comment"), myVar)

In Localizable.strings file:

"translated_key %@" = "My var is: %@";

Of course, the %@ on the right side can be placed anywhere:

"translated_key %@" = "My var is: %@";
"translated_key %@" = "%@ is my var";
"translated_key %@" = "I use %@ as my var";

Also, %@ can be replaced by %d for int or %f for a float.

Upvotes: 12

jawad
jawad

Reputation: 825

String(format: NSLocalizedString("Pick_Only", comment: ""), sectionData.max)

In Localizable.strings: "Pick_Only" = "Pick only %d";

Upvotes: 1

duan
duan

Reputation: 8885

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")

Upvotes: 48

Erich Flock
Erich Flock

Reputation: 287

An example with the Localizable.strings file:

In Localizable.strings file:

localizable_text = "Title %@ (Code: %@)";

In Swift class:

let title = "Error"
let code = "123456"
let alertMessage = String(format: NSLocalizedString("localizable_text", comment: ""), title, code)

Upvotes: 11

Related Questions