1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-09-30 19:22:14 +00:00

Rework translate_string and related

This commit is contained in:
y5nw 2025-02-24 16:32:09 +01:00 committed by y5nw
parent 027a2a67b7
commit e8370769a9
7 changed files with 50 additions and 41 deletions

View file

@ -229,42 +229,46 @@ std::string findLocaleFileInMods(const std::string &path, const std::string &fil
/******************************************************************************/ /******************************************************************************/
Translations *GUIEngine::getContentTranslations(const std::string &path, Translations *GUIEngine::getContentTranslations(const std::string &path,
const std::string &domain, const std::string &lang_code) const std::string &domain, const std::vector<std::wstring> &lang)
{ {
if (domain.empty() || lang_code.empty()) if (domain.empty() || lang.empty())
return nullptr; return nullptr;
std::string filename_no_ext = domain + "." + lang_code; std::string key = path + DIR_DELIM "locale" DIR_DELIM + domain;
std::string key = path + DIR_DELIM "locale" DIR_DELIM + filename_no_ext;
if (key == m_last_translations_key) if (key == m_last_translations_key)
return &m_last_translations; return &m_last_translations;
std::string trans_path = key; Translations translations;
for (const auto &lang_code: lang) {
switch (getContentType(path)) { auto utf8_lang_code = wide_to_utf8(lang_code);
case ContentType::GAME: std::string filename_no_ext = domain + "." + utf8_lang_code;
trans_path = findLocaleFileInMods(path + DIR_DELIM "mods" DIR_DELIM, auto trans_path = key + "." + utf8_lang_code;
filename_no_ext); switch (getContentType(path)) {
break; case ContentType::GAME:
case ContentType::MODPACK: trans_path = findLocaleFileInMods(path + DIR_DELIM "mods" DIR_DELIM,
trans_path = findLocaleFileInMods(path, filename_no_ext); filename_no_ext);
break; break;
default: case ContentType::MODPACK:
trans_path = findLocaleFileWithExtension(trans_path); trans_path = findLocaleFileInMods(path, filename_no_ext);
break; break;
default:
trans_path = findLocaleFileWithExtension(trans_path);
break;
}
if (trans_path.empty())
continue;
std::string data;
if (fs::ReadFile(trans_path, data)) {
translations.loadTranslation(fs::GetFilenameFromPath(trans_path.c_str()), data);
}
} }
if (trans_path.empty()) if (translations.empty())
return nullptr; return nullptr;
m_last_translations_key = key; m_last_translations_key = key;
m_last_translations = {}; m_last_translations = std::move(translations);
std::string data;
if (fs::ReadFile(trans_path, data)) {
m_last_translations.loadTranslation(fs::GetFilenameFromPath(trans_path.c_str()), data);
}
return &m_last_translations; return &m_last_translations;
} }

View file

@ -156,7 +156,7 @@ public:
* change with the next call to `getContentTranslations`. * change with the next call to `getContentTranslations`.
* */ * */
Translations *getContentTranslations(const std::string &path, Translations *getContentTranslations(const std::string &path,
const std::string &domain, const std::string &lang_code); const std::string &domain, const std::vector<std::wstring> &lang_code);
private: private:
std::string m_last_translations_key; std::string m_last_translations_key;

View file

@ -1376,9 +1376,10 @@ int ModApiEnv::l_get_translated_string(lua_State * L)
std::string lang_code = luaL_checkstring(L, 1); std::string lang_code = luaL_checkstring(L, 1);
std::string string = luaL_checkstring(L, 2); std::string string = luaL_checkstring(L, 2);
auto lang = str_split(utf8_to_wide(lang_code), L':');
auto *translations = getServer(L)->getTranslationLanguage(lang_code); auto *translations = getServer(L)->getTranslationLanguage(lang_code);
string = wide_to_utf8(translate_string(utf8_to_wide(string), translations)); string = wide_to_utf8(translate_string(utf8_to_wide(string), lang, translations));
lua_pushstring(L, string.c_str()); lua_pushstring(L, string.c_str());
return 1; return 1;
} }

View file

@ -527,10 +527,10 @@ 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 = get_client_language_code(); auto lang = get_effective_locale();
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), lang, translations));
lua_pushstring(L, string.c_str()); lua_pushstring(L, string.c_str());
return 1; return 1;
} }

View file

