R.swift
R.swift copied to clipboard
Localized strings with named parameters from .strings file
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")
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?
Interesting idea, the calling code sure looks nicer.
The design requires a bit of work, a couple of complications that come to mind:
- Multiple parameters
- Different parameter types
- Positional specifiers on parameters
- Naming of generated StringResource constants
- No conflicts with localised strings from different locales https://github.com/mac-cain13/R.swift/issues/219
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?
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"])
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.
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)