mult-fmt.lua /size: 14 Kb    last modification: 2023-12-21 09:44
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-- function interfaces.setuserinterface(interface,response)
45--     local variables     = interfaces.variables
46--     local constants     = interfaces.constants
47--     local elements      = interfaces.elements
48--     local formats       = interfaces.formats
49--     local translations  = interfaces.translations
50--     local setupstrings  = interfaces.setupstrings
51--     local complete      = interfaces.complete
52--     local sharedstorage = storage.shared
53--     --
54--     sharedstorage.currentinterface, currentinterface = interface, interface
55--     sharedstorage.currentresponse, currentresponse = response, response
56--     --
57--     if environment.initex then
58--         local nofconstants    = 0
59--         local nofvariables    = 0
60--         local nofelements     = 0
61--         local nofcommands     = 0
62--         local nofformats      = 0
63--         local noftranslations = 0
64--         local nofsetupstrings = 0
65--         --
66--         do
67--             local list = complete.constants -- forces the load
68--             local t    = { }
69--             local f    = formatters["\\ui_c{%s}{%s}"]
70--             local s    = formatters["\\ui_s{%s}"]
71--             logs.startfilelogging(report,"translated constants")
72--             for given, constant in sortedhash(list) do
73--                 constant = constant[interface] or constant.en or given
74--                 constants[constant] = given -- breedte -> width
75--                 nofconstants = nofconstants + 1
76--                 if given == constant then
77--                     t[nofconstants] = s(given)
78--                 else
79--                     t[nofconstants] = f(given,constant)
80--                 end
81--                 report_constant("%-40s: %s",given,constant)
82--             end
83--             logs.stopfilelogging()
84--             contextsprint(prtcatcodes,concat(t))
85--         end
86--         do
87--             local list = complete.variables -- forces the load
88--             local t    = { }
89--             local f    = formatters["\\ui_v{%s}{%s}"]
90--             logs.startfilelogging(report,"translated variables")
91--             for given, variable in sortedhash(list) do
92--                 variable = variable[interface] or variable.en or given
93--                 variables[given] = variable -- ja -> yes
94--                 nofvariables = nofvariables + 1
95--                 t[nofvariables] = f(given,variable)
96--                 report_variable("%-40s: %s",given,variable)
97--             end
98--             logs.stopfilelogging()
99--             contextsprint(prtcatcodes,concat(t))
100--         end
101--         do
102--             local list = complete.elements -- forces the load
103--             local t    = { }
104--             local f    = formatters["\\ui_e{%s}{%s}"]
105--             logs.startfilelogging(report,"translated elements")
106--             for given, element in sortedhash(list) do
107--                 element = element[interface] or element.en or given
108--                 elements[element] = given
109--                 nofelements = nofelements + 1
110--                 t[nofelements] = f(given,element)
111--                 report_element("%-40s: %s",given,element)
112--             end
113--             logs.stopfilelogging()
114--             contextsprint(prtcatcodes,concat(t))
115--         end
116--         do
117--             local list = complete.commands -- forces the load
118--             local t    = { }
119--             local n    = 0
120--             local f    = formatters["\\ui_a\\%s\\%s"] -- formatters["\\ui_m{%s}{%s}"]
121--             logs.startfilelogging(report,"translated commands")
122--             for given, command in sortedhash(list) do
123--                 command = command[interface] or command.en or given
124--                 if command ~= given then
125--                     n = n + 1
126--                     t[n] = f(given,command)
127--                     report_command("%-40s: %s",given,command)
128--                 end
129--                 nofcommands = nofcommands + 1
130--             end
131--             logs.stopfilelogging()
132--             contextsprint(prtcatcodes,"\\toksapp\\everydump{"..concat(t).."}")
133--         end
134--         do
135--             local list = complete.messages.formats
136--             logs.startfilelogging(report,"translated message formats")
137--             for given, format in sortedhash(list) do
138--                 local found = format[interface] or format.en or given
139--                 formats[given] = found
140--                 nofformats = nofformats + 1
141--                 report_messagetag("%-40s: %s",limit(given,38),limit(found,38))
142--             end
143--             logs.stopfilelogging()
144--         end
145--         do
146--             local list = complete.messages.translations
147--             logs.startfilelogging(report,"translated message tags")
148--             for given, translation in sortedhash(list) do
149--                 local found = translation[interface] or translation.en or given
150--                 translations[given] = found
151--                 noftranslations = noftranslations + 1
152--                 report_messagetag("%-40s: %s",given,found)
153--             end
154--             logs.stopfilelogging()
155--         end
156--         do
157--             local list = complete.setupstrings
158--             logs.startfilelogging(report,"translated setupstrings")
159--             for given, setupstring in sortedhash(list) do
160--                 local found = setupstring[interface] or setupstring.en or given
161--                 setupstrings[given] = found
162--                 nofsetupstrings = nofsetupstrings + 1
163--                 report_setupstring("%-40s: %s",given,found)
164--             end
165--             logs.stopfilelogging()
166--         end
167--         report_interface("definitions: %a constants, %a variables, %a elements, %a commands, %a formats, %a translations, %a setupstrings",
168--             nofconstants,nofvariables,nofelements,nofcommands,nofformats,noftranslations,nofsetupstrings)
169--     else
170--         report_interface("the language(s) can only be set when making the format")
171--     end
172--     interfaces.currentinterface = currentinterface
173--     interfaces.currentresponse  = currentresponse
174-- end
175
176-- different per interface
177--
178-- en:
179--
180-- ui_c macro:#1#2 -> \immutable \gdefcsname \c!prefix! #1\endcsname {#1}
181-- ui_v macro:#1#2 -> \immutable \gdefcsname \v!prefix! #1\endcsname {#2}
182-- ui_e macro:#1#2 -> \immutable \gdefcsname \e!prefix! #1\endcsname {#2}
183-- ui_a macro:#1#2 -> \frozen \protected \def #2{#1}
184--
185-- otherwise:
186--
187-- ui_c macro:#1#2 -> \immutable \gdefcsname \c!prefix! #1\endcsname {#1}
188--                    \immutable \gdefcsname \k!prefix! #2\endcsname {#1}
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 -- forces the load
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 -- breedte -> width
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 -- forces the load
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 -- ja -> yes
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 -- forces the load
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--         do
253--             local list = complete.commands -- forces the load
254--             local todo = { } -- normally a small list
255--             logs.startfilelogging(report,"translated commands")
256--             for given, command in sortedhash(list) do
257--                 command = command[interface] or command.en or given
258--                 if command ~= given then
259--                     report_command("%-40s: %s",given,command)
260--                     todo[given] = command
261--                 end
262--                 nofcommands = nofcommands + 1
263--             end
264--             logs.stopfilelogging()
265--             -- For some reason we get corrupted definitions.
266--             luatex.registerdumpactions(function()
267--                 for given, command in sortedhash(todo) do
268--                  -- if definedmacro(given) then
269--                         setmacro(command,"\\"..given,"frozen","protected","global")
270--                  -- end
271--                 end
272--             end)
273--         end
274        do
275            local list = complete.commands -- forces the load
276            local t    = { }
277            local n    = 0
278            local f    = formatters["\\frozen\\protected\\def\\%s{\\%s}"] -- formatters["\\ui_m{%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