summaryrefslogtreecommitdiff
path: root/lib.lua
blob: db6de0d00a83f6073e65cbbe8deb5da65e9829f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

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(filename)
	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