mirror of
https://github.com/luanti-org/luanti.git
synced 2025-09-30 19:22:14 +00:00
Move client-side language code fetching into a single function
This commit is contained in:
parent
2c91ba2513
commit
e7c6525d47
5 changed files with 43 additions and 18 deletions
|
@ -6,6 +6,7 @@
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "gettext.h"
|
#include "gettext.h"
|
||||||
|
#include "util/langcode.h"
|
||||||
#include "util/string.h"
|
#include "util/string.h"
|
||||||
#include "porting.h"
|
#include "porting.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
@ -146,6 +147,10 @@ static void MSVC_LocaleWorkaround(int argc, char* argv[])
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static std::string configured_locale;
|
||||||
|
static std::vector<std::wstring> effective_locale;
|
||||||
|
static std::string effective_locale_string;
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
void init_gettext(const char *path, const std::string &configured_language,
|
void init_gettext(const char *path, const std::string &configured_language,
|
||||||
int argc, char *argv[])
|
int argc, char *argv[])
|
||||||
|
@ -223,6 +228,17 @@ void init_gettext(const char *path, const std::string &configured_language,
|
||||||
setlocale(LC_ALL, "");
|
setlocale(LC_ALL, "");
|
||||||
#endif // if USE_GETTEXT
|
#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" */
|
/* no matter what locale is used we need number format to be "C" */
|
||||||
/* to ensure formspec parameters are evaluated correctly! */
|
/* 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: "
|
infostream << "Message locale is now set to: "
|
||||||
<< setlocale(LC_ALL, 0) << std::endl;
|
<< setlocale(LC_ALL, 0) << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::vector<std::wstring> &get_effective_locale()
|
||||||
|
{
|
||||||
|
return effective_locale;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string &get_client_language_code()
|
||||||
|
{
|
||||||
|
return effective_locale_string;
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
#include "config.h" // for USE_GETTEXT
|
#include "config.h" // for USE_GETTEXT
|
||||||
#include "porting.h"
|
#include "porting.h"
|
||||||
#include "util/string.h"
|
#include "util/string.h"
|
||||||
|
@ -103,3 +104,15 @@ inline std::string fmtgettext(const char *format, Args&&... args)
|
||||||
|
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the effective locale setting for in-game translations.
|
||||||
|
* @return A vector of language codes based on priority.
|
||||||
|
*/
|
||||||
|
const std::vector<std::wstring> &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();
|
||||||
|
|
|
@ -141,13 +141,7 @@ void Client::handleCommand_AuthAccept(NetworkPacket* pkt)
|
||||||
<< m_recommended_send_interval<<std::endl;
|
<< m_recommended_send_interval<<std::endl;
|
||||||
|
|
||||||
// Reply to server
|
// Reply to server
|
||||||
/*~ DO NOT TRANSLATE THIS LITERALLY!
|
std::string lang = get_client_language_code();
|
||||||
This is a special string which needs to contain the translation's
|
|
||||||
language code (e.g. "de" for German). */
|
|
||||||
std::string lang = gettext("LANG_CODE");
|
|
||||||
if (lang == "LANG_CODE")
|
|
||||||
lang.clear();
|
|
||||||
|
|
||||||
NetworkPacket resp_pkt(TOSERVER_INIT2, sizeof(u16) + lang.size());
|
NetworkPacket resp_pkt(TOSERVER_INIT2, sizeof(u16) + lang.size());
|
||||||
resp_pkt << lang;
|
resp_pkt << lang;
|
||||||
Send(&resp_pkt);
|
Send(&resp_pkt);
|
||||||
|
|
|
@ -177,10 +177,7 @@ int ModApiClient::l_get_language(lua_State *L)
|
||||||
#else
|
#else
|
||||||
char *locale = setlocale(LC_MESSAGES, NULL);
|
char *locale = setlocale(LC_MESSAGES, NULL);
|
||||||
#endif
|
#endif
|
||||||
std::string lang = gettext("LANG_CODE");
|
std::string lang = get_client_language_code();
|
||||||
if (lang == "LANG_CODE")
|
|
||||||
lang.clear();
|
|
||||||
|
|
||||||
lua_pushstring(L, locale);
|
lua_pushstring(L, locale);
|
||||||
lua_pushstring(L, lang.c_str());
|
lua_pushstring(L, lang.c_str());
|
||||||
return 2;
|
return 2;
|
||||||
|
|
|
@ -527,9 +527,7 @@ int ModApiMainMenu::l_get_content_translation(lua_State *L)
|
||||||
std::string path = luaL_checkstring(L, 1);
|
std::string path = luaL_checkstring(L, 1);
|
||||||
std::string domain = luaL_checkstring(L, 2);
|
std::string domain = luaL_checkstring(L, 2);
|
||||||
std::string string = luaL_checkstring(L, 3);
|
std::string string = luaL_checkstring(L, 3);
|
||||||
std::string lang = gettext("LANG_CODE");
|
std::string lang = get_client_language_code();
|
||||||
if (lang == "LANG_CODE")
|
|
||||||
lang = "";
|
|
||||||
|
|
||||||
auto *translations = engine->getContentTranslations(path, domain, lang);
|
auto *translations = engine->getContentTranslations(path, domain, lang);
|
||||||
string = wide_to_utf8(translate_string(utf8_to_wide(string), translations));
|
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)
|
int ModApiMainMenu::l_get_language(lua_State *L)
|
||||||
{
|
{
|
||||||
std::string lang = gettext("LANG_CODE");
|
std::string lang = get_client_language_code();
|
||||||
if (lang == "LANG_CODE")
|
|
||||||
lang = "";
|
|
||||||
|
|
||||||
lua_pushstring(L, lang.c_str());
|
lua_pushstring(L, lang.c_str());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue