[CSM] Fix and improve minetest.get_language()
authorsfan5 <sfan5@live.de>
Sat, 9 Nov 2019 15:15:05 +0000 (16:15 +0100)
committersfan5 <sfan5@live.de>
Mon, 11 Nov 2019 13:06:48 +0000 (14:06 +0100)
Previously this method would accidentally reset the locale
and break everything.

clientmods/preview/init.lua
doc/client_lua_api.txt
src/script/lua_api/l_client.cpp

index 2e679b866175df5f78d10a44cb8d188a1569e4d8..5777adcafa86e45ca0caee54aa3713c05baf46ef 100644 (file)
@@ -9,13 +9,18 @@ core.register_on_shutdown(function()
 end)
 local id = nil
 
-local server_info = core.get_server_info()
-print("Server version: " .. server_info.protocol_version)
-print("Server ip: " .. server_info.ip)
-print("Server address: " .. server_info.address)
-print("Server port: " .. server_info.port)
+do
+       local server_info = core.get_server_info()
+       print("Server version: " .. server_info.protocol_version)
+       print("Server ip: " .. server_info.ip)
+       print("Server address: " .. server_info.address)
+       print("Server port: " .. server_info.port)
 
-print("CSM restrictions: " .. dump(core.get_csm_restrictions()))
+       print("CSM restrictions: " .. dump(core.get_csm_restrictions()))
+
+       local l1, l2 = core.get_language()
+       print("Configured language: " .. l1 .. " / " .. l2)
+end
 
 mod_channel = core.mod_channel_join("experimental_preview")
 
index a7e928f566eb6765bbf061688adfe675e77352b1..c24de8d8578e5b3ea1b21e877fde2ca230eb0583 100644 (file)
@@ -634,7 +634,9 @@ Minetest namespace reference
    the trailing separator. This is useful to load additional Lua files
    contained in your mod:
    e.g. `dofile(minetest.get_modpath(minetest.get_current_modname()) .. "stuff.lua")`
-* `minetest.get_language()`: returns the currently set gettext language.
+* `minetest.get_language()`: returns two strings
+   * the current gettext locale
+   * the current language code (the same as used for client-side translations)
 * `minetest.get_version()`: returns a table containing components of the
    engine version.  Components:
     * `project`: Name of the project, eg, "Minetest"
index fa369a360b53a445c65e9081df2deed1ba2f32d8..4aa7d814de33fab2bc94b6dcc9f339639ca1bcd6 100644 (file)
@@ -230,9 +230,14 @@ int ModApiClient::l_get_node_or_nil(lua_State *L)
 
 int ModApiClient::l_get_language(lua_State *L)
 {
-       char *locale = setlocale(LC_ALL, "");
+       char *locale = setlocale(LC_MESSAGES, NULL);
+       std::string lang = gettext("LANG_CODE");
+       if (lang == "LANG_CODE")
+               lang = "";
+
        lua_pushstring(L, locale);
-       return 1;
+       lua_pushstring(L, lang.c_str());
+       return 2;
 }
 
 int ModApiClient::l_get_wielded_item(lua_State *L)