summaryrefslogtreecommitdiff
path: root/gettext.lua
blob: 26a2d7329035b633f220a7b9a859110d16e14713 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

local strsub, strrep = string.sub, string.rep
local strmatch, strgsub = string.match, string.gsub

local function trim(str)
	return strmatch(str, "^%s*(.-)%s*$")
end

local escapes = { n="\n", r="\r", t="\t" }

local function unescape(str)
	return (strgsub(str, "(\\+)([nrt]?)", function(bs, c)
		local bsl = #bs
		local realbs = strrep("\\", bsl/2)
		if bsl%2 == 1 then
			c = escapes[c] or c
		end
		return realbs..c
	end))
end

local function parse_po(str)
	local state, msgid, msgid_plural, msgstrind
	local texts = { }
	local lineno = 0
	local function perror(msg)
		return error(msg.." at line "..lineno)
	end
	for _, line in ipairs(str:split("\n")) do repeat
		lineno = lineno + 1
		line = trim(line)

		if line == "" or strmatch(line, "^#") then
			state, msgid, msgid_plural = nil, nil, nil
			break -- continue
		end

		local mid = strmatch(line, "^%s*msgid%s*\"(.*)\"%s*$")
		if mid then
			if state == "id" then
				return perror("unexpected msgid")
			end
			state, msgid = "id", unescape(mid)
			break -- continue
		end

		mid = strmatch(line, "^%s*msgid_plural%s*\"(.*)\"%s*$")
		if mid then
			if state ~= "id" then
				return perror("unexpected msgid_plural")
			end
			state, msgid_plural = "idp", unescape(mid)
			break -- continue
		end

		local ind, mstr = strmatch(line,
				"^%s*msgstr([0-9%[%]]*)%s*\"(.*)\"%s*$")
		if ind then
			if not msgid then
				return perror("missing msgid")
			elseif ind == "" then
				msgstrind = 0
			elseif strmatch(ind, "%[[0-9]+%]") then
				msgstrind = tonumber(strsub(ind, 2, -2))
			else
				return perror("malformed msgstr")
			end
			texts[msgid] = texts[msgid] or { }
			if msgid_plural then
				texts[msgid_plural] = texts[msgid]
			end
			texts[msgid][msgstrind] = unescape(mstr)
			state = "str"
			break -- continue
		end

		mstr = strmatch(line, "^%s*\"(.*)\"%s*$")
		if mstr then
			if state == "id" then
				msgid = msgid..unescape(mstr)
				break -- continue
			elseif state == "idp" then
				msgid_plural = msgid_plural..unescape(mstr)
				break -- continue
			elseif state == "str" then
				local text = texts[msgid][msgstrind]
				texts[msgid][msgstrind] = text..unescape(mstr)
				break -- continue
			end
		end

		return perror("malformed line")

	-- luacheck: ignore
	until true end -- end for

	return texts
end

local M = { }

local function warn(msg)
	minetest.log("warning", "[intllib] "..msg)
end

-- hax!
-- This function converts a C expression to an equivalent Lua expression.
-- It handles enough stuff to parse the `Plural-Forms` header correctly.
-- Note that it assumes the C expression is valid to begin with.
local function compile_plural_forms(str)
	local plural = strmatch(str, "plural=([^;]+);?$")
	local function replace_ternary(s)
		local c, t, f = strmatch(s, "^(.-)%?(.-):(.*)")
		if c then
			return ("__if("
					..replace_ternary(c)
					..","..replace_ternary(t)
					..","..replace_ternary(f)
					..")")
		end
		return s
	end
	plural = replace_ternary(plural)
	plural = strgsub(plural, "&&", " and ")
	plural = strgsub(plural, "||", " or ")
	plural = strgsub(plural, "!=", "~=")
	plural = strgsub(plural, "!", " not ")
	local f, err = loadstring([[
		local function __if(c, t, f)
			if c and c~=0 then return t else return f end
		end
		local function __f(n)
			return (]]..plural..[[)
		end
		return (__f(...))
	]])
	if not f then return nil, err end
	local env = { }
	env._ENV, env._G = env, env
	setfenv(f, env)
	return function(n)
		local v = f(n)
		if type(v) == "boolean" then
			-- Handle things like a plain `n != 1`
			v = v and 1 or 0
		end
		return v
	end
end

local function parse_headers(str)
	local headers = { }
	for _, line in ipairs(str:split("\n")) do
		local k, v = strmatch(line, "^([^:]+):%s*(.*)")
		if k then
			headers[k] = v
		end
	end
	return headers
end

local function load_catalog(filename)
	local f, data, err

	local function bail(msg)
		warn(msg..(err and ": " or "")..(err or ""))
		return nil
	end

	f, err = io.open(filename, "rb")
	if not f then
		return --bail("failed to open catalog")
	end

	data, err = f:read("*a")

	f:close()

	if not data then
		return bail("failed to read catalog")
	end

	data, err = parse_po(data)
	if not data then
		return bail("failed to parse catalog")
	end

	err = nil
	local hdrs = data[""]
	if not (hdrs and hdrs[0]) then
		return bail("catalog has no headers")
	end

	hdrs = parse_headers(hdrs[0])

	local pf = hdrs["Plural-Forms"]
	if not pf then
		-- XXX: Is this right? Gettext assumes this if header not present.
		pf = "nplurals=2; plural=n != 1"
	end

	data.plural_index, err = compile_plural_forms(pf)
	if not data.plural_index then
		return bail("failed to compile plural forms")
	end

	--warn("loaded: "..filename)

	return data
end

function M.load_catalogs(path)
	local langs = intllib.get_detected_languages()

	local cats = { }
	for _, lang in ipairs(langs) do
		local cat = load_catalog(path.."/"..lang..".po")
		if cat then
			cats[#cats+1] = cat
		end
	end

	return cats
end

return M