/
begin-translation.fs
394 lines (332 loc) · 10.9 KB
/
begin-translation.fs
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
\ galope/begin-translation.fs
\ This file is part of Galope
\ http://programandala.net/en.program.galope.html
\ Author: Marcos Cruz (programandala.net), 2019.
\ Last modified 201912271508
\ See change log at the end of the file
\ ==============================================================
\ Requirements {{{1
require ./noname-create.fs \ `noname-create`
\ ==============================================================
\ begin-translation {{{1
defer lang ( -- )
\ doc{
\
\ lang ( -- n )
\
\ A defered word that must be configured by the application.
\ It returns the number _n_ (0 index) of the current language, which
\ will be used to return the proprer translation defined by
\ `begin-translation`.
\
\ See: `langs`, `default-lang`.
\
\ }doc
: +lang ( a1 -- a2 ) lang cells + ;
0 value langs
\ doc{
\
\ langs ( -- n )
\
\ A ``value`` that returns the number of languages used by the
\ application. It must be configured by the application.
\
\ Usage example:
\ ----
\ 0 constant en_language \ English
\ 1 constant eo_language \ Esperanto
\ 2 constant es_language \ Spanish
\ 3 to langs
\ ----
\
\ See: `lang`, `default-lang`.
\
\ }doc
: ?langs ( -- )
langs 0= abort" `langs` is not set." ;
\ Aborts if `langs` is zero, i.e. if no languages has been set yet.
0 value default-lang
\ doc{
\
\ default-lang ( -- n )
\
\ A ``value`` that returns the language of the translation returned
\ by constants created by `begin-translation` or
\ `begin-noname-transalation` when the translation in the current
\ language is not available, unless `default-translation` is set.
\ Its default value is zero, i.e. the first language defined by the
\ application.
\
\ See: `default-translation`, `lang`, `langs`.
\
\ }doc
$variable default-translation
\ doc{
\
\ default-translation ( -- a )
\
\ A dynamic string variable. _a_ is the address of the string, which
\ can be retrieved by Gforth's ``$@`` and set by ``$!``. Its default
\ value is an empty string.
\
\ When the dynamic string pointed by _a_ is not empty, it will be
\ returned by the variables created by `begin-translation` or
\ `begin-noname-translation`, whenever the translation in the
\ current language is not available.
\
\ When the dynamic string pointed by _a_ is empty, which is the
\ default, `default-lang` is used instead when the current
\ translation of a `translation` variable is not available.
\
\ By storing an identificable string in ``default-translation``, missing
\ translations can be traced application output.
\
\ See: `default-lang`, `langs`.
\
\ }doc
true constant translation-sys
\ The value left by `begin-translation` and
\ `begin-noname-transaltion`, to be consumed by `end-translation`.
: translation, ( translation-sys n[n] ca[n] len[n] ... n[1] ca[1] len[1] -- )
here >r langs 0 ?do 0 , loop
begin dup translation-sys <>
while ( n ca len ) rot cells r@ + $!
repeat drop rdrop ;
\ Compile the localization strings received by `end-translation`.
: no-translation ( pfa -- ca len )
default-translation $@ dup
if rot drop
else 2drop default-lang cells + $@
then ;
\ Behaviour of words created by `begin-translation` or
\ `begin-noname-translation` when no translation has been defined
\ for the current language: If `default-translation` contains a
\ non-empty string, return it. Otherwise return the translation in
\ the language `default-lang`.
: (translation) ( pfa -- ca len )
dup +lang @ if +lang $@ else no-translation then ;
\ Behaviour of words created by `begin-translation` or
\ `begin-noname-translation`.
: end-translation ( translation-sys n[n] ca[n] len[n] ... n[1] ca[1] len[1] -- )
translation, does> ( -- ca len ) ( pfa ) (translation) ;
\ doc{
\
\ end-translation ( translation-sys n[n] ca[n] len[n] ... n[1] ca[1] len[1] --)
\
\ End the definition of a translation started by
\ `begin-translation`, by compiling all translations from string
\ _ca[1] len[1]_ in language _n[n]_ to string _ca[n] len[n]_ in
\ language _n[1]_. Any number of translations can be provided.
\ _translation-sys_ is left by `begin-translation` and
\ `begin-noname-translation` in order to mark marks the end of the
\ parameters.
\
\ See `begin-translation` for details and a usage example.
\
\ }doc
: begin-translation ( "name" -- translation-sys )
?langs create translation-sys ;
\ doc{
\
\ begin-translation ( "name" -- translation-sys )
\
\ Begin the definition of a translation, i.e. a string constant that
\ will be calculated at run-time depending on the language of the
\ current page. _translation-sys_ is consumed by `end-translation`.
\
\ When executed, _name_ will return the string corresponding to the
\ language of the current page. If the required translation is not
\ available, `default-translation` is tried first, then
\ `default-lang`.
\
\
\ Usage example:
\ ----
\ 0 constant english
\ 1 constant esperanto
\ 2 constant spanish
\ 3 constant interlingue
\ 4 to langs
\
\ begin-translation multilingual-salute$
\ spanish s" Hola"
\ interlingue s" Salute"
\ english s" Hello"
\ end-translation
\
\ \ Since no Esperanto translation has been defined in this
\ \ example, it will be calculated depending on
\ \ `default-translation` and `default-lang`.
\ ----
\ See: `end-translation`, `default-translation`, `default-lang`.
\
\ }doc
: begin-noname-translation ( -- xt translation-sys )
?langs noname-create translation-sys ;
\ doc{
\
\ begin-noname-translation ( -- xt translation-sys )
\
\ Begin the definition of an unnamed translation an return its _xt_.
\ A transaltion is a string constant that will be calculated at
\ run-time depending on the language of the current page.
\ _translation-sys_ is consumed by `end-translation`.
\
\ When executed, _xt_ will return the string corresponding to the
\ language of the current page. If the required translation is not
\ available, `default-translation` is tried first, then
\ `default-lang`.
\
\ Usage example:
\ ----
\ 0 constant english
\ 1 constant esperanto
\ 2 constant spanish
\ 3 constant interlingue
\ 4 to langs
\
\ defer my-salute
\
\ begin-noname-translation
\ spanish s" Hola"
\ interlingue s" Salute"
\ english s" Hello"
\ end-translation is my-salute
\
\ \ Since no Esperanto translation has been defined in this
\ \ example, it will be calculated depending on
\ \ `default-translation` and `default-lang`.
\ ----
\ See: `begin-translation`, `end-translation`,
\ `default-translation`, `default-lang`.
\
\ }doc
\ ==============================================================
\ Draft {{{1
false [if]
\ XXX OLD -- This code is a draft of the definitive implementation.
require ./dollar-comma.fs \ `$,`
0 value default-lang
\ default-lang ( -- n )
\
\ A ``value`` that returns the language that `l10n$` variables
\ will use when the translation in the current language is not
\ available, unless `default-l10n$` is set. Its default value is
\ zero, i.e. the first language defined by the application.
\
\ See: `default-l10n$`, `lang`, `langs`.
$variable default-l10n$
\ default-l10n$ ( -- a )
\
\ A dynamic string variable. _a_ is the address of the string, which
\ can be retrieved by Gforth's ``$@`` and set by ``$!``. Its default
\ value is an empty string.
\
\ When the dynamic string pointed by _a_ is not empty, it will be
\ returned by the variables created by `l10n$`, whenever the
\ translation in the current language is not available.
\
\ When the dynamic string pointed by _a_ is empty, which is the
\ default, `default-lang` is used instead when the current
\ translation of a `l10n$` variable is not available.
\
\ By storing an identificable string ``default-l10n$``, missing
\ translations can be traced in the HTML.
\
\ See: `default-lang`, `langs`.
: l10n$, ( true n[n] ca[n] len[n] ... n[1] ca[1] len[1] -- )
here >r langs 0 ?do s" " $, loop
begin dup true <>
while ( n ca len ) rot cells r@ + $!
repeat drop rdrop ;
\ Compile the localization strings received by `l10n$`.
: (l10n$) ( a -- ca len )
dup +lang $@
dup if rot drop \ current lang
else 2drop default-lang cells + $@ \ default lang
then ;
\ Behaviour of localization variables created by `l10n$`.
\ _a_ is the pfa
: l10n$ ( true n[n] ca[n] len[n] ... n[1] ca[1] len[1] "name" -- )
langs 0= abort" `langs` is not set."
create l10n$,
does> ( -- ca len ) ( pfa ) (l10n$) ;
\ l10n$ ( true n[n] ca[n] len[n] ... n[1] ca[1] len[1] "name" -- )
\
\ Create a localization string constant, with translations from
\ _ca[1] len[1]_ in language _n[n]_ to translation _ca[n] len[n]_ in
\ language _n[1]_. Any number of translations can be provided.
\ _true_ marks the end of data.
\
\ When executed, _name_ will return the string corresponding to the
\ language of the current page. If the required translation is not
\ available, `default-l10n$` is tried first, then `default-lang`.
\
\ Usage example:
\ ----
\ 0 constant english
\ 1 constant esperanto
\ 2 constant spanish
\ 3 constant interlingue
\ 4 to langs
\
\ true
\ spanish s" Hola"
\ interlingue s" Salute"
\ english s" Hello"
\ l10n$ multilingual-salute$
\ ----
\ See: `l10n$`, `default-lang`, `l10n-string`.
[then]
\ ==============================================================
\ Development notes {{{1
false [if]
\ 2019-03-11: Possible syntaxes considered and tried, from simple to
\ complex to implement:
\ The simplest one:
true
s" Hola" spanish
s" Saluton" esperanto
s" Salute" interlingue
s" Hello" english
l10n-constant$ multilingual-salute$
\ More legible, and only a `rot` has to be added to the code:
true
spanish s" Hola"
esperanto s" Saluton"
interlingue s" Salute"
english s" Hello"
l10n$ multilingual-salute$
\ Just syntactic sugar instead of `true`:
begin-l10n
spanish s" Hola"
esperanto s" Saluton"
interlingue s" Salute"
english s" Hello"
end-l10n multilingual-salute$
\ No change, only clearer names:
begin-translation
spanish s" Hola"
esperanto s" Saluton"
interlingue s" Salute"
english s" Hello"
end-translation multilingual-salute$
\ Final, a bit more complex to implement:
begin-translation multilingual-salute$
spanish s" Hola"
esperanto s" Saluton"
interlingue s" Salute"
english s" Hello"
end-translation
[then]
\ ==============================================================
\ Change log {{{1
\ 2019-03-11: Write `begin-translation` and `begin-noname-translation`
\ as part of Fendo (http://programandala.net/en.program.fendo.html) to
\ replace its words `l10n-string` and `noname-l10n-string`.
\
\ 2019-03-14: Move the code from Fendo to Galope. It is usable by
\ other applications.
\
\ 2019-12-27: Fix documentation, which still mentioned its
\ original usage as part of Fendo.