Skip to content

Commit

Permalink
Add: Translatable list separator. (#13149)
Browse files Browse the repository at this point in the history
Some languages should use a separator other than ", " to separate list items, so it is now a translatable string.
  • Loading branch information
PeterN authored Dec 6, 2024
1 parent ef76f0e commit cba329d
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/cargotype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ uint64_t CargoSpec::WeightOfNUnitsInTrain(uint32_t n) const
*/
std::optional<std::string> 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);
Expand All @@ -277,7 +279,7 @@ std::optional<std::string> 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 */
Expand Down
2 changes: 2 additions & 0 deletions src/lang/english.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/network/network_content_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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);
Expand All @@ -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()) {
Expand Down
3 changes: 2 additions & 1 deletion src/roadveh_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
14 changes: 13 additions & 1 deletion src/strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,22 @@ struct LoadedLanguagePack {

std::array<uint, TEXT_TAB_END> langtab_num; ///< Offset into langpack offs
std::array<uint, TEXT_TAB_END> 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)
{
Expand Down Expand Up @@ -1311,14 +1321,15 @@ static void FormatString(StringBuilder &builder, const char *str_arg, StringPara
CargoTypes cmask = args.GetNextParameter<CargoTypes>();
bool first = true;

std::string_view list_separator = GetListSeparator();
for (const auto &cs : _sorted_cargo_specs) {
if (!HasBit(cmask, cs->Index())) continue;

if (first) {
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);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/strings_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/table/strgen_tables.h
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit cba329d

Please sign in to comment.