mirror of https://github.com/OpenTTD/OpenTTD
Codechange: use std::string_view for locales
parent
96fd291693
commit
d037dbb42a
|
@ -151,7 +151,7 @@ void OSOpenBrowser(const std::string &url)
|
||||||
/**
|
/**
|
||||||
* Determine and return the current user's locale.
|
* Determine and return the current user's locale.
|
||||||
*/
|
*/
|
||||||
const char *GetCurrentLocale(const char *)
|
std::optional<std::string_view> GetCurrentLocale(const char *)
|
||||||
{
|
{
|
||||||
static char retbuf[32] = { '\0' };
|
static char retbuf[32] = { '\0' };
|
||||||
NSUserDefaults *defs = [ NSUserDefaults standardUserDefaults ];
|
NSUserDefaults *defs = [ NSUserDefaults standardUserDefaults ];
|
||||||
|
|
|
@ -89,7 +89,7 @@ bool FiosIsHiddenFile(const std::filesystem::path &path)
|
||||||
#include "../../debug.h"
|
#include "../../debug.h"
|
||||||
#include "../../string_func.h"
|
#include "../../string_func.h"
|
||||||
|
|
||||||
const char *GetCurrentLocale(const char *param);
|
std::optional<std::string_view> GetCurrentLocale(const char *param);
|
||||||
|
|
||||||
#define INTERNALCODE "UTF-8"
|
#define INTERNALCODE "UTF-8"
|
||||||
|
|
||||||
|
@ -98,16 +98,17 @@ const char *GetCurrentLocale(const char *param);
|
||||||
* variables. MacOSX is hardcoded, other OS's are dynamic. If no suitable
|
* variables. MacOSX is hardcoded, other OS's are dynamic. If no suitable
|
||||||
* locale can be found, don't do any conversion ""
|
* locale can be found, don't do any conversion ""
|
||||||
*/
|
*/
|
||||||
static const char *GetLocalCode()
|
static std::string_view GetLocalCode()
|
||||||
{
|
{
|
||||||
#if defined(__APPLE__)
|
#if defined(__APPLE__)
|
||||||
return "UTF-8-MAC";
|
return "UTF-8-MAC";
|
||||||
#else
|
#else
|
||||||
/* Strip locale (eg en_US.UTF-8) to only have UTF-8 */
|
/* Strip locale (eg en_US.UTF-8) to only have UTF-8 */
|
||||||
const char *locale = GetCurrentLocale("LC_CTYPE");
|
auto locale = GetCurrentLocale("LC_CTYPE");
|
||||||
if (locale != nullptr) locale = strchr(locale, '.');
|
if (!locale.has_value()) return "";
|
||||||
|
auto pos = locale->find('.');
|
||||||
return (locale == nullptr) ? "" : locale + 1;
|
if (pos == std::string_view::npos) return "";
|
||||||
|
return locale->substr(pos + 1);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,8 +152,8 @@ std::string OTTD2FS(std::string_view name)
|
||||||
{
|
{
|
||||||
static iconv_t convd = (iconv_t)(-1);
|
static iconv_t convd = (iconv_t)(-1);
|
||||||
if (convd == (iconv_t)(-1)) {
|
if (convd == (iconv_t)(-1)) {
|
||||||
const char *env = GetLocalCode();
|
std::string env{GetLocalCode()};
|
||||||
convd = iconv_open(env, INTERNALCODE);
|
convd = iconv_open(env.c_str(), INTERNALCODE);
|
||||||
if (convd == (iconv_t)(-1)) {
|
if (convd == (iconv_t)(-1)) {
|
||||||
Debug(misc, 0, "[iconv] conversion from codeset '{}' to '{}' unsupported", INTERNALCODE, env);
|
Debug(misc, 0, "[iconv] conversion from codeset '{}' to '{}' unsupported", INTERNALCODE, env);
|
||||||
return std::string{name};
|
return std::string{name};
|
||||||
|
@ -171,8 +172,8 @@ std::string FS2OTTD(std::string_view name)
|
||||||
{
|
{
|
||||||
static iconv_t convd = (iconv_t)(-1);
|
static iconv_t convd = (iconv_t)(-1);
|
||||||
if (convd == (iconv_t)(-1)) {
|
if (convd == (iconv_t)(-1)) {
|
||||||
const char *env = GetLocalCode();
|
std::string env{GetLocalCode()};
|
||||||
convd = iconv_open(INTERNALCODE, env);
|
convd = iconv_open(INTERNALCODE, env.c_str());
|
||||||
if (convd == (iconv_t)(-1)) {
|
if (convd == (iconv_t)(-1)) {
|
||||||
Debug(misc, 0, "[iconv] conversion from codeset '{}' to '{}' unsupported", env, INTERNALCODE);
|
Debug(misc, 0, "[iconv] conversion from codeset '{}' to '{}' unsupported", env, INTERNALCODE);
|
||||||
return std::string{name};
|
return std::string{name};
|
||||||
|
|
|
@ -395,7 +395,7 @@ wchar_t *convert_to_fs(std::string_view src, std::span<wchar_t> dst_buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Determine the current user's locale. */
|
/** Determine the current user's locale. */
|
||||||
const char *GetCurrentLocale(const char *)
|
std::optional<std::string_view> GetCurrentLocale(const char *)
|
||||||
{
|
{
|
||||||
const LANGID userUiLang = GetUserDefaultUILanguage();
|
const LANGID userUiLang = GetUserDefaultUILanguage();
|
||||||
const LCID userUiLocale = MAKELCID(userUiLang, SORT_DEFAULT);
|
const LCID userUiLocale = MAKELCID(userUiLang, SORT_DEFAULT);
|
||||||
|
|
|
@ -2083,25 +2083,23 @@ bool ReadLanguagePack(const LanguageMetadata *lang)
|
||||||
* set. Pass nullptr if you don't want additional checks.
|
* set. Pass nullptr if you don't want additional checks.
|
||||||
* @return return string containing current charset, or nullptr if not-determinable
|
* @return return string containing current charset, or nullptr if not-determinable
|
||||||
*/
|
*/
|
||||||
const char *GetCurrentLocale(const char *param)
|
std::optional<std::string_view> GetCurrentLocale(const char *param)
|
||||||
{
|
{
|
||||||
const char *env;
|
auto env = GetEnv("LANGUAGE");
|
||||||
|
if (env.has_value()) return env;
|
||||||
|
|
||||||
env = std::getenv("LANGUAGE");
|
env = GetEnv("LC_ALL");
|
||||||
if (env != nullptr) return env;
|
if (env.has_value()) return env;
|
||||||
|
|
||||||
env = std::getenv("LC_ALL");
|
|
||||||
if (env != nullptr) return env;
|
|
||||||
|
|
||||||
if (param != nullptr) {
|
if (param != nullptr) {
|
||||||
env = std::getenv(param);
|
env = GetEnv(param);
|
||||||
if (env != nullptr) return env;
|
if (env.has_value()) return env;
|
||||||
}
|
}
|
||||||
|
|
||||||
return std::getenv("LANG");
|
return GetEnv("LANG");
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
const char *GetCurrentLocale(const char *param);
|
std::optional<std::string_view> GetCurrentLocale(const char *param);
|
||||||
#endif /* !(defined(_WIN32) || defined(__APPLE__)) */
|
#endif /* !(defined(_WIN32) || defined(__APPLE__)) */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2182,8 +2180,8 @@ void InitializeLanguagePacks()
|
||||||
if (_languages.empty()) UserError("No available language packs (invalid versions?)");
|
if (_languages.empty()) UserError("No available language packs (invalid versions?)");
|
||||||
|
|
||||||
/* Acquire the locale of the current system */
|
/* Acquire the locale of the current system */
|
||||||
const char *lang = GetCurrentLocale("LC_MESSAGES");
|
auto lang = GetCurrentLocale("LC_MESSAGES");
|
||||||
if (lang == nullptr) lang = "en_GB";
|
if (!lang.has_value()) lang = "en_GB";
|
||||||
|
|
||||||
const LanguageMetadata *chosen_language = nullptr; ///< Matching the language in the configuration file or the current locale
|
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
|
const LanguageMetadata *language_fallback = nullptr; ///< Using pt_PT for pt_BR locale when pt_BR is not available
|
||||||
|
@ -2199,13 +2197,14 @@ void InitializeLanguagePacks()
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp (lng.isocode, "en_GB") == 0) en_GB_fallback = &lng;
|
std::string_view iso_code = lng.isocode;
|
||||||
|
if (iso_code == "en_GB") en_GB_fallback = &lng;
|
||||||
|
|
||||||
/* Only auto-pick finished translations */
|
/* Only auto-pick finished translations */
|
||||||
if (!lng.IsReasonablyFinished()) continue;
|
if (!lng.IsReasonablyFinished()) continue;
|
||||||
|
|
||||||
if (strncmp(lng.isocode, lang, 5) == 0) chosen_language = &lng;
|
if (iso_code.starts_with(lang->substr(0, 5))) chosen_language = &lng;
|
||||||
if (strncmp(lng.isocode, lang, 2) == 0) language_fallback = &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.
|
/* We haven't found the language in the config nor the one in the locale.
|
||||||
|
|
Loading…
Reference in New Issue