Consider using a template engine for string building #1212
Labels
enhancement
New feature or request
question
Further information is requested
refactoring
This issue requires code refactoring
Is your feature request related to a problem? Please describe.
When constructing complex text, we're limited by the plural-handling capabilities of
gettext
(Qt's own solution has the same limitations). This becomes obvious with messages where two or more plural forms can appear, such as%1 citizen(s) (is|are) made happier using %2 luxury good(s)
.Describe the solution you'd like
We should have a better way to format such complex strings. One solution is to use ICU directly: its class
MessageFormat
allows for very complex templates. We already depend on ICU through Qt.I believe that simple strings should still go through
gettext
or Qt, but we could fetch the translated template with it and then format it:icu::MessageFormat format = qstring_to_icu(_("some template here")); QString formatted = icu_to_qstring(format.format(args...));
The two conversion functions look easy to implement since both ICU and
QString
use UTF-16 for the internal representation. Maybe we'll want a small wrapper to avoid writing ICU code as this might be confusing.A translator guide will be needed to help them with the uncommon format (in terms of Freeciv21 strings).
Describe alternatives you've considered
Splitting sentences is a bad option because not all language like the same sentence structures as English. Using
(s)
is also a bad option for languages with many plurals.Grantlee is another templating engine, but it suffers from the same drawbacks with respect to translations as
gettext
and Qt.Additional context
#1206 #1208 #1209 #1210
Discord discussion at https://discord.com/channels/378908274113904641/753356257007501454/1005933097428201552
The text was updated successfully, but these errors were encountered: