From f4eba3bfba998d14f8a95e832beae40b03eacf8a Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sun, 25 Feb 2024 22:09:52 +0000 Subject: [PATCH] Add support for ContentDB package translation (#14410) --- builtin/mainmenu/content/dlg_contentstore.lua | 19 +++++++++++++++++-- src/script/lua_api/l_mainmenu.cpp | 13 +++++++++++++ src/script/lua_api/l_mainmenu.h | 2 ++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/builtin/mainmenu/content/dlg_contentstore.lua b/builtin/mainmenu/content/dlg_contentstore.lua index 6070bf753..1d6b30cf6 100644 --- a/builtin/mainmenu/content/dlg_contentstore.lua +++ b/builtin/mainmenu/content/dlg_contentstore.lua @@ -154,7 +154,9 @@ local function start_install(package, reason) if conf_path then local conf = Settings(conf_path) - conf:set("title", package.title) + if not conf:get("title") then + conf:set("title", package.title) + end if not name_is_title then conf:set("name", package.name) end @@ -642,8 +644,21 @@ local function fetch_pkgs() end end + local languages + local current_language = core.get_language() + if current_language ~= "" then + languages = { current_language, "en;q=0.8" } + else + languages = { "en" } + end + local http = core.get_http_api() - local response = http.fetch_sync({ url = url }) + local response = http.fetch_sync({ + url = url, + extra_headers = { + "Accept-Language: " .. table.concat(languages, ", ") + }, + }) if not response.succeeded then return end diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp index 0877e930d..44b4b57e7 100644 --- a/src/script/lua_api/l_mainmenu.cpp +++ b/src/script/lua_api/l_mainmenu.cpp @@ -933,6 +933,17 @@ int ModApiMainMenu::l_get_video_drivers(lua_State *L) return 1; } +/******************************************************************************/ +int ModApiMainMenu::l_get_language(lua_State *L) +{ + std::string lang = gettext("LANG_CODE"); + if (lang == "LANG_CODE") + lang = ""; + + lua_pushstring(L, lang.c_str()); + return 1; +} + /******************************************************************************/ int ModApiMainMenu::l_gettext(lua_State *L) { @@ -1151,6 +1162,7 @@ void ModApiMainMenu::Initialize(lua_State *L, int top) API_FCT(get_mainmenu_path); API_FCT(show_path_select_dialog); API_FCT(download_file); + API_FCT(get_language); API_FCT(gettext); API_FCT(get_video_drivers); API_FCT(get_window_info); @@ -1191,5 +1203,6 @@ void ModApiMainMenu::InitializeAsync(lua_State *L, int top) API_FCT(download_file); API_FCT(get_min_supp_proto); API_FCT(get_max_supp_proto); + API_FCT(get_language); API_FCT(gettext); } diff --git a/src/script/lua_api/l_mainmenu.h b/src/script/lua_api/l_mainmenu.h index d06ff57b4..780e3b5b8 100644 --- a/src/script/lua_api/l_mainmenu.h +++ b/src/script/lua_api/l_mainmenu.h @@ -74,6 +74,8 @@ private: static int l_get_mapgen_names(lua_State *L); + static int l_get_language(lua_State *L); + static int l_gettext(lua_State *L); //packages