This commit is contained in:
rubenwardy 2024-04-03 18:19:18 +02:00 committed by GitHub
commit f9a1b82592
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 204 additions and 120 deletions

View File

@ -74,6 +74,19 @@ local REASON_UPDATE = "update"
local REASON_DEPENDENCY = "dependency" local REASON_DEPENDENCY = "dependency"
function reset_contentdb()
store.load_ok = false
store.loading = false
store.load_error = false
store.packages = {}
store.packages_full = {}
store.packages_full_unordered = {}
store.aliases = {}
search_string = ""
cur_page = 1
end
local function get_download_url(package, reason) local function get_download_url(package, reason)
local base_url = core.settings:get("contentdb_url") local base_url = core.settings:get("contentdb_url")
local ret = base_url .. ("/packages/%s/releases/%d/download/"):format( local ret = base_url .. ("/packages/%s/releases/%d/download/"):format(

View File

@ -163,6 +163,9 @@ function make.enum(setting)
self.resettable = core.settings:has(setting.name) self.resettable = core.settings:has(setting.name)
local labels = setting.option_labels or {} local labels = setting.option_labels or {}
if type(labels) == "function" then
labels = labels(self, setting, value)
end
local items = {} local items = {}
for i, option in ipairs(setting.values) do for i, option in ipairs(setting.values) do
@ -187,6 +190,14 @@ function make.enum(setting)
end end
core.settings:set(setting.name, value) core.settings:set(setting.name, value)
if setting.name == "language" then
-- Refresh content to update translations
pkgmgr.refresh_globals()
pkgmgr.update_gamelist()
reset_contentdb()
end
return true return true
end, end,
} }

View File

