mac-cain13 / R.swift

Strong typed, autocompleted resources like images, fonts and segues in Swift projects
MIT License
9.49k stars 760 forks source link

Localized strings with named parameters from .strings file #294

Open SpacyRicochet opened 7 years ago

SpacyRicochet commented 7 years ago

Instead of only generating named parameters for localised string functions if they're taken from a stringsdict file, you could also add a rule/option that generates named functions from keys as well.

For example;

"test_string_amount%@" = "A test string with amount '%@'";

Could be used like this;

R.string.localizable.testString(amount: "3")

mac-cain13 commented 7 years ago

Interesting idea, thanks for the suggestion!

We should describe how this behaviour works and it shouldn't be confusing for users, but the example looks quite elegant.

@tomlokhorst what do you think?

tomlokhorst commented 7 years ago

Interesting idea, the calling code sure looks nicer.

The design requires a bit of work, a couple of complications that come to mind:

Also, this shouldn't break existing code (maybe in a major version release?). We could enable this via command line flag, but do we want to add different code generation paths behind flags?

josselin-oudry commented 7 years ago

On the same note, based on the Android square/phrase library: [https://github.com/square/phrase], it would interesting to implement this: Base string "account_login_testGreetings" = "Hello {firstName} {lastName}!";

Could be used like this: R.string.localizable.account_login_testGreetings(firstName: "John", lastName: "Appleseed")

This would address at least multiple parameters and positional specifiers. For now I've implemented my own search and replace function (String.localizedString()) that I call like this: let text = String.localizedString(rKey: R.string.localizable.account_login_testGreetings(), ["firstName": "John", "lastName": "Appleseed"])

FesenkoG commented 5 years ago

Hey! @tomlokhorst @mac-cain13 are you going to implement this feature, or, maybe, have already implemented it? I've just faced a problem with different order for different locales and that's kind of sad. Am I supposed to use my custom workarounds for such issues or you have some built in solution?

UPD: Probably found a solution.

Note that you can also use the “n$” positional specifiers such as %1$@ %2$s.

luolong commented 5 years ago

There is also an option to parse localization of string comments for parameter names. (this would greatly encourage commenting one's localization strings and thus help developers and translators alike)