1
0
mirror of https://github.com/minetest-mods/intllib.git synced 2025-01-07 00:20:27 +01:00

Use optional dependencies to only load once and re-add support for escapes

This commit is contained in:
ShadowNinja 2013-10-29 11:19:35 -04:00
parent 5de9bcea76
commit af31d71d1a
3 changed files with 81 additions and 94 deletions

View File

@ -12,20 +12,17 @@ it at the beginning of your source file(s):
-- Boilerplate to support localized strings if intllib mod is installed.
local S
if (minetest.get_modpath("intllib")) then
dofile(minetest.get_modpath("intllib").."/intllib.lua")
S = intllib.Getter(minetest.get_current_modname())
if intllib then
S = intllib.Getter()
else
S = function ( s ) return s end
S = function(s) return s end
end
Note that by using this snippet, you don't need to depend on `intllib'. In
fact, the mod's `init.lua' is a no-op; you need to explicitly execute intllib's
`intllib.lua' file.
Also note that if the intllib "mod" is not installed, the S() function is
defined so it returns the string unchanged. This is done so you don't have to
sprinkle tons of `if's (or similar constructs) to check if the lib is actually
installed.
You will also need to optionally depend on intllib, to do so add "intllib?" to
a empty line in your depends.txt. Also note that if intllib is not installed,
the S() function is defined so it returns the string unchanged. This is done
so you don't have to sprinkle tons of 'if's (or similar constructs) to check
if the lib is actually installed.
Next, for each "translatable" string in your sources, use the S() function
(defined in the snippet) to return the translated string. For example:
@ -42,9 +39,8 @@ support. Here's an example for a Spanish locale file (`es.txt'):
# Lines beginning with a pound sign are comments and are effectively ignored
# by the reader. Note that comments only span until the end of the line;
# there's no support for multiline comments.
Blank lines not containing an equals sign are also ignored.
Hello, World! = Hola, Mundo!
String with\nnewlines and \ttabs = Cadena con\nsaltos de linea y\ttabuladores
String with\nnewlines = Cadena con\nsaltos de linea
String with an \= equals sign = Cadena con un signo de \= igualdad
Since there's currently no portable way to detect the language, this library
@ -71,22 +67,6 @@ Also note that there are some problems with using accented, and in general
non-latin characters in strings. Until a fix is found, please limit yourself
to using only US-ASCII characters.
Frequently Asked Questions
--------------------------
Q: Were you bored when you did this?
A: Yes.
Q: Why are my texts are not translated?
A: RTFM...or ask in the topic 8-----)
Q: How come the README is bigger than the actual code?
A: Because I'm adding silly unfunny questions here...and because there are
some users that are too lazy to understand how the code works, so I have
to document things.
Q: I don't like this sh*t!
A: That's not a question.
Thanks for reading up to this point.
Should you have any comments/suggestions, please post them in the forum topic.

View File

@ -0,0 +1,70 @@
-- Support the old multi-load method
intllib = intllib or {}
local strings = {}
local LANG = minetest.setting_get("language") or os.getenv("LANG") or "en"
LANG = LANG:sub(1, 2)
local escapes = {
["\\"] = "\\",
["n"] = "\n",
["="] = "=",
}
local function unescape(s)
return s:gsub("([\\]?)\\(.)", function(slash, what)
if slash and (slash ~= "") then
return "\\"..what
else
return escapes[what] or what
end
end)
end
local function find_eq(s)
for slashes, pos in s:gmatch("([\\]*)=()") do
if (slashes:len() % 2) == 0 then
return pos - 1
end
end
end
function load_strings(modname)
local modpath = minetest.get_modpath(modname)
local file, err = io.open(modpath.."/locale/"..LANG..".txt", "r")
if not file then
return nil
end
local strings = {}
for line in file:lines() do
line = line:trim()
if line ~= "" and line:sub(1, 1) ~= "#" then
local pos = find_eq(line)
if pos then
local msgid = unescape(line:sub(1, pos - 1):trim())
strings[msgid] = unescape(line:sub(pos + 1):trim())
end
end
end
file:close()
return strings
end
-- Support the old multi-load method
intllib.getters = intllib.getters or {}
function intllib.Getter(modname)
modname = modname or minetest.get_current_modname()
if not intllib.getters[modname] then
local msgstr = load_strings(modname) or {}
intllib.getters[modname] = function (s)
return msgstr[s] or s
end
end
return intllib.getters[modname]
end

View File

@ -1,66 +1,3 @@
-- Support for the old multi-load method
dofile(minetest.get_modpath("intllib").."/init.lua")
intllib = { };
local strings = { };
local INTLLIB_DEBUG = true;
local LANG = minetest.setting_get("language") or os.getenv("LANG") or "en";
LANG = LANG:sub(1, 2);
local TRACE;
if (INTLLIB_DEBUG) then
TRACE = function ( s )
print("*** DEBUG: "..s);
end
else
TRACE = function ( ) end
end
local function do_load_strings ( f )
local msgstr = { };
for line in f:lines() do
line = line:trim();
if ((line ~= "") and (line:sub(1, 1) ~= "#")) then
local pos = line:find("=", 1, true);
if (pos) then
local msgid = line:sub(1, pos - 1):trim();
msgstr[msgid] = line:sub(pos + 1):trim();
end
end
end
return msgstr;
end
function load_strings ( modname, lang )
lang = lang or LANG;
local f, e = io.open(minetest.get_modpath(modname).."/locale/"..lang..".txt");
if (not f) then
f, e = io.open(minetest.get_modpath("intllib").."/locale/"..modname.."/"..lang..".txt");
if (not f) then
return nil;
end
end
local strings;
strings = do_load_strings(f);
f:close();
return strings;
end
local getters = { };
function intllib.Getter ( modname )
if (not modname) then modname = minetest.get_current_modname(); end
if (not getters[modname]) then
local msgstr = load_strings(modname, lang) or { };
getters[modname] = function ( s )
return msgstr[repr(s)] or s;
end;
end
return getters[modname];
end
function intllib.get_current_language ( )
return LANG;
end