-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalization.monkey2
502 lines (366 loc) · 10.1 KB
/
Localization.monkey2
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
Namespace jentos.locale
' create global const to use it everywhere
'
Const Locale:=New Localization
#Rem monkeydoc Localization class.
You can load data from file or folder.
File is json format, with keys:
"langs":["ru","en", etc] - array with all supported langs
"default":"en" - default lang
"ru":{},"en":{}, etc - json objects with key-value for each localized string
Folder must contains info.json with json data of
"langs" and "default" like as file above.
and all key-value pairs must be put in separated
files: en.json, ru.json, etc
#End
Class Localization
#Rem monkeydoc Emitted when lang was changes
#End
Field LangChanged:Void()
#Rem monkeydoc Create new instance.
You can use Locale const all the time and
don't create new instances yourself.
#End
Method New( defLocale:String="en" )
_def=defLocale
' built-in loaders
'
RegisterLoader( "json",New JsonLocLoader )
RegisterLoader( "ini",New IniLocLoader )
End
#Rem monkeydoc Load localization data from file or folder.
#End
Method Load( path:String,lang:String="" )
_path=path
_lang=lang ?Else _def
Local type:=GetFileType( path )
If type=FileType.File
LoadFile( path )
Elseif type=FileType.Directory
LoadFolder( path )
Else
LoadDefaults()
Endif
LangChanged()
End
#Rem monkeydoc Set desired current lang.
#End
Method SetLang( lang:String,forceReload:Bool=False )
If lang=_lang And Not forceReload Return
_lang=lang ?Else _def
If _allInOneFile
_cur=_data[_lang]
Else
LoadFolder( _path )
Endif
ReLocalizeAll()
LangChanged()
End
#Rem monkeydoc Return localized string by key.
#End
Operator []:String( key:String )
If Not _providers.Empty
Local found:=False
For Local provide:=Eachin _providers
Local value:=provide( key,Varptr found )
If found Return value
Next
Endif
If Not _overrides.Empty
Local items:=_overrides[_lang]
If items And items.Contains( key )
Return items[key]
Endif
Endif
If _cur.Contains( key ) Return _cur[key]
Local s:="$$"+key+"$$"
_cur[key]=s
Return s
End
#Rem monkeydoc Return array of all available langs.
#End
Property AllLangs:String[]()
Return _langs
End
#Rem monkeydoc Return default lang.
#End
Property DefaultLang:String()
Return _def
End
#Rem monkeydoc Return current lang.
#End
Property Lang:String()
Return _lang
End
#Rem monkeydoc Bind any function with signature of 'Void(String)' - store it internally,
and automatically call when lang will be changed.
#End
Method Bind( key:String,func:Void(String) )
func( Self[key] )
Local items:=_bindedFuncs[key]
If Not items
items=New Stack<Void(String)>
_bindedFuncs[key]=items
Endif
If Not items.Contains( func )
items.Add( func )
Endif
End
#Rem monkeydoc You can unbind if you want.
#End
Method UnBind<T>( target:T ) Where T Implements Localizable
For Local key:=Eachin _bindedItems.Keys
Local items:=_bindedItems[key]
Local ok:=items.Remove( target )
If ok Exit
Next
End
#Rem monkeydoc You can unbind if you want.
#End
Method UnBind( func:Void(String) )
For Local key:=Eachin _bindedFuncs.Keys
Local funcs:=_bindedFuncs[key]
Local ok:=funcs.Remove( func )
If ok Exit
Next
End
#Rem monkeydoc Add value to override data.
This value will be used instead of loaded from disk.
#End
Method AddOverrided( lang:String,key:String,value:String,updateBindings:Bool=True )
Local items:=_overrides[lang]
If Not items
items=New StringMap<String>
_overrides[lang]=items
Endif
items[key]=value
If updateBindings
ReLocalizeKey( key )
Endif
End
#Rem monkeydoc Remove value from overrided.
#End
Method RemoveOverrided( lang:String,key:String )
Local items:=_overrides[lang]
If items
items.Remove( key )
Endif
End
#Rem monkeydoc Register loader for desired file format.
Format is extension of file w/o dot at the beginning ("json","csv").
Note: every registered loader overrides existing one.
#End
Function RegisterLoader( format:String,loader:LocLoader )
_loaders[format]=loader
End
#Rem monkeydoc Register provider that will be used inside of indexer [] operator.
Each provider return value for a key, and set 'eaten' flag.
If eaten flag set to true - localization return value from this provider.
Else looking at next registered provider.
And then try to find value inside of itself.
#End
Function RegisterValuesProvider( providerFunc:String(key:String,eaten:Bool Ptr) )
_providers.Add( providerFunc )
End
Protected
Field _data:StringMap<StringMap<String>>
Private
Field _lang:String
Field _overrides:=New StringMap<StringMap<String>>
Field _cur:StringMap<String>
Field _langs:String[]
Field _def:String
Field _format:String
Field _allInOneFile:Bool
Field _path:String
Field _bindedFuncs:=New StringMap<Stack<Void(String)>>
Global _loaders:=New StringMap<LocLoader>
Global _providers:=New Stack<String(String,Bool Ptr)>
Method LoadFile( path:String )
_format=ExtractExt( path ).Slice( 1 ) ' strip dot
Local loader:=_loaders[_format]
Assert( loader<>Null,"Unsupported format - '"+_format+"' !" )
_data=loader.LoadAll( path )
_langs=loader.Langs
_def=loader.DefaultLang
_cur=_data[_lang]
_allInOneFile=True
End
Method LoadFolder( dir:String )
If Not dir.EndsWith( "/" ) Then dir+="/"
Local jinfo:=JsonObject.Load( dir+"info.json" )
Assert( jinfo<>Null,"Can't load localization config from "+dir+"info.json!" )
Local info:=LocInfo.Parse( jinfo )
_def=info.def
_format=info.format
_langs=info.langs
Local loader:=_loaders[_format]
Assert( loader<>Null,"Unsupported format - '"+_format+"' !" )
_cur=loader.LoadLang( dir+_lang+"."+_format,_lang )
Assert( _cur<>Null,"Can't load localization data for "+_lang+" lang!" )
_allInOneFile=False
End
Method LoadDefaults()
' TODO
'_cur=_data.ToObject()
_allInOneFile=True
End
Method ReLocalizeAll()
For Local key:=Eachin _bindedFuncs.Keys
Local funcs:=_bindedFuncs[key]
For Local f:=Eachin funcs
f( Self[key] )
Next
Next
End
Method ReLocalizeKey( key:String )
Local funcs:=_bindedFuncs[key]
If funcs
For Local f:=Eachin funcs
f( Self[key] )
Next
Endif
End
End
#Rem monkeydoc Call Localization.Bind() on target and return itself.
#End
Function Localized<T>:T( key:String,target:T )
Locale.Bind( key,target.Localize )
Return target
End
#Rem monkeydoc Create *New T(string)*, call Localization.Bind() on it and return it.
#End
Function Localized<T>:T( key:String )
Local target:=New T( "" )
Locale.Bind( key,target.Localize )
Return target
End
#Rem monkeydoc Call Localization.Bind() on func and return itself.
#End
Function Localized:Void(String)( key:String,func:Void(String) )
Locale.Bind( key,func )
Return func
End
#Rem monkeydoc Helper to get localized string.
#End
Function Localized:String( key:String )
Return Locale[key]
End
#Rem monkeydoc Wrapper stored localized string.
You can use it as a plain string thanks to To:String operator.
#End
Class LocString
Method New( key:String )
Locale.Bind( key,Self.Localize )
End
Method Localize( t:String )
_val=t
End
Operator To:String()
Return _val
End
Private
Field _val:String
End
Class LocLoader
Property Format:String()
Return _format
End
Property Langs:String[]()
Return _langs
End
Property DefaultLang:String()
Return _def
End
Property Data:StringMap<StringMap<String>>()
Return _data
End
Method LoadAll:StringMap<StringMap<String>>( path:String ) Abstract
Method LoadLang:StringMap<String>( path:String,lang:String ) Abstract
Protected
Field _data:StringMap<StringMap<String>>
Field _def:String
Field _langs:String[]
Field _format:String
End
Class LocInfo
Field def:String
Field langs:String[]
Field format:String
Function Parse:LocInfo( json:JsonObject )
Local info:=New LocInfo
info.def=json.GetString( "default" )
info.format=json.GetString( "format" )
Local jarr:=json.GetArray( "langs" )
info.langs=New String[jarr.Data.Length]
For Local i:=0 Until info.langs.Length
info.langs[i]=jarr.Data[i].ToString()
Next
Return info
End
End
Class JsonLocLoader Extends LocLoader
Method LoadAll:StringMap<StringMap<String>>( path:String ) Override
Local data:=JsonObject.Load( path )
Local info:=LocInfo.Parse( data )
_def=info.def
_format="json"
_langs=info.langs
_data=New StringMap<StringMap<String>>
For Local lang:=Eachin _langs
_data[lang]=FromJson( data[lang].ToObject() )
Next
Return _data
End
Method LoadLang:StringMap<String>( path:String,lang:String ) Override
Local data:=JsonObject.Load( path )
_data=New StringMap<StringMap<String>>
_data[lang]=FromJson( data[lang].ToObject() )
Return _data[lang]
End
Private
Method FromJson:StringMap<String>( data:StringMap<JsonValue> )
Local map:=New StringMap<String>
For Local key:=Eachin data.Keys
map[key]=data[key].ToString()
Next
Return map
End
End
Class IniLocLoader Extends LocLoader
Method LoadAll:StringMap<StringMap<String>>( path:String ) Override
Local lines:=LoadString( path,True ).Split( "~n" )
_data=New StringMap<StringMap<String>>
Local map:StringMap<String>
For Local line:=Eachin lines
line=line.Trim()
If Not line Continue
If line.StartsWith( "[" )
Local group:=line.Slice( 1,line.Length-1 )
map=New StringMap<String>
_data[group]=map
Else
Local pair:=line.Split( "=" )
map[pair[0].Trim()]=pair[1].Trim().Replace( "\n","~n" )
Endif
Next
_def=_data["info"]["default"]
_langs=_data["info"]["langs"].Split( "," )
_format="ini"
Return _data
End
Method LoadLang:StringMap<String>( path:String,lang:String ) Override
Local lines:=LoadString( path,True ).Split( "~n" )
_data=New StringMap<StringMap<String>>
Local map:=New StringMap<String>
For Local line:=Eachin lines
line=line.Trim()
If line And Not line.StartsWith( "[" )
Local pair:=line.Split( "=" )
map[pair[0].Trim()]=pair[1].Trim().Replace( "\n","~n" )
Endif
Next
_data[lang]=map
Return map
End
End