R.swift icon indicating copy to clipboard operation
R.swift copied to clipboard

Localized strings with named parameters from .strings file

Open SpacyRicochet opened this issue 8 years ago • 5 comments

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")

SpacyRicochet avatar Dec 08 '16 22:12 SpacyRicochet

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?

mac-cain13 avatar Dec 09 '16 08:12 mac-cain13

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?

tomlokhorst avatar Dec 09 '16 09:12 tomlokhorst

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"])

josselin-oudry avatar Feb 23 '17 10:02 josselin-oudry

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.

FesenkoG avatar Jan 30 '19 12:01 FesenkoG

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)

luolong avatar Feb 01 '19 12:02 luolong