2

i would like to translate a string, which have two variables inside. at the moment, i use for translating this code:

NSLocalizedString("Name_In_Langauge_String_File",comment:"")

but how can i translate the following string?

This is a test with 100 Pictures and 50 Users

where 100 and 50 are variables.

Stack108
  • 915
  • 2
  • 14
  • 35
  • 1
    [nslocalizedstring with swift variable](http://stackoverflow.com/questions/26277626/nslocalizedstring-with-swift-variable) – R Menke Mar 15 '16 at 12:09
  • yes i found this too, but how does it look like in the Localizable.string ? – Stack108 Mar 15 '16 at 12:15
  • [string formatting swift](https://thatthinginswift.com/string-formatting/) – R Menke Mar 15 '16 at 12:16
  • that doesnt solve my problem. how does my Localizable.string look like for that? at the moment i works in swift with this: `NSLocalizedString("Test",comment:"")` and in the String File `Test="This is a test"; – Stack108 Mar 15 '16 at 12:18

2 Answers2

3

Put this in you Localizable.strings:

"Name_In_Langauge_String_File" = "This is a test with %d Pictures and %d Users";

and in your code:

String.localizedStringWithFormat(
    NSLocalizedString("Name_In_Langauge_String_File",
    comment: ""),
    pictures,
    users)
Giordano Scalzo
  • 6,312
  • 3
  • 31
  • 31
0

In a project I was working on I noticed that we kept repeating the code to do the string formatting for the localization file. This meant you could not just use the value, you first needed to check what parameters were required. One way to avoid this problem is to use Swift enums. This method is also useful for unit testing your localizations.

Assume you have the following 3 localizations in your strings file:

"TestNoParams" = "This is a test message";
"TestOneParam" = "Hello %@";
"TestTwoParams" = "This is a test with %d Pictures and %d Users";

Now you can use the following enum, protocol and extension to reference your strings:

protocol LocalizationProtocol {
    var key: String { get }
    var value: String { get }
}

extension LocalizationProtocol {
    private func localizationValue() -> String {
        return NSLocalizedString(key, comment:key)
    }

    private func localizationValueWithFormat(parameters: CVarArgType...) -> String {
        return String(format: localizationValue(), arguments: parameters)
    }
}

enum Localizations: LocalizationProtocol {
    case TestNoParams
    case TestOneParam(name: String)
    case TestPicturesAndUsers(pictures: Int, users: Int)

    var key: String {
        switch self {
        case .TestNoParams: return "TestNoParams"
        case .TestOneParam: return "TestOneParam"
        case .TestPicturesAndUsers: return "TestTwoParams"
        }
    }

    var value: String {
        switch self {
        case .TestOneParam(let name):
            return localizationValueWithFormat(name)

        case .TestPicturesAndUsers(let pictures, let users):
            return localizationValueWithFormat(pictures, users)

        default:
            return localizationValue()
        }
    }
}

Now to use it you just need to call the enums value method:

let testNoParam = Localizations.TestNoParams.value
let testOneParam = Localizations.TestOneParam(name: "users name").value
let testTwoParams = Localizations.TestPicturesAndUsers(pictures: 4, users: 500).value

The example I have shown is simplified, but you can also nest enums to provide a nice grouping for your localizations. For instance you could have your enums nested by ViewController. This is an example for a welcome message: Localizations.Main.WelcomeMessage.value

totiDev
  • 5,189
  • 3
  • 30
  • 30