From e7c6525d476145fdd16be772c3bd2fa4782d9bf5 Mon Sep 17 00:00:00 2001 From: y5nw <37980625+y5nw@users.noreply.github.com> Date: Sat, 2 Mar 2024 21:20:43 +0100 Subject: [PATCH] Move client-side language code fetching into a single function --- src/gettext.cpp | 26 ++++++++++++++++++++++++++ src/gettext.h | 13 +++++++++++++ src/network/clientpackethandler.cpp | 8 +------- src/script/lua_api/l_client.cpp | 5 +---- src/script/lua_api/l_mainmenu.cpp | 9 ++------- 5 files changed, 43 insertions(+), 18 deletions(-) diff --git a/src/gettext.cpp b/src/gettext.cpp index 7cb4a7763b..b9af09f1d4 100644 --- a/src/gettext.cpp +++ b/src/gettext.cpp @@ -6,6 +6,7 @@ #include #include #include "gettext.h" +#include "util/langcode.h" #include "util/string.h" #include "porting.h" #include "log.h" @@ -146,6 +147,10 @@ static void MSVC_LocaleWorkaround(int argc, char* argv[]) #endif +static std::string configured_locale; +static std::vector effective_locale; +static std::string effective_locale_string; + /******************************************************************************/ void init_gettext(const char *path, const std::string &configured_language, int argc, char *argv[]) @@ -223,6 +228,17 @@ void init_gettext(const char *path, const std::string &configured_language, setlocale(LC_ALL, ""); #endif // if USE_GETTEXT + // Set up locale for in-game translations + configured_locale = configured_language; + if (configured_locale.empty()) { + if (auto lang = getenv("LANGUAGE"); lang && *lang) + configured_locale = lang; + else + configured_locale = getenv("LANG"); + } + effective_locale = parse_language_list(utf8_to_wide(configured_locale)); + effective_locale_string = wide_to_utf8(str_join(effective_locale, L":")); + /* no matter what locale is used we need number format to be "C" */ /* to ensure formspec parameters are evaluated correctly! */ @@ -230,3 +246,13 @@ void init_gettext(const char *path, const std::string &configured_language, infostream << "Message locale is now set to: " << setlocale(LC_ALL, 0) << std::endl; } + +const std::vector &get_effective_locale() +{ + return effective_locale; +} + +const std::string &get_client_language_code() +{ + return effective_locale_string; +} diff --git a/src/gettext.h b/src/gettext.h index a58dc93025..5dc983f810 100644 --- a/src/gettext.h +++ b/src/gettext.h @@ -4,6 +4,7 @@ #pragma once +#include #include "config.h" // for USE_GETTEXT #include "porting.h" #include "util/string.h" @@ -103,3 +104,15 @@ inline std::string fmtgettext(const char *format, Args&&... args) return buf; } + +/** + * Returns the effective locale setting for in-game translations. + * @return A vector of language codes based on priority. + */ +const std::vector &get_effective_locale(); + +/** + * Returns the effective locale setting for in-game translations. + * @return A string of the expanded language code. + */ +const std::string &get_client_language_code(); diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index 78027fbac6..2f17fc866d 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -141,13 +141,7 @@ void Client::handleCommand_AuthAccept(NetworkPacket* pkt) << m_recommended_send_interval<getContentTranslations(path, domain, lang); string = wide_to_utf8(translate_string(utf8_to_wide(string), translations)); @@ -879,10 +877,7 @@ int ModApiMainMenu::l_download_file(lua_State *L) /******************************************************************************/ int ModApiMainMenu::l_get_language(lua_State *L) { - std::string lang = gettext("LANG_CODE"); - if (lang == "LANG_CODE") - lang = ""; - + std::string lang = get_client_language_code(); lua_pushstring(L, lang.c_str()); return 1; }