1
0
mirror of https://github.com/minetest-mods/intllib.git synced 2024-11-16 15:30:28 +01:00
intllib/lib.lua

64 lines
1.1 KiB
Lua
Raw Normal View History

2013-10-29 20:37:36 +01:00
2013-12-18 02:29:01 +01:00
intllib = intllib or {}
2015-02-02 00:10:09 +01:00
local INS_CHAR = "@"
intllib.INSERTION_CHAR = INS_CHAR
2013-10-29 20:37:36 +01:00
local escapes = {
["\\"] = "\\",
["n"] = "\n",
2015-02-02 00:10:09 +01:00
[INS_CHAR] = INS_CHAR..INS_CHAR,
2013-10-29 20:37:36 +01:00
}
2015-02-02 00:10:09 +01:00
local function unescape(str)
local parts = {}
local n = 1
local function add(s)
parts[n] = s
n = n + 1
end
local start = 1
while true do
local pos = str:find("\\", start, true)
if pos then
add(str:sub(start, pos - 1))
2013-10-29 20:37:36 +01:00
else
2015-02-02 00:10:09 +01:00
add(str:sub(start))
break
2013-10-29 20:37:36 +01:00
end
2015-02-02 00:10:09 +01:00
local c = str:sub(pos + 1, pos + 1)
add(escapes[c] or c)
start = pos + 2
end
return table.concat(parts)
2013-10-29 20:37:36 +01:00
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
2013-12-18 02:29:01 +01:00
function intllib.load_strings(filename)
2013-10-29 20:37:36 +01:00
local file, err = io.open(filename, "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