From cba329d9e2f15d4973be8752600b7de53534a111 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Fri, 6 Dec 2024 12:32:36 +0000 Subject: [PATCH] Add: Translatable list separator. (#13149) Some languages should use a separator other than ", " to separate list items, so it is now a translatable string. --- src/cargotype.cpp | 4 +++- src/lang/english.txt | 2 ++ src/network/network_content_gui.cpp | 7 ++++--- src/roadveh_gui.cpp | 3 ++- src/strings.cpp | 14 +++++++++++++- src/strings_func.h | 1 + src/table/strgen_tables.h | 1 + 7 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/cargotype.cpp b/src/cargotype.cpp index 2e232d6ed92..454895c9b7d 100644 --- a/src/cargotype.cpp +++ b/src/cargotype.cpp @@ -268,6 +268,8 @@ uint64_t CargoSpec::WeightOfNUnitsInTrain(uint32_t n) const */ std::optional BuildCargoAcceptanceString(const CargoArray &acceptance, StringID label) { + std::string_view list_separator = GetListSeparator(); + /* Cargo acceptance is displayed in a extra multiline */ std::stringstream line; line << GetString(label); @@ -277,7 +279,7 @@ std::optional BuildCargoAcceptanceString(const CargoArray &acceptan CargoID cid = cs->Index(); if (acceptance[cid] > 0) { /* Add a comma between each item. */ - if (found) line << ", "; + if (found) line << list_separator; found = true; /* If the accepted value is less than 8, show it in 1/8:ths */ diff --git a/src/lang/english.txt b/src/lang/english.txt index 4a86a9dbcbe..08b4828db70 100644 --- a/src/lang/english.txt +++ b/src/lang/english.txt @@ -266,6 +266,8 @@ STR_UNITS_MINUTES :{NUM}{NBSP}minu STR_UNITS_YEARS :{NUM}{NBSP}year{P "" s} STR_UNITS_PERIODS :{NUM}{NBSP}period{P "" s} +STR_LIST_SEPARATOR :,{SPACE} + # Common window strings STR_LIST_FILTER_TITLE :{BLACK}Filter: STR_LIST_FILTER_OSKTITLE :{BLACK}Enter one or more keywords to filter the list for diff --git a/src/network/network_content_gui.cpp b/src/network/network_content_gui.cpp index 57abe51ca22..a9d97691dd9 100644 --- a/src/network/network_content_gui.cpp +++ b/src/network/network_content_gui.cpp @@ -739,6 +739,7 @@ class NetworkContentListWindow : public Window, ContentCallback { SetDParam(0, this->selected->filesize); tr.top = DrawStringMultiLine(tr, STR_CONTENT_DETAIL_FILESIZE); + std::string_view list_separator = GetListSeparator(); if (!this->selected->dependencies.empty()) { /* List dependencies */ std::string buf; @@ -749,7 +750,7 @@ class NetworkContentListWindow : public Window, ContentCallback { const ContentInfo *ci = *iter; if (ci->id != cid) continue; - if (!buf.empty()) buf += ", "; + if (!buf.empty()) buf += list_separator; buf += (*iter)->name; break; } @@ -762,7 +763,7 @@ class NetworkContentListWindow : public Window, ContentCallback { /* List all tags */ std::string buf; for (auto &tag : this->selected->tags) { - if (!buf.empty()) buf += ", "; + if (!buf.empty()) buf += list_separator; buf += tag; } SetDParamStr(0, buf); @@ -778,7 +779,7 @@ class NetworkContentListWindow : public Window, ContentCallback { for (const ContentInfo *ci : tree) { if (ci == this->selected || ci->state != ContentInfo::SELECTED) continue; - if (!buf.empty()) buf += ", "; + if (!buf.empty()) buf += list_separator; buf += ci->name; } if (!buf.empty()) { diff --git a/src/roadveh_gui.cpp b/src/roadveh_gui.cpp index a548b5a4584..026c98a5b3a 100644 --- a/src/roadveh_gui.cpp +++ b/src/roadveh_gui.cpp @@ -51,12 +51,13 @@ void DrawRoadVehDetails(const Vehicle *v, const Rect &r) } std::string capacity = GetString(STR_VEHICLE_DETAILS_TRAIN_ARTICULATED_RV_CAPACITY); + std::string_view list_separator = GetListSeparator(); bool first = true; for (const CargoSpec *cs : _sorted_cargo_specs) { CargoID cid = cs->Index(); if (max_cargo[cid] > 0) { - if (!first) capacity += ", "; + if (!first) capacity += list_separator; SetDParam(0, cid); SetDParam(1, max_cargo[cid]); diff --git a/src/strings.cpp b/src/strings.cpp index d95ff909b3c..6514a50a42b 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -214,12 +214,22 @@ struct LoadedLanguagePack { std::array langtab_num; ///< Offset into langpack offs std::array langtab_start; ///< Offset into langpack offs + + std::string list_separator; ///< Current list separator string. }; static LoadedLanguagePack _langpack; static bool _scan_for_gender_data = false; ///< Are we scanning for the gender of the current string? (instead of formatting it) +/** + * Get the list separator string for the current language. + * @returns string containing list separator to use. + */ +std::string_view GetListSeparator() +{ + return _langpack.list_separator; +} const char *GetStringPtr(StringID string) { @@ -1311,6 +1321,7 @@ static void FormatString(StringBuilder &builder, const char *str_arg, StringPara CargoTypes cmask = args.GetNextParameter(); bool first = true; + std::string_view list_separator = GetListSeparator(); for (const auto &cs : _sorted_cargo_specs) { if (!HasBit(cmask, cs->Index())) continue; @@ -1318,7 +1329,7 @@ static void FormatString(StringBuilder &builder, const char *str_arg, StringPara first = false; } else { /* Add a comma if this is not the first item */ - builder += ", "; + builder += list_separator; } GetStringWithArgs(builder, cs->name, args, next_substr_case_index, game_script); @@ -1964,6 +1975,7 @@ bool ReadLanguagePack(const LanguageMetadata *lang) _current_text_dir = (TextDirection)_current_language->text_dir; _config_language_file = FS2OTTD(_current_language->file.filename()); SetCurrentGrfLangID(_current_language->newgrflangid); + _langpack.list_separator = GetString(STR_LIST_SEPARATOR); #ifdef _WIN32 extern void Win32SetCurrentLocaleName(std::string iso_code); diff --git a/src/strings_func.h b/src/strings_func.h index 8dcd4103199..43076207a52 100644 --- a/src/strings_func.h +++ b/src/strings_func.h @@ -109,6 +109,7 @@ extern TextDirection _current_text_dir; ///< Text direction of the currently sel void InitializeLanguagePacks(); const char *GetCurrentLanguageIsoCode(); +std::string_view GetListSeparator(); /** * A searcher for missing glyphs. diff --git a/src/table/strgen_tables.h b/src/table/strgen_tables.h index badab707bd6..6f9a3310d35 100644 --- a/src/table/strgen_tables.h +++ b/src/table/strgen_tables.h @@ -127,6 +127,7 @@ static const CmdStruct _cmd_structs[] = { {"COMPANY_NUM", EmitSingleChar, SCC_COMPANY_NUM, 1, -1, C_NONE}, {"PRESIDENT_NAME", EmitSingleChar, SCC_PRESIDENT_NAME, 1, -1, C_NONE | C_GENDER}, + {"SPACE", EmitSingleChar, ' ', 0, -1, C_DONTCOUNT}, {"", EmitSingleChar, '\n', 0, -1, C_DONTCOUNT}, {"{", EmitSingleChar, '{', 0, -1, C_DONTCOUNT}, {"UP_ARROW", EmitSingleChar, SCC_UP_ARROW, 0, -1, C_DONTCOUNT},