1if not modules then modules = { } end modules ['mult-fmt'] = {
2 version = 1.001,
3 comment = "companion to mult-ini.mkiv",
4 author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5 copyright = "PRAGMA ADE / ConTeXt Development Team",
6 license = "see context related readme files"
7}
8
9local next = next
10local concat, sortedhash = table.concat, table.sortedhash
11local sub, formatters = string.sub, string.formatters
12local utfsplit = utf.split
13
14local prtcatcodes = catcodes.numbers.prtcatcodes
15local contextsprint = context.sprint
16local implement = interfaces.implement
17
18local setmacro = token.setmacro or token.set_macro
19local definedmacro = token.isdefined or token.is_defined
20
21local report = logs.reporter("interface")
22local report_interface = logs.reporter("interface","initialization")
23local report_variable = logs.reporter("variable")
24local report_constant = logs.reporter("constant")
25local report_command = logs.reporter("command")
26local report_element = logs.reporter("element")
27local report_format = logs.reporter("format")
28local report_messagetag = logs.reporter("messagetag")
29local report_setupstring = logs.reporter("setupstring")
30
31local function limit(str,n)
32 if n > 6 and #str > n then
33 n = n - 4
34 local t = utfsplit(str)
35 local m = #t
36 if m > n then
37 t[n] = " ..."
38 str = concat(t,"",1,n)
39 end
40 end
41 return str
42end
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
190function interfaces.setuserinterface(interface,response)
191 local variables = interfaces.variables
192 local constants = interfaces.constants
193 local elements = interfaces.elements
194 local formats = interfaces.formats
195 local translations = interfaces.translations
196 local setupstrings = interfaces.setupstrings
197 local complete = interfaces.complete
198 local sharedstorage = storage.shared
199
200 sharedstorage.currentinterface, currentinterface = interface, interface
201 sharedstorage.currentresponse, currentresponse = response, response
202
203 if environment.initex then
204 local nofconstants = 0
205 local nofvariables = 0
206 local nofelements = 0
207 local nofcommands = 0
208 local nofformats = 0
209 local noftranslations = 0
210 local nofsetupstrings = 0
211 local reversetoo = interface ~= "en"
212
213 do
214 local list = complete.constants
215 logs.startfilelogging(report,"translated constants")
216 for given, constant in sortedhash(list) do
217 constant = constant[interface] or constant.en or given
218 constants[constant] = given
219 nofconstants = nofconstants + 1
220 setmacro("c!" .. given,given,"immutable","constant")
221 if reversetoo then
222 setmacro("k!" .. constant,given,"immutable","constant")
223 end
224 report_constant("%-40s: %s",given,constant)
225 end
226 logs.stopfilelogging()
227 end
228 do
229 local list = complete.variables
230 logs.startfilelogging(report,"translated variables")
231 for given, variable in sortedhash(list) do
232 variable = variable[interface] or variable.en or given
233 variables[given] = variable
234 nofvariables = nofvariables + 1
235 setmacro("v!" .. given,variable,"immutable","constant")
236 report_variable("%-40s: %s",given,variable)
237 end
238 logs.stopfilelogging()
239 end
240 do
241 local list = complete.elements
242 logs.startfilelogging(report,"translated elements")
243 for given, element in sortedhash(list) do
244 element = element[interface] or element.en or given
245 elements[element] = given
246 nofelements = nofelements + 1
247 setmacro("e!" .. given,element,"immutable","constant")
248 report_element("%-40s: %s",given,element)
249 end
250 logs.stopfilelogging()
251 end
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274 do
275 local list = complete.commands
276 local t = { }
277 local n = 0
278 local f = formatters["\\frozen\\protected\\def\\%s{\\%s}"]
279 logs.startfilelogging(report,"translated commands")
280 for given, command in sortedhash(list) do
281 command = command[interface] or command.en or given
282 if command ~= given then
283 n = n + 1
284 t[n] = f(command,given)
285 report_command("%-40s: %s",given,command)
286 end
287 nofcommands = nofcommands + 1
288 end
289 logs.stopfilelogging()
290 contextsprint(prtcatcodes,"\\toksapp\\everydump{"..concat(t).."}")
291 end
292 do
293 local list = complete.messages.formats
294 logs.startfilelogging(report,"translated message formats")
295 for given, format in sortedhash(list) do
296 local found = format[interface] or format.en or given
297 formats[given] = found
298 nofformats = nofformats + 1
299 report_messagetag("%-40s: %s",limit(given,38),limit(found,38))
300 end
301 logs.stopfilelogging()
302 end
303 do
304 local list = complete.messages.translations
305 logs.startfilelogging(report,"translated message tags")
306 for given, translation in sortedhash(list) do
307 local found = translation[interface] or translation.en or given
308 translations[given] = found
309 noftranslations = noftranslations + 1
310 report_messagetag("%-40s: %s",given,found)
311 end
312 logs.stopfilelogging()
313 end
314 do
315 local list = complete.setupstrings
316 logs.startfilelogging(report,"translated setupstrings")
317 for given, setupstring in sortedhash(list) do
318 local found = setupstring[interface] or setupstring.en or given
319 setupstrings[given] = found
320 nofsetupstrings = nofsetupstrings + 1
321 report_setupstring("%-40s: %s",given,found)
322 end
323 logs.stopfilelogging()
324 end
325 report_interface("definitions: %a constants, %a variables, %a elements, %a commands, %a formats, %a translations, %a setupstrings",
326 nofconstants,nofvariables,nofelements,nofcommands,nofformats,noftranslations,nofsetupstrings)
327 else
328 report_interface("the language(s) can only be set when making the format")
329 end
330 interfaces.currentinterface = currentinterface
331 interfaces.currentresponse = currentresponse
332end
333
334interfaces.implement {
335 name = "setuserinterface",
336 actions = interfaces.setuserinterface,
337 arguments = "2 strings",
338}
339 |