Skip to content

Commit c44318a

Browse files
committedNov 11, 2019
[CSM] Fix and improve minetest.get_language()
Previously this method would accidentally reset the locale and break everything.
1 parent 2c4cf50 commit c44318a

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed
 

Diff for: ‎clientmods/preview/init.lua

+11-6
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ core.register_on_shutdown(function()
99
end)
1010
local id = nil
1111

12-
local server_info = core.get_server_info()
13-
print("Server version: " .. server_info.protocol_version)
14-
print("Server ip: " .. server_info.ip)
15-
print("Server address: " .. server_info.address)
16-
print("Server port: " .. server_info.port)
12+
do
13+
local server_info = core.get_server_info()
14+
print("Server version: " .. server_info.protocol_version)
15+
print("Server ip: " .. server_info.ip)
16+
print("Server address: " .. server_info.address)
17+
print("Server port: " .. server_info.port)
1718

18-
print("CSM restrictions: " .. dump(core.get_csm_restrictions()))
19+
print("CSM restrictions: " .. dump(core.get_csm_restrictions()))
20+
21+
local l1, l2 = core.get_language()
22+
print("Configured language: " .. l1 .. " / " .. l2)
23+
end
1924

2025
mod_channel = core.mod_channel_join("experimental_preview")
2126

Diff for: ‎doc/client_lua_api.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,9 @@ Minetest namespace reference
634634
the trailing separator. This is useful to load additional Lua files
635635
contained in your mod:
636636
e.g. `dofile(minetest.get_modpath(minetest.get_current_modname()) .. "stuff.lua")`
637-
* `minetest.get_language()`: returns the currently set gettext language.
637+
* `minetest.get_language()`: returns two strings
638+
* the current gettext locale
639+
* the current language code (the same as used for client-side translations)
638640
* `minetest.get_version()`: returns a table containing components of the
639641
engine version. Components:
640642
* `project`: Name of the project, eg, "Minetest"

Diff for: ‎src/script/lua_api/l_client.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,14 @@ int ModApiClient::l_get_node_or_nil(lua_State *L)
230230

231231
int ModApiClient::l_get_language(lua_State *L)
232232
{
233-
char *locale = setlocale(LC_ALL, "");
233+
char *locale = setlocale(LC_MESSAGES, NULL);
234+
std::string lang = gettext("LANG_CODE");
235+
if (lang == "LANG_CODE")
236+
lang = "";
237+
234238
lua_pushstring(L, locale);
235-
return 1;
239+
lua_pushstring(L, lang.c_str());
240+
return 2;
236241
}
237242

238243
int ModApiClient::l_get_wielded_item(lua_State *L)

0 commit comments

Comments
 (0)
Please sign in to comment.