Make utils::format_timespan() use gettext plurals

This uses N_n() in combination with VNGETTEXT() to get the plural forms
written to po files by xgettext.

NOTE: This is NOT a string-freeze-compliant change. Changing the strings
from being standalone to being part of a plural form set causes them to
be automatically fuzzied by the pot-update target. Merging this in 1.16
will have to wait until after 1.16.0 is released.

CC #6036
This commit is contained in:
Iris Morelle 2021-09-17 07:32:33 -03:00
parent 190b3cce08
commit 4df1365ffa
No known key found for this signature in database
GPG key ID: E312033F4023A753

View file

@ -298,26 +298,34 @@ std::string format_timespan(std::time_t time)
typedef std::tuple<std::time_t, const char*, const char*> time_factor;
static const std::vector<time_factor> TIME_FACTORS{
time_factor{ 31104000, N_("timespan^$num year"), N_("timespan^$num years") }, // 12 months
time_factor{ 2592000, N_("timespan^$num month"), N_("timespan^$num months") }, // 30 days
time_factor{ 604800, N_("timespan^$num week"), N_("timespan^$num weeks") },
time_factor{ 86400, N_("timespan^$num day"), N_("timespan^$num days") },
time_factor{ 3600, N_("timespan^$num hour"), N_("timespan^$num hours") },
time_factor{ 60, N_("timespan^$num minute"), N_("timespan^$num minutes") },
time_factor{ 1, N_("timespan^$num second"), N_("timespan^$num seconds") },
// TRANSLATORS: The "timespan^$num xxxxx" strings originating from the same file
// as the string with this comment MUST be translated following the usual rules
// for WML variable interpolation -- that is, without including or translating
// the caret^ prefix, and leaving the $num variable specification intact, since
// it is technically code. The only translatable natural word to be found here
// is the time unit (year, month, etc.) For example, for French you would
// translate "timespan^$num years" as "$num ans", thus allowing the game UI to
// generate output such as "39 ans" after variable interpolation.
time_factor{ 31104000, N_n("timespan^$num year", "timespan^$num years") }, // 12 months
time_factor{ 2592000, N_n("timespan^$num month", "timespan^$num months") }, // 30 days
time_factor{ 604800, N_n("timespan^$num week", "timespan^$num weeks") },
time_factor{ 86400, N_n("timespan^$num day", "timespan^$num days") },
time_factor{ 3600, N_n("timespan^$num hour", "timespan^$num hours") },
time_factor{ 60, N_n("timespan^$num minute", "timespan^$num minutes") },
time_factor{ 1, N_n("timespan^$num second", "timespan^$num seconds") },
};
std::vector<t_string> display_text;
string_map i18n;
for(const auto& factor : TIME_FACTORS) {
const int amount = time / std::get<0>(factor);
const auto [ secs, fmt_singular, fmt_plural ] = factor;
const int amount = time / secs;
if(amount) {
time -= std::get<0>(factor) * amount;
time -= secs * amount;
i18n["num"] = std::to_string(amount);
const auto fmt = amount == 1 ? std::get<1>(factor) : std::get<2>(factor);
display_text.emplace_back(VGETTEXT(fmt, i18n));
display_text.emplace_back(VNGETTEXT(fmt_singular, fmt_plural, amount, i18n));
}
}