1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-16 17:02:37 +01:00

Codechange: Make GetCurrentLocale return a std::string instead of a reference to a static buffer.

For win32 this is even a fix, because the static buffer was only updated once. Later calls discarded the determined locale.
This commit is contained in:
frosch
2025-05-04 19:04:15 +02:00
committed by frosch
parent 0d5b3ebd7f
commit 75a775e59d
4 changed files with 25 additions and 20 deletions

View File

@@ -2083,23 +2083,26 @@ bool ReadLanguagePack(const LanguageMetadata *lang)
* set. Pass nullptr if you don't want additional checks.
* @return return string containing current charset, or nullptr if not-determinable
*/
std::optional<std::string_view> GetCurrentLocale(const char *param)
std::optional<std::string> GetCurrentLocale(const char *param)
{
auto env = GetEnv("LANGUAGE");
if (env.has_value()) return env;
if (env.has_value()) return std::string{*env};
env = GetEnv("LC_ALL");
if (env.has_value()) return env;
if (env.has_value()) return std::string{*env};
if (param != nullptr) {
env = GetEnv(param);
if (env.has_value()) return env;
if (env.has_value()) return std::string{*env};
}
return GetEnv("LANG");
env = GetEnv("LANG");
if (env.has_value()) return std::string{*env};
return std::nullopt;
}
#else
std::optional<std::string_view> GetCurrentLocale(const char *param);
std::optional<std::string> GetCurrentLocale(const char *param);
#endif /* !(defined(_WIN32) || defined(__APPLE__)) */
/**
@@ -2180,8 +2183,8 @@ void InitializeLanguagePacks()
if (_languages.empty()) UserError("No available language packs (invalid versions?)");
/* Acquire the locale of the current system */
auto lang = GetCurrentLocale("LC_MESSAGES");
if (!lang.has_value()) lang = "en_GB";
auto str_lang = GetCurrentLocale("LC_MESSAGES");
std::string_view lang = str_lang.has_value() ? std::string_view{*str_lang} : "en_GB";
const LanguageMetadata *chosen_language = nullptr; ///< Matching the language in the configuration file or the current locale
const LanguageMetadata *language_fallback = nullptr; ///< Using pt_PT for pt_BR locale when pt_BR is not available
@@ -2203,8 +2206,8 @@ void InitializeLanguagePacks()
/* Only auto-pick finished translations */
if (!lng.IsReasonablyFinished()) continue;
if (iso_code.starts_with(lang->substr(0, 5))) chosen_language = &lng;
if (iso_code.starts_with(lang->substr(0, 2))) language_fallback = &lng;
if (iso_code.starts_with(lang.substr(0, 5))) chosen_language = &lng;
if (iso_code.starts_with(lang.substr(0, 2))) language_fallback = &lng;
}
/* We haven't found the language in the config nor the one in the locale.