implement language saving
This commit is contained in:
parent
bea880cf97
commit
f917c78ae8
73
init.lua
73
init.lua
@ -12,6 +12,7 @@ dofile(modpath.."/chat.lua" )
|
|||||||
local langprefs = minetest.get_worldpath().."/babel_langprefs"
|
local langprefs = minetest.get_worldpath().."/babel_langprefs"
|
||||||
local engine = minetest.setting_get("babelfish.engine") or "yandex"
|
local engine = minetest.setting_get("babelfish.engine") or "yandex"
|
||||||
babel.key = minetest.setting_get("babelfish.key")
|
babel.key = minetest.setting_get("babelfish.key")
|
||||||
|
babel.defaultlang = minetest.setting_get("babelfish.defaultlang") or "en"
|
||||||
|
|
||||||
minetest.register_privilege("babelmoderator")
|
minetest.register_privilege("babelmoderator")
|
||||||
|
|
||||||
@ -36,6 +37,30 @@ require = oldrequire -- restore the sandbox's require
|
|||||||
|
|
||||||
-- =====================
|
-- =====================
|
||||||
|
|
||||||
|
local function prefsave()
|
||||||
|
local serdata = minetest.serialize(player_pref_language)
|
||||||
|
if not serdata then
|
||||||
|
minetest.log("error", "[babelfish] Data serialization failed")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local file, err = io.open(langprefs, "w")
|
||||||
|
if err then
|
||||||
|
return err
|
||||||
|
end
|
||||||
|
file:write(serdata)
|
||||||
|
file:close()
|
||||||
|
end
|
||||||
|
|
||||||
|
local function prefload()
|
||||||
|
local file, err = io.open(langprefs, "r")
|
||||||
|
if err then
|
||||||
|
minetest.log("error", "[babelfish] Data read failed")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
player_pref_language = minetest.deserialize(file:read("*a"))
|
||||||
|
file:close()
|
||||||
|
end
|
||||||
|
|
||||||
-- ========================== Language engine and overridable validation
|
-- ========================== Language engine and overridable validation
|
||||||
|
|
||||||
function babel.validate_lang(self, langstring)
|
function babel.validate_lang(self, langstring)
|
||||||
@ -173,6 +198,16 @@ local function f_babelmsg(player, argstring)
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function setplayerlanguage(tplayer, langcode)
|
||||||
|
if minetest.get_player_by_name(tplayer) then
|
||||||
|
player_pref_language[tplayer] = langcode
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function getplayerlanguage(tplayer)
|
||||||
|
return player_pref_language[tplayer]
|
||||||
|
end
|
||||||
|
|
||||||
minetest.register_chatcommand("bblang", {
|
minetest.register_chatcommand("bblang", {
|
||||||
description = "Set your preferred language",
|
description = "Set your preferred language",
|
||||||
func = function(player,args)
|
func = function(player,args)
|
||||||
@ -211,6 +246,7 @@ minetest.register_chatcommand("bb", {
|
|||||||
minetest.register_chatcommand("bmsg", {
|
minetest.register_chatcommand("bmsg", {
|
||||||
description = "Send a private message to a player, in their preferred language",
|
description = "Send a private message to a player, in their preferred language",
|
||||||
params = "<player> <sentence>",
|
params = "<player> <sentence>",
|
||||||
|
privs = {shout = true},
|
||||||
func = f_babelmsg
|
func = f_babelmsg
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -222,37 +258,20 @@ minetest.register_chatcommand("bbset", {
|
|||||||
privs = {babelmoderator = true},
|
privs = {babelmoderator = true},
|
||||||
func = function(player, message)
|
func = function(player, message)
|
||||||
local tplayer, langcode = components(message)
|
local tplayer, langcode = components(message)
|
||||||
if minetest.get_player_by_name(tplayer) then
|
setplayerlanguage(tplayer, langcode)
|
||||||
player_pref_language[tplayer] = langcode
|
|
||||||
end
|
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
-- Set player's default language
|
||||||
|
|
||||||
|
minetest.register_on_joinplayer(function(player, ip)
|
||||||
|
local playername = player:get_player_name()
|
||||||
|
if not getplayerlanguage(playername) then
|
||||||
|
setplayerlanguage(playername, babel.defaultlang)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
-- Display help string, and compliance if set
|
-- Display help string, and compliance if set
|
||||||
dofile(minetest.get_modpath("babelfish").."/compliance.lua")
|
dofile(minetest.get_modpath("babelfish").."/compliance.lua")
|
||||||
|
|
||||||
local function prefsave()
|
|
||||||
local serdata = minetest.serialize(player_pref_language)
|
|
||||||
if not serdata then
|
|
||||||
minetest.log("error", "[babelfish] Data serialization failed")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local file, err = io.open(langprefs, "w")
|
|
||||||
if err then
|
|
||||||
return err
|
|
||||||
end
|
|
||||||
file:write(serdata)
|
|
||||||
file:close()
|
|
||||||
end
|
|
||||||
|
|
||||||
local function prefload()
|
|
||||||
local file, err = io.open(langprefs, "r")
|
|
||||||
if err then
|
|
||||||
minetest.log("error", "[babelfish] Data read failed")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
player_pref_language = minetest.deserialize(file:read("*a"))
|
|
||||||
file:close()
|
|
||||||
end
|
|
||||||
|
|
||||||
prefload()
|
prefload()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user