Terraforming-mars: i18n of dynamic PlayerInput titles

Created on 6 Oct 2020  ·  5Comments  ·  Source: bafolts/terraforming-mars

Recent #dev-topics talk raised an i18n problem. Again.
We have strings like "Select card to add " + jovianTags + " floater(s)" formatted at the backend.
So on client side we need to translate endless list of similar values like these:

"Select card to add 1 floater(s)"
"Select card to add 2 floater(s)"
"Select card to add 3 floater(s)"
"Select card to add 4 floater(s)"

Obviously, it’s better to get a pattern “Select card to add ${0} floater(s)” with param and then replace “${0}” with a value from that param.

What is the best solution to it?

I thought we could return an object like LocalizableString with params instead of regular string, but my attempt to change the type of title field of PlayerInput to something like:

public title: string | LocalizableString

produced a lot of type errors. Is this approach incorrect? How could these type errors be fixed? What alternatives do we have?

enhancement question

All 5 comments

@bafolts can you help us with new ideas?

Since the value is always going to be a number it should be simpler to use regular expressions for the match and replacement.

Something like:

"Select card to add 5 resource".replace(new RegExp("Select card to add (\\d) resource"), "Select card to add $1 resources!")
"Select card to add 5 resource!"

Updating the directive should only need to call to match the built RegExp.

new RegExp("Select card to add (\\d) resource").test("Select card to add 1 resource")

I am going through open issues looking for one to grab. I can implement this if you don't mind the RegExp approach @alrusdi

@bafolts interesting approach. Can we provide placeholder symbols ( like {} ) instead of actual regex? Placeholders are easier for translators to understand.

And... not all the variable params are numbers. Sometimes it is a string containing name of some entity (like player name, production name, resource name, Turmoil party name etc.). Can we wrap original strings to some pair of symbols to be matchable as variable?

So for runtime string like this:
"{Brian} reduced {Alrusdi}'s {plant} production to {1}",
translation string can be something like this:
"{1} уменьшил производство {3} игрока {2} на {4}" (Note a little change in substitution order to make the phrase more natural in target language)

That should be fine. I can get something together.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dabewi picture dabewi  ·  5Comments

dabewi picture dabewi  ·  6Comments

dabewi picture dabewi  ·  3Comments

galfridus picture galfridus  ·  5Comments

vsrisuknimit picture vsrisuknimit  ·  5Comments