@ -15,6 +15,11 @@
--with this program; if not, write to the Free Software Foundation, Inc., --with this program; if not, write to the Free Software Foundation, Inc.,
--51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-- Capture translations without translating
local function gettext_lazy(x)
return x
end
local component_funcs = dofile(core.get_mainmenu_path() .. DIR_DELIM .. local component_funcs = dofile(core.get_mainmenu_path() .. DIR_DELIM ..
"settings" .. DIR_DELIM .. "components.lua") "settings" .. DIR_DELIM .. "components.lua")
@ -65,23 +70,23 @@ local change_keys = {
add_page({ add_page({
id = "accessibility", id = "accessibility",
title = fgettext_ne("Accessibility"), title = gettext_lazy("Accessibility"),
content = { content = {
"language", "language",
{ heading = fgettext_ne("General") }, { heading = gettext_lazy("General") },
"font_size", "font_size",
"chat_font_size", "chat_font_size",
"gui_scaling", "gui_scaling",
"hud_scaling", "hud_scaling",
"show_nametag_backgrounds", "show_nametag_backgrounds",
{ heading = fgettext_ne("Chat") }, { heading = gettext_lazy("Chat") },
"console_height", "console_height",
"console_alpha", "console_alpha",
"console_color", "console_color",
{ heading = fgettext_ne("Controls") }, { heading = gettext_lazy("Controls") },
"autojump", "autojump",
"safe_dig_and_place", "safe_dig_and_place",
{ heading = fgettext_ne("Movement") }, { heading = gettext_lazy("Movement") },
"arm_inertia", "arm_inertia",
"view_bobbing_amount", "view_bobbing_amount",
"fall_bobbing_amount", "fall_bobbing_amount",
@ -96,7 +101,7 @@ local function load_settingtypes()
if not page then if not page then
page = add_page({ page = add_page({
id = (section or "general"):lower():gsub(" ", "_"), id = (section or "general"):lower():gsub(" ", "_"),
title = section or fgettext_ne("General"), title = section or gettext_lazy("General"),
section = section, section = section,
content = {}, content = {},
}) })
@ -120,7 +125,7 @@ local function load_settingtypes()
elseif entry.level == 2 then elseif entry.level == 2 then
ensure_page_started() ensure_page_started()
page.content[#page.content + 1] = { page.content[#page.content + 1] = {
heading = fgettext_ne(entry.readable_name or entry.name), heading = entry.readable_name or entry.name,
} }
end end
else else
@ -153,68 +158,70 @@ end
-- These must not be translated, as they need to show in the local -- These must not be translated, as they need to show in the local
-- language no matter the user's current language. -- language no matter the user's current language.
-- This list must be kept in sync with src/unsupported_language_list.txt. -- This list must be kept in sync with src/unsupported_language_list.txt.
get_setting_info("language").option_labels = { get_setting_info("language").option_labels = function()
[""] = fgettext_ne("(Use system language)"), return {
--ar = " [ar]", blacklisted [""] = fgettext_ne("(Use system language)"),
be = "Беларуская [be]", --ar = " [ar]", blacklisted
bg = "Български [bg]", be = "Беларуская [be]",
ca = "Català [ca]", bg = "Български [bg]",
cs = "Česky [cs]", ca = "Català [ca]",
cy = "Cymraeg [cy]", cs = "Česky [cs]",
da = "Dansk [da]", cy = "Cymraeg [cy]",
de = "Deutsch [de]", da = "Dansk [da]",
--dv = " [dv]", blacklisted de = "Deutsch [de]",
el = "Ελληνικά [el]", --dv = " [dv]", blacklisted
en = "English [en]", el = "Ελληνικά [el]",
eo = "Esperanto [eo]", en = "English [en]",
es = "Español [es]", eo = "Esperanto [eo]",
et = "Eesti [et]", es = "Español [es]",
eu = "Euskara [eu]", et = "Eesti [et]",
fi = "Suomi [fi]", eu = "Euskara [eu]",
fil = "Wikang Filipino [fil]", fi = "Suomi [fi]",
fr = "Français [fr]", fil = "Wikang Filipino [fil]",
gd = "Gàidhlig [gd]", fr = "Français [fr]",
gl = "Galego [gl]", gd = "Gàidhlig [gd]",
--he = " [he]", blacklisted gl = "Galego [gl]",
--hi = " [hi]", blacklisted --he = " [he]", blacklisted
hu = "Magyar [hu]", --hi = " [hi]", blacklisted
id = "Bahasa Indonesia [id]", hu = "Magyar [hu]",
it = "Italiano [it]", id = "Bahasa Indonesia [id]",
ja = "日本語 [ja]", it = "Italiano [it]",
jbo = "Lojban [jbo]", ja = "日本語 [ja]",
kk = "Қазақша [kk]", jbo = "Lojban [jbo]",
--kn = " [kn]", blacklisted kk = "Қазақша [kk]",
ko = "한국어 [ko]", --kn = " [kn]", blacklisted
ky = "Kırgızca / Кыргызча [ky]", ko = "한국어 [ko]",
lt = "Lietuvių [lt]", ky = "Kırgızca / Кыргызча [ky]",
lv = "Latviešu [lv]", lt = "Lietuvių [lt]",
mn = "Монгол [mn]", lv = "Latviešu [lv]",
mr = "मराठी [mr]", mn = "Монгол [mn]",
ms = "Bahasa Melayu [ms]", mr = "मराठी [mr]",
--ms_Arab = " [ms_Arab]", blacklisted ms = "Bahasa Melayu [ms]",
nb = "Norsk Bokmål [nb]", --ms_Arab = " [ms_Arab]", blacklisted
nl = "Nederlands [nl]", nb = "Norsk Bokmål [nb]",
nn = "Norsk Nynorsk [nn]", nl = "Nederlands [nl]",
oc = "Occitan [oc]", nn = "Norsk Nynorsk [nn]",
pl = "Polski [pl]", oc = "Occitan [oc]",
pt = "Português [pt]", pl = "Polski [pl]",
pt_BR = "Português do Brasil [pt_BR]", pt = "Português [pt]",
ro = "Română [ro]", pt_BR = "Português do Brasil [pt_BR]",
ru = "Русский [ru]", ro = "Română [ro]",
sk = "Slovenčina [sk]", ru = "Русский [ru]",
sl = "Slovenščina [sl]", sk = "Slovenčina [sk]",
sr_Cyrl = "Српски [sr_Cyrl]", sl = "Slovenščina [sl]",
sr_Latn = "Srpski (Latinica) [sr_Latn]", sr_Cyrl = "Српски [sr_Cyrl]",
sv = "Svenska [sv]", sr_Latn = "Srpski (Latinica) [sr_Latn]",
sw = "Kiswahili [sw]", sv = "Svenska [sv]",
--th = " [th]", blacklisted sw = "Kiswahili [sw]",
tr = "Türkçe [tr]", --th = " [th]", blacklisted
tt = "Tatarça [tt]", tr = "Türkçe [tr]",
uk = "Українська [uk]", tt = "Tatarça [tt]",
vi = "Tiếng Việt [vi]", uk = "Українська [uk]",
zh_CN = "中文 (简体) [zh_CN]", vi = "Tiếng Việt [vi]",
zh_TW = "正體中文 (繁體) [zh_TW]", zh_CN = "中文 (简体) [zh_CN]",
} zh_TW = "正體中文 (繁體) [zh_TW]",
}
end
-- See if setting matches keywords -- See if setting matches keywords
@ -420,7 +427,7 @@ local function build_page_components(page)
elseif item.get_formspec then elseif item.get_formspec then
retval[i] = item retval[i] = item
elseif item.heading then elseif item.heading then
retval[i] = component_funcs.heading(item.heading) retval[i] = component_funcs.heading(fgettext_ne(item.heading))
end end
end end
return retval return retval

View File

@ -116,7 +116,9 @@ end
return { return {
name = "about", name = "about",
caption = fgettext("About"), caption = function()
return fgettext("About")
end,
cbf_formspec = function(tabview, name, tabdata) cbf_formspec = function(tabview, name, tabdata)
local logofile = defaulttexturedir .. "logo.png" local logofile = defaulttexturedir .. "logo.png"

View File

@ -459,7 +459,9 @@ end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
return { return {
name = "local", name = "local",
caption = fgettext("Start Game"), caption = function()
return fgettext("Start Game")
end ,
cbf_formspec = get_formspec, cbf_formspec = get_formspec,
cbf_button_handler = main_button_handler, cbf_button_handler = main_button_handler,
on_change = on_change on_change = on_change

View File

@ -425,7 +425,9 @@ end
return { return {
name = "online", name = "online",
caption = fgettext("Join Game"), caption = function()
return fgettext("Join Game")
end,
cbf_formspec = get_formspec, cbf_formspec = get_formspec,
cbf_button_handler = main_button_handler, cbf_button_handler = main_button_handler,
on_change = on_change on_change = on_change

View File

@ -164,57 +164,33 @@ static void MSVC_LocaleWorkaround(int argc, char* argv[])
#endif #endif
namespace {
std::string system_language;
}
/*
* System translations
*
* Gettext determines the current language by reading env variables in the following order:
* LANGUAGE, LC_ALL, LC_<type>, LANG.
*
* To change the language, Minetest sets LANGUAGE.
*
* To set the language as the system language, LANGUAGE just needs to be restored
* to the value Minetest was started with. We don't need to touch any of the other
* environment variables
*/
/******************************************************************************/ /******************************************************************************/
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[])
{ {
#if USE_GETTEXT #if USE_GETTEXT
// First, try to set user override environment char *system_language_c = getenv("LANGUAGE");
if (!configured_language.empty()) { if (system_language_c)
// Set LANGUAGE which overrides all others, see system_language = system_language_c;
// <https://www.gnu.org/software/gettext/manual/html_node/Locale-Environment-Variables.html>
#ifndef _MSC_VER
setenv("LANGUAGE", configured_language.c_str(), 1);
// Reload locale with changed environment set_gettext_language(configured_language);
setlocale(LC_ALL, "");
#else
std::string current_language;
const char *env_lang = getenv("LANGUAGE");
if (env_lang)
current_language = env_lang;
setenv("LANGUAGE", configured_language.c_str(), 1);
SetEnvironmentVariableA("LANGUAGE", configured_language.c_str());
#ifndef SERVER
// Hack to force gettext to see the right environment
if (current_language != configured_language)
MSVC_LocaleWorkaround(argc, argv);
#else
errorstream << "*******************************************************" << std::endl;
errorstream << "Can't apply locale workaround for server!" << std::endl;
errorstream << "Expect language to be broken!" << std::endl;
errorstream << "*******************************************************" << std::endl;
#endif
setlocale(LC_ALL, configured_language.c_str());
#endif // ifdef _MSC_VER
} else {
/* set current system default locale */
setlocale(LC_ALL, "");
}
#if defined(_WIN32)
if (getenv("LANGUAGE") != 0) {
setlocale(LC_ALL, getenv("LANGUAGE"));
}
#ifdef _MSC_VER
else if (getenv("LANG") != 0) {
setlocale(LC_ALL, getenv("LANG"));
}
#endif
#endif
std::string name = lowercase(PROJECT_NAME); std::string name = lowercase(PROJECT_NAME);
infostream << "Gettext: domainname=\"" << name infostream << "Gettext: domainname=\"" << name
@ -232,14 +208,73 @@ void init_gettext(const char *path, const std::string &configured_language,
#endif #endif
#else #else
/* set current system default locale */ /* If gettext is not enabled, still set the default locales */
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
setlocale(LC_NUMERIC, "C");
#endif // if USE_GETTEXT #endif // if USE_GETTEXT
}
void set_gettext_language(std::string configured_language)
{
#if USE_GETTEXT
if (configured_language.empty())
configured_language = system_language;
#ifdef _MSC_VER
std::string current_language;
const char *env_lang = getenv("LANGUAGE");
if (env_lang)
current_language = env_lang;
#endif
if (!configured_language.empty()) {
// Set LANGUAGE which overrides all others, see
// <https://www.gnu.org/software/gettext/manual/html_node/Locale-Environment-Variables.html>
setenv("LANGUAGE", configured_language.c_str(), 1);
#ifdef _MSC_VER
SetEnvironmentVariableA("LANGUAGE", configured_language.c_str());
// Reload locale with changed environment
setlocale(LC_ALL, configured_language.c_str());
#else
// Reload locale with changed environment
setlocale(LC_ALL, "");
#endif
} else {
unsetenv("LANGUAGE");
#ifdef _MSC_VER
SetEnvironmentVariableA("LANGUAGE", NULL);
#endif
setlocale(LC_ALL, "");
}
#ifdef _MSC_VER
#ifndef SERVER
// Hack to force gettext to see the right environment
if (current_language != configured_language)
MSVC_LocaleWorkaround(argc, argv);
#else
errorstream << "*******************************************************" << std::endl;
errorstream << "Can't apply locale workaround for server!" << std::endl;
errorstream << "Expect language to be broken!" << std::endl;
errorstream << "*******************************************************" << std::endl;
#endif
#endif
// Notify gettext that the language has changed
// Source: https://www.gnu.org/software/gettext/manual/html_node/gettext-grok.html
{
extern int _nl_msg_cat_cntr;
++_nl_msg_cat_cntr;
}
#endif
/* 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! */
setlocale(LC_NUMERIC, "C"); setlocale(LC_NUMERIC, "C");
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;
} }

View File

@ -46,6 +46,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
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[]);
/**
* Change the language
*
* @param configured_language Language code
*/
void set_gettext_language(std::string configured_language);
inline std::string strgettext(const char *str) inline std::string strgettext(const char *str)
{ {
// We must check here that is not an empty string to avoid trying to translate it // We must check here that is not an empty string to avoid trying to translate it

View File

@ -703,6 +703,10 @@ static bool init_common(const Settings &cmd_args, int argc, char *argv[])
init_gettext(porting::path_locale.c_str(), init_gettext(porting::path_locale.c_str(),
g_settings->get("language"), argc, argv); g_settings->get("language"), argc, argv);
g_settings->registerChangedCallback("language", [](auto _name, auto _data) {
set_gettext_language(g_settings->get("language"));
});
return true; return true;
} }

View File

@ -58,6 +58,7 @@ xgettext --package-name=minetest \
--keyword=fwgettext \ --keyword=fwgettext \
--keyword=fgettext \ --keyword=fgettext \
--keyword=fgettext_ne \ --keyword=fgettext_ne \
--keyword=gettext_lazy \
--keyword=strgettext \ --keyword=strgettext \
--keyword=wstrgettext \ --keyword=wstrgettext \
--keyword=core.gettext \ --keyword=core.gettext \