Feature: Fully customisable number abbreviations per translation

This commit is contained in:
Rubidium 2024-02-08 21:39:16 +01:00 committed by rubidium42
parent a253205b93
commit 9f8fd80112
1 changed files with 20 additions and 20 deletions

View File

@ -56,6 +56,7 @@ const LanguageMetadata *_current_language = nullptr; ///< The currently loaded l
TextDirection _current_text_dir; ///< Text direction of the currently selected language. TextDirection _current_text_dir; ///< Text direction of the currently selected language.
static NumberFormatSeparators _number_format_separators; static NumberFormatSeparators _number_format_separators;
static NumberAbbreviations _number_abbreviations;
#ifdef WITH_ICU_I18N #ifdef WITH_ICU_I18N
std::unique_ptr<icu::Collator> _current_collator; ///< Collator for the language currently in use. std::unique_ptr<icu::Collator> _current_collator; ///< Collator for the language currently in use.
@ -389,6 +390,8 @@ static const char *GetDecimalSeparator()
void InitializeNumberFormats() void InitializeNumberFormats()
{ {
ParseNumberFormatSeparators(_number_format_separators, _current_language->number_format); ParseNumberFormatSeparators(_number_format_separators, _current_language->number_format);
ParseNumberAbbreviations(_number_abbreviations, _current_language->number_abbreviations);
_number_abbreviations.emplace_back(0, _number_format_separators);
} }
/** /**
@ -516,32 +519,29 @@ static void FormatGenericCurrency(StringBuilder &builder, const CurrencySpec *sp
* The only remaining value is 1 (suffix), so everything that is not 1 */ * The only remaining value is 1 (suffix), so everything that is not 1 */
if (spec->symbol_pos != 1) builder += spec->prefix; if (spec->symbol_pos != 1) builder += spec->prefix;
StringID number_str = STR_NULL; NumberFormatSeparators *format = &_number_format_separators;
/* For huge numbers, compact the number. */ /* For huge numbers, compact the number. */
if (compact) { if (compact) {
/* Take care of the thousand rounding. Having 1 000 000 k auto it = _number_abbreviations.begin();
* and 1 000 M is inconsistent, so always use 1 000 M. */ for (;;) {
if (number >= Money(1'000'000'000'000'000) - 500'000'000) { int64_t threshold = it->threshold;
number = (number + Money(500'000'000'000)) / Money(1'000'000'000'000); ++it;
number_str = STR_CURRENCY_SHORT_TERA; if (it == _number_abbreviations.end()) break;
} else if (number >= Money(1'000'000'000'000) - 500'000) {
number = (number + 500'000'000) / 1'000'000'000; int64_t divisor = it->threshold;
number_str = STR_CURRENCY_SHORT_GIGA; threshold -= divisor / 2;
} else if (number >= 1'000'000'000 - 500) {
number = (number + 500'000) / 1'000'000; if ((int64_t)number > threshold) {
number_str = STR_CURRENCY_SHORT_MEGA; format = &it->format;
} else if (number >= 1'000'000) { number += divisor / 2;
number = (number + 500) / 1'000; number /= divisor;
number_str = STR_CURRENCY_SHORT_KILO; break;
}
} }
} }
FormatNumber(builder, number, _number_format_separators); FormatNumber(builder, number, *format);
if (number_str != STR_NULL) {
auto tmp_params = ArrayStringParameters<0>();
FormatString(builder, GetStringPtr(number_str), tmp_params);
}
/* Add suffix part, following symbol_pos specification. /* Add suffix part, following symbol_pos specification.
* Here, it can can be either 1 (suffix) or 2 (both prefix and suffix). * Here, it can can be either 1 (suffix) or 2 (both prefix and suffix).