2

So I'm localizing a project using an Extension to NSString that I found on SO. That extension looks like this:

extension String {
    var localized: String {
        return NSLocalizedString(self, tableName: nil, bundle: NSBundle.mainBundle(), value: "", comment: "")
    }
}

I however have come across strings in my Localizable.strings list that contain parameters. For example:

"explore_item_desc1" = "Welcome to rent my %1$s!";

Before I was able to do this:

uiLabel.text = "localizedString".localized

How do I do something similar for those strings holding parameters?

Rutger Huijsmans
  • 2,330
  • 2
  • 30
  • 67
  • maybe you can do something [like this](http://stackoverflow.com/a/26277912/1219956) (make your `localized` var into a func with variable parameters instead) – Fonix May 10 '16 at 03:14

1 Answers1

2

Your localized keys should look like this:

"localized_key_name1" = "foo";

"localized_key_name2" = "%@ foo %@";

Make your localized variable into a function rather

extension String {
     var localized: String {
         return NSLocalizedString(self, tableName: nil, bundle: NSBundle.mainBundle(), value: "", comment: "")
     }
     func localized(args : CVarArgType...) -> String {
         return withVaList(args, { (f:CVaListPointer) -> String in
        (NSString.init(format:NSLocalizedString(self, comment:""), arguments: f) as String)
     })
  }
}

usage without Parameters

uiLabel.text = "localized_key_name1".localized   // "foo"

usage with Parameters

uiLabel.text = "localized_key_name2".localized("param1", "param2")   // "param1 foo param2"

credit

Community
  • 1
  • 1
Fonix
  • 11,447
  • 3
  • 45
  • 74
  • Thanks a bunch! This is almost working. When I use it it prints the params strangely though. So using: print("explore_item_desc1".localized("param1")) will result in: Welcome to rent my ¯Ûò! – Rutger Huijsmans May 10 '16 at 03:31
  • ah hmm, strange, let me fiddle around a bit with it – Fonix May 10 '16 at 03:33
  • it seems swift has a problem with passing variadic arguments to other functions that have variadic arguments, after googling around it seems swift just doesnt support it properly yet – Fonix May 10 '16 at 04:34
  • I only need to pass in 1 parameter though, after changing (params: String...) to (param: String) it is still printing out the same line. – Rutger Huijsmans May 10 '16 at 04:43
  • it seems the problem has already been solved http://stackoverflow.com/a/29401619/1219956 – Fonix May 10 '16 at 04:45
  • Even with that post I'm not able to get it work, anyway you can add the right function to the snippet proposed by you above? – Rutger Huijsmans May 10 '16 at 04:56
  • hmm ye it doesnt seem to work for me either, seems swift just doesnt want you to pass variadic parameters at all heh, ill let you know if i find a proper solution, but its looking dire at the moment :( – Fonix May 10 '16 at 04:58
  • ok got it to work, if you use the NSString type of formatting, it seems to work, so use `%@` with my updated solution – Fonix May 10 '16 at 05:10
  • Still not exactly what I'm looking for. This: print("explore_item_desc1 %@".localizedParam(name)) will print out: "explore_item_desc1 梯" when I'm hoping to get "Welcome to rent my 梯!" it's not localizing explore_item_desc1 from my Strings file this way. – Rutger Huijsmans May 10 '16 at 05:19
  • 1
    oh no, you still should use your key how it is (so just "explore_item_desc1") in the values of the localized strings you must use the %@ (i was just using the key as the same as the value in my case) so `"explore_item_desc1" = "Welcome to rent my %@";` – Fonix May 10 '16 at 05:22
  • Thanks! Works perfectly! – Rutger Huijsmans May 10 '16 at 06:07
  • good edit having both the var and the function, but you can name the func and the var the same then you can use `.localized("param")` or `.localized` (more elegant in my opinion), but up to you – Fonix May 11 '16 at 05:10