tomkowz / Swifternalization

Localize iOS apps in a smarter way using JSON files. Swift framework.
MIT License
579 stars 47 forks source link

Passing strings as arguments #18

Closed paulrolfe closed 2 months ago

paulrolfe commented 8 years ago

I can't figure out how I would pass in strings as arguments. Perhaps it's not built in, I was expecting that I18n.localizedString("Key", stringValue: "word") where "Key": "Put %@ here" would produce "Put word here". That's apparently not what happens though.

Is there support for this that I don't see?

tomkowz commented 8 years ago

Hi @paulrolfe,

The thing that you want to achieve is not possible with the method you suggested.

The stringValue parameter is not for passing a value that will be set as a format. It is only to help determine Switernalization to filter available options (translations) and pick the most correct one.

It could work if your translation json file look like this:

    "Key": {
        "word": "put word here",
        "default": "put %@ here"
    }

What I'd suggest for this moment is to create your string this way:

let format = Swifternalization.localizedString("put.?.here.format")
let output = String(format: format, arguments: ["word"]) // put word here

However, I am thinking whether this is worth to implement method like I18n.localizedStringWithFormat(key, stringValue, arguments, fittingWidth, defaultValue, comment) that will obtain format string for a specified key and a stringValue and will pass arguments to the string on its own. The last thing is what you would like to use and this is not implemented in the current version.

paulrolfe commented 8 years ago

Ok I can do that for now -- build the string on my own. But I think that would be a great feature to fill out this library.

Thanks!

tomkowz commented 8 years ago

Sure, np, thanks for the idea, will think about it this week!