@ -30,11 +30,14 @@ public:
{ {
return getFileLanguage(filename) != ""; return getFileLanguage(filename) != "";
} }
// for testing
inline size_t size() inline size_t size()
{ {
return m_translations.size() + m_plural_translations.size()/2; return m_translations.size() + m_plural_translations.size()/2;
} }
inline bool empty()
{
return size() == 0;
}
#ifndef SERVER #ifndef SERVER
const std::wstring &getTranslation( const std::wstring &getTranslation(

View file

@ -7,6 +7,7 @@
#include "numeric.h" #include "numeric.h"
#include "log.h" #include "log.h"
#include "gettext.h"
#include "hex.h" #include "hex.h"
#include "porting.h" #include "porting.h"
#include "translation.h" #include "translation.h"
@ -654,9 +655,9 @@ std::string wrap_rows(std::string_view from, unsigned row_len, bool has_color_co
*/ */
static void translate_all(std::wstring_view s, size_t &i, static void translate_all(std::wstring_view s, size_t &i,
Translations *translations, std::wstring &res); const std::vector<std::wstring> &lang, Translations *translations, std::wstring &res);
static void translate_string(std::wstring_view s, Translations *translations, static void translate_string(std::wstring_view s, const std::vector<std::wstring> &lang, Translations *translations,
const std::wstring &textdomain, size_t &i, std::wstring &res, const std::wstring &textdomain, size_t &i, std::wstring &res,
bool use_plural, unsigned long int number) bool use_plural, unsigned long int number)
{ {
@ -716,7 +717,7 @@ static void translate_string(std::wstring_view s, Translations *translations,
if (arg_number >= 10) { if (arg_number >= 10) {
errorstream << "Ignoring too many arguments to translation" << std::endl; errorstream << "Ignoring too many arguments to translation" << std::endl;
std::wstring arg; std::wstring arg;
translate_all(s, i, translations, arg); translate_all(s, i, lang, translations, arg);
args.push_back(arg); args.push_back(arg);
continue; continue;
} }
@ -724,7 +725,7 @@ static void translate_string(std::wstring_view s, Translations *translations,
output += std::to_wstring(arg_number); output += std::to_wstring(arg_number);
++arg_number; ++arg_number;
std::wstring arg; std::wstring arg;
translate_all(s, i, translations, arg); translate_all(s, i, lang, translations, arg);
args.push_back(std::move(arg)); args.push_back(std::move(arg));
} else { } else {
// This is an escape sequence *inside* the template string to translate itself. // This is an escape sequence *inside* the template string to translate itself.
@ -739,10 +740,10 @@ static void translate_string(std::wstring_view s, Translations *translations,
if (translations != nullptr) { if (translations != nullptr) {
if (use_plural) if (use_plural)
toutput = translations->getPluralTranslation( toutput = translations->getPluralTranslation(
textdomain, output, number); lang, textdomain, output, number);
else else
toutput = translations->getTranslation( toutput = translations->getTranslation(
textdomain, output); lang, textdomain, output);
} else { } else {
toutput = output; toutput = output;
} }
@ -780,7 +781,7 @@ static void translate_string(std::wstring_view s, Translations *translations,
} }
static void translate_all(std::wstring_view s, size_t &i, static void translate_all(std::wstring_view s, size_t &i,
Translations *translations, std::wstring &res) const std::vector<std::wstring> &lang, Translations *translations, std::wstring &res)
{ {
res.clear(); res.clear();
res.reserve(s.length()); res.reserve(s.length());
@ -858,7 +859,7 @@ static void translate_all(std::wstring_view s, size_t &i,
} }
} }
std::wstring translated; std::wstring translated;
translate_string(s, translations, textdomain, i, translated, use_plural, number); translate_string(s, lang, translations, textdomain, i, translated, use_plural, number);
res.append(translated); res.append(translated);
} else { } else {
// Another escape sequence, such as colors. Preserve it. // Another escape sequence, such as colors. Preserve it.
@ -868,17 +869,17 @@ static void translate_all(std::wstring_view s, size_t &i,
} }
// Translate string server side // Translate string server side
std::wstring translate_string(std::wstring_view s, Translations *translations) std::wstring translate_string(std::wstring_view s, const std::vector<std::wstring> &lang, Translations *translations)
{ {
size_t i = 0; size_t i = 0;
std::wstring res; std::wstring res;
translate_all(s, i, translations, res); translate_all(s, i, lang, translations, res);
return res; return res;
} }
std::wstring translate_string(std::wstring_view s) std::wstring translate_string(std::wstring_view s)
{ {
return translate_string(s, g_client_translations); return translate_string(s, get_effective_locale(), g_client_translations);
} }
static const std::array<std::wstring_view, 30> disallowed_dir_names = { static const std::array<std::wstring_view, 30> disallowed_dir_names = {

View file

@ -655,7 +655,7 @@ std::vector<std::basic_string<T> > split(const std::basic_string<T> &s, T delim)
} }
[[nodiscard]] [[nodiscard]]
std::wstring translate_string(std::wstring_view s, Translations *translations); std::wstring translate_string(std::wstring_view s, const std::vector<std::wstring> &lang, Translations *translations);
[[nodiscard]] [[nodiscard]]
std::wstring translate_string(std::wstring_view s); std::wstring translate_string(std::wstring_view s);