summaryrefslogtreecommitdiff
path: root/gettext.lua
blob: d6ce2234ae5ac78ba282b4c7d5bf9a451e0b6557 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287

local strfind, strsub, strrep = string.find, string.sub, string.rep
local strmatch, strgsub = string.match, string.gsub
local floor = math.floor

local function split(str, sep)
	local pos, endp = 1, #str+1
	return function()
		if (not pos) or pos > endp then return end
		local s, e = strfind(str, sep, pos, true)
		local part = strsub(str, pos, s and s-1)
		pos = e and e + 1
		return part
	end
end

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]
		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 split(str, "\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")

	until true end -- end for

	return texts
end

local M = { }

local domains = { }
local dgettext_cache = { }
local dngettext_cache = { }
local langs

local function detect_languages()
	if langs then return langs end

	langs = { }

	local function addlang(l)
		local sep
		langs[#langs+1] = l
		sep = strfind(l, ".", 1, true)
		if sep then
			l = strsub(l, 1, sep-1)
			langs[#langs+1] = l
		end
		sep = strfind(l, "_", 1, true)
		if sep then
			langs[#langs+1] = strsub(l, 1, sep-1)
		end
	end

	local v

	v = rawget(_G, "minetest") and minetest.setting_get("language")
	if v and v~="" then
		addlang(v)
	end

	v = os.getenv("LANGUAGE")
	if v then
		for item in split(v, ":") do
			addlang(item)
		end
	end

	v = os.getenv("LANG")
	if v then
		addlang(v)
	end

	return langs
end

local function warn(msg)
	if rawget(_G, "minetest") then
		minetest.log("warning", msg)
	else
		io.stderr:write("WARNING: ", msg, "\n")
	end
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(str)
		local c, t, f = strmatch(str, "^(.-)%?(.-):(.*)")
		if c then
			return ("__if("
					..replace_ternary(c)
					..","..replace_ternary(t)
					..","..replace_ternary(f)
					..")")
		end
		return str
	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 split(str, "\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 ": ")..(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
		return bail("failed to load catalog:"
				.." catalog has no Plural-Forms header")
	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)
	detect_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