-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathResBundle.java
More file actions
691 lines (623 loc) · 20.7 KB
/
ResBundle.java
File metadata and controls
691 lines (623 loc) · 20.7 KB
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
/**
* ResBundle.java - An ilib resource bundle
*
* Copyright 2013, JEDLSoft, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.ilib;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import org.json.JSONException;
import org.json.JSONObject;
/**
* ResBundle
*
* @author edwin
*/
public class ResBundle
{
// protected static final File PSEUDOROOT = new File("js/data/locale");
protected static final String HTML_TYPE = "html";
protected static final String XML_TYPE = "xml";
protected static final String RAW_TYPE = "raw";
protected static final String DEFAULT_RESOURCES = "resources";
protected static final String DEFAULT_TEXT_TYPE = "text";
protected static final String DEFAULT_RES_FILE = "strings.json";
protected static final String PSEUDO_JSON = "pseudomap.json";
protected static final String RESOURCES_JSON = "resources.json";
protected static Map<String, String> sourceResources = null;
protected static volatile IlibLocale sourceLocale = new IlibLocale("en-US");
protected IlibLocale targetLocale;
protected String name;
protected String type;
protected Map<String, String> translations = null;
protected Map<String, String> pseudoCharacters = null;
protected boolean lengthen;
protected MissingType missing = MissingType.SOURCE;
protected String resourceFilename;
public enum MissingType {
SOURCE,
PSEUDO,
EMPTY
};
/**
*
*/
public ResBundle()
{
this(null, null, null);
}
/**
* @param name
*/
public ResBundle(String name)
{
this(name, null, null, null);
}
/**
* @param name
* @param locale
*/
public ResBundle(String name, IlibLocale locale)
{
this(name, locale, null, null);
}
/**
* @param name
* @param locale
* @param type
*/
public ResBundle(String name, IlibLocale locale, String type)
{
this(name, locale, type, null);
}
/**
*
* @param name
* @param locale
* @param type
* @param resFileName
*/
public ResBundle(String name, IlibLocale locale, String type, String resFileName)
{
this.targetLocale = (locale != null && !locale.toString().isEmpty()) ? locale : new IlibLocale("en-US");
this.name = (name != null) ? name : DEFAULT_RESOURCES;
this.type = (type != null) ? type : DEFAULT_TEXT_TYPE;
this.resourceFilename = (resFileName != null) ? resFileName : DEFAULT_RES_FILE;
lengthen = true;
initResources();
}
protected void initResources()
{
initResourcesMap(name);
initPseudoMap();
loadAllTranslations(resourceFilename);
}
private static void initResourcesMap(String resourcesDir)
{
if (sourceResources != null) return;
sourceResources = new HashMap<>();
File inputFile = new File(resourcesDir + File.separator + RESOURCES_JSON);
if (!inputFile.exists()) return;
StringBuilder builder = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile), "utf-8")); ) {
String currentLine = null;
while ( (currentLine = reader.readLine()) != null ) {
builder.append(currentLine);
}
} catch (FileNotFoundException ex) {
System.err.println("Exception in file: " + inputFile.getPath() + ", file is missing or does not exist.");
} catch (IOException e) {
e.printStackTrace();
return;
}
try {
JSONObject pseudoJSON = new JSONObject(builder.toString());
if ( pseudoJSON != null ) {
Iterator<String> it = pseudoJSON.keys();
String p;
while ( it.hasNext() ) {
p = it.next();
sourceResources.put(p, pseudoJSON.getString(p));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
protected void loadAllTranslations(String filename) throws IllegalArgumentException
{
if (!filename.endsWith(".json"))
throw new IllegalArgumentException("File " + filename + " has another extension instead of plain JSON extension.");
Set<String> resourceFiles = new LinkedHashSet<>();
String language = targetLocale.getLanguage(),
region = targetLocale.getRegion(),
script = targetLocale.getScript(),
variant = targetLocale.getVariant();
String separator = File.separator;
String resources_path = name;
StringBuilder tmp_path = new StringBuilder();
tmp_path.append(resources_path).append(separator).append(filename);
resourceFiles.add(tmp_path.toString());
if (!language.isEmpty()) {
tmp_path.setLength(0);
tmp_path.append(resources_path).append(separator).append(language).append(separator).append(filename);
resourceFiles.add(tmp_path.toString());
}
if (!region.isEmpty()) {
tmp_path.setLength(0);
tmp_path.append(resources_path).append(separator).append("und").append(separator).append(region).append(separator).append(filename);
resourceFiles.add(tmp_path.toString());
}
if (!language.isEmpty()) {
if (!script.isEmpty()) {
tmp_path.setLength(0);
tmp_path.append(resources_path).append(separator).append(language).append(separator).append(script).append(separator).append(filename);
resourceFiles.add(tmp_path.toString());
}
if (!region.isEmpty()) {
tmp_path.setLength(0);
tmp_path.append(resources_path).append(separator).append(language).append(separator).append(region).append(separator).append(filename);
resourceFiles.add(tmp_path.toString());
}
}
if (!region.isEmpty() && !variant.isEmpty()) {
tmp_path.setLength(0);
tmp_path.append(resources_path).append(separator).append("und").append(separator).append(region).append(separator)
.append(variant).append(separator).append(filename);
resourceFiles.add(tmp_path.toString());
}
if (!language.isEmpty() && !script.isEmpty() && !region.isEmpty()) {
tmp_path.setLength(0);
tmp_path.append(resources_path).append(separator).append(language).append(separator).append(script).append(separator)
.append(region).append(separator).append(filename);
resourceFiles.add(tmp_path.toString());
}
if (!language.isEmpty() && !region.isEmpty() && !variant.isEmpty()) {
tmp_path.setLength(0);
tmp_path.append(resources_path).append(separator).append(language).append(separator).append(region).append(separator)
.append(variant).append(separator).append(filename);
resourceFiles.add(tmp_path.toString());
}
if (!language.isEmpty() && !script.isEmpty() && !region.isEmpty()) {
tmp_path.setLength(0);
tmp_path.append(resources_path).append(separator).append(language).append(separator).append(script).append(separator)
.append(region).append(separator).append(variant).append(separator).append(filename);
resourceFiles.add(tmp_path.toString());
}
translations = new LinkedHashMap<>();
for (String resfile : resourceFiles)
loadTranslationsFromFile(resfile);
resourceFiles.clear();
}
protected void loadTranslationsFromFile(String filename)
{
File inputFile = new File(filename);
if (!inputFile.exists()) return;
StringBuilder builder = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile), "utf-8")); ) {
String currentLine = null;
while ( (currentLine = reader.readLine()) != null ) {
builder.append(currentLine);
}
} catch (FileNotFoundException ex) {
System.err.println("Exception in file: " + filename + ", file is missing or does not exist.");
return;
} catch (IOException e) {
e.printStackTrace();
}
try {
JSONObject pseudoJSON = new JSONObject(builder.toString());
if ( pseudoJSON != null ) {
Iterator<String> it = pseudoJSON.keys();
String p;
while ( it.hasNext() ) {
p = it.next();
translations.put(p, pseudoJSON.getString(p));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
protected void initPseudoMap()
{
pseudoCharacters = new LinkedHashMap<>();
StringBuilder script = new StringBuilder();
//switch(targetLocale.getScript()) {
switch( ScriptInfo.getScriptByLocale(targetLocale) ) {
case "Cyrl":
script = script.append("zxx").append(File.separator).append("Cyrl").append(File.separator);
break;
case "Hans":
script = script.append("zxx").append(File.separator).append("Hans").append(File.separator);
break;
case "Hebr":
script = script.append("zxx").append(File.separator).append("Hebr").append(File.separator);
break;
default:
break;
}
String result = null;
ClassLoader cl = this.getClass().getClassLoader();
// File pseudoMapFile = new File(PSEUDOROOT, script.toString() + PSEUDO_JSON);
try {
InputStream is = cl.getResourceAsStream("locale/" + script.toString() + "/" + PSEUDO_JSON);
if (is != null) {
Scanner scanner = new Scanner(is, "utf-8");
result = scanner.useDelimiter("\\A").next();
scanner.close();
JSONObject pseudoJSON = new JSONObject(result);
if ( pseudoJSON != null ) {
Iterator<String> it = pseudoJSON.keys();
String p;
while ( it.hasNext() ) {
p = it.next();
pseudoCharacters.put(p, pseudoJSON.getString(p));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Specifies source locale for all threads in current process
* @param locale source locale
*/
public static void setSourceLocale(IlibLocale locale)
{
sourceLocale = locale;
}
/**
* Returns source locale for all threads in current process
* @return source locale
*/
public static IlibLocale getSourceLocale()
{
return sourceLocale;
}
/**
* Specifies resources filename; default file is strings.json
* @param filename resource filename of JSON file to look up for translations
*/
public void setResourceFileName(String filename)
{
resourceFilename = filename;
}
/**
* Returns target locale object in BCP47 format
* @return target locale instance
*/
public IlibLocale getLocale()
{
return targetLocale;
}
/**
* Examines if source string has already been translated or not
*
* @param source source string to be examined
* @return true if source string is found in translations, otherwise - false
*/
public boolean containsSource(String source)
{
return translations.containsKey(source);
}
/**
* Returns given name for resources location which are seeking for for translations
* @return given name for resources with translations
*/
public String getName()
{
return name;
}
/**
* Returns type of text information which ResBundle is being operating with (default type: raw)
* @return input text type: html, xml, raw
*/
public String getType()
{
return type;
}
/**
* Specifies type of text information which ResBundle is being operating with
* @param type input text specified type (default type: raw).
*/
public void setType(String type) {
this.type = type.toLowerCase();
}
/**
* Returns lengthen option that allows to lengthen potential length for translated string (for UI/UX issues)
* using pseudo-localization.
* @return true if lengthen option is specified, false otherwise
*/
public boolean isLengthen() {
return lengthen;
}
/**
* Specifies lengthen option to lengthen potential length for translated string (for UI/UX issues)
* using pseudo-localization.
* @param lengthen option allows to lengthen potential translated string length using pseudo string
* as returned instead.
*/
public void setLengthen(boolean lengthen) {
this.lengthen = lengthen;
}
/**
* Returns missing option - that option specifies behavior in case when translation is not found
* by given key or value.
*
* <info>
* Possible values:
* SOURCE - return source string if translation is not found;
* PSEUDO - return pseudo localized string from source string if translation is not found;
* EMPTY - return empty string if translation is not found.
* </info>
* @return missing option value
*/
public MissingType getMissing() {
return missing;
}
/**
* Specifies missing option - option, that specifies behavior in case when translation is not found
* by given key or value.
*
* <info>
* Possible values:
* SOURCE - return source string if translation is not found;
* PSEUDO - return pseudo localized string from source string if translation is not found;
* EMPTY - return empty string if translation is not found.
* </info>
* @param type
*/
public void setMissingType(MissingType type) {
this.missing = type;
}
/**
* Returns pseudo string from input one using appropriate pseudomap
* @param source input string needed to be pseudo localized
* @return pseudo localized string
*/
protected String pseudo(String source)
{
if ( source == null ) {
return null;
}
StringBuilder ret = new StringBuilder();
int i;
for ( i = 0; i < source.length(); i++ ) {
if ( !type.equals(RAW_TYPE) ) {
if ( isHTML_XML_Type() ) {
if (source.charAt(i) == '<') {
ret.append(source.charAt(i++));
while (i < source.length() && source.charAt(i) != '>') {
ret.append(source.charAt(i++));
}
if (i < source.length()) {
ret.append(source.charAt(i++));
}
} else if (source.charAt(i) == '&') {
ret.append(source.charAt(i++));
while (i < source.length() && source.charAt(i) != ';' && source.charAt(i) != ' ') {
ret.append(source.charAt(i++));
}
if (i < source.length()) {
ret.append(source.charAt(i++));
}
}
}
if (i < source.length()) {
if (source.charAt(i) == '{') {
ret.append(source.charAt(i++));
while (i < source.length() && source.charAt(i) != '}') {
ret.append(source.charAt(i++));
}
if (i < source.length()) {
ret.append(source.charAt(i));
}
} else {
String c = source.substring(i, i+1);
ret.append( getPseudoCharacter(c) );
}
}
} else {
String c = source.substring(i, i+1);
ret.append( getPseudoCharacter(c) );
}
}
if (lengthen) {
int add;
if (ret.length() <= 20) {
add = Math.round(ret.length() / 2);
} else if (ret.length() > 20 && ret.length() <= 40) {
add = Math.round(ret.length() / 3);
} else {
add = Math.round(ret.length() / 5);
}
for (i = add-1; i >= 0; i--) {
ret.append("" + (i % 10));
}
}
return ret.toString();
}
/**
* Returns string with pseudo character that corresponds to the input one
* @param character input character string
* @return string with pseudo character if input character is found in appropriate pseudoMap,
* otherwise - input character
*/
protected String getPseudoCharacter(String character)
{
return pseudoCharacters.containsKey(character) ? pseudoCharacters.get(character) : character;
}
/**
* Replaces all xml and html tags occurences with corresponding replacements
* @param str input string
* @return string with no html- and xml-style tags
*/
protected String escape(String str)
{
if ( str == null )
return null;
String ret;
ret = str.replaceAll("&", "&");
ret = ret.replaceAll("<", "<");
ret = ret.replaceAll(">", ">");
return ret;
}
/**
* Invokes retroaction for escape(String)
*
* @see String escape(String str);
* @param str input string
* @return string with html- and xml-style tags
*/
protected String unescape(String str)
{
if ( str == null )
return null;
String ret;
ret = str.replaceAll("&", "&");
ret = ret.replaceAll("<", "<");
ret = ret.replaceAll(">", ">");
return ret;
}
/**
* Creates a unique key from given source string
* @param source input source string
* @return unique key that contains no spaces, and modified equals and colon signs
*/
protected String makeKey(String source)
{
if ( source == null )
return null;
String ret;
// compress all whitespace so that they don't matter to the key
ret = source.replaceAll("\\s+", "\\ ");
// need to escape these chars because they are special for properties files
ret = source.replaceAll("=", "\\=");
ret = source.replaceAll(":", "\\:");
return isHTML_XML_Type() ? unescape(ret) : ret;
}
/**
* Returns translation for given source and key strings in IString format
* @param source source string
* @param key key item, if null - then unique key will be generated
* @see #makeKey(String)
* @return translation for target locale if it is exists, otherwise the source string
* @see MissingType
*/
public IString getString(String source, String key)
{
if (source == null && key == null) return null;
if (targetLocale.isPseudo()) {
String str = (source != null) ? source : (translations != null ? translations.get(key) : key);
return new IString(pseudo(str), targetLocale.getLanguage());
}
String trans = getTranslation(source, key);
return new IString(trans, targetLocale.getLanguage());
}
/**
*
* @param source
* @param key
* @return the translation for the given source string/key combination
*/
protected String getTranslation(String source, String key)
{
String keyName = null;
if (key != null && key.length() > 0) {
keyName = key;
} else {
if (source != null)
keyName = makeKey(source);
else {
System.err.println("Incorrect translation parameters: either key or source are both null!");
return "";
}
}
if ( sourceLocale.equals(targetLocale.toString() )) {
String trans = (source != null) ? source : keyName;
if (isHTML_XML_Type()) trans = escape(trans);
return trans;
}
String trans = null;
switch(missing) {
case SOURCE:
trans = ( translations != null && translations.containsKey(keyName) ) ? translations.get(keyName) :
( sourceResources.get(keyName) != null ? sourceResources.get(keyName) : keyName );
break;
case PSEUDO:
String pseudoItem = (source != null) ? pseudo(source) :
( (translations != null && translations.get(keyName) != null) ? pseudo(translations.get(keyName)) :
(sourceResources.get(keyName) != null ?
pseudo(sourceResources.get(keyName)) : key) );
trans = ( translations != null && translations.containsKey(keyName) ) ? translations.get(keyName) : pseudoItem;
break;
case EMPTY:
trans = ( translations != null && translations.containsKey(keyName) ) ? translations.get(keyName) : "";
break;
default:
trans = ( translations != null && translations.containsKey(keyName) ) ? translations.get(keyName) : source;
break;
}
if (isHTML_XML_Type()) trans = escape(trans);
return trans;
}
protected boolean isHTML_XML_Type()
{
return (type.equals(XML_TYPE) || type.equals(HTML_TYPE));
}
/**
* @param source
* @return the translation for the given source string
*/
public IString getString(String source)
{
return getString(source, null);
}
/**
* Return the translation for the given source string in a Java "String" instead of a formattable "IString".
* @param source
* @return the translation for the given source string as a Java "String"
*/
public String getStringJ(String source)
{
IString i = getString(source, null);
return i != null ? i.toString() : null;
}
/**
*
* @param source
* @return the pseudo-translated version of the source string
*/
public IString getStringPseudo(String source) {
if (source == null) return null;
return new IString(pseudo(source), targetLocale.getLanguage());
}
}