GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fred/src/freenet/l10n/BaseL10n.java at master · hyphanet/fred

Expand file tree

/

BaseL10n.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
845 lines (759 loc) · 30.4 KB
/

BaseL10n.java

Copy path

File metadata and controls

845 lines (759 loc) · 30.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
/* This code is part of Freenet. It is distributed under the GNU General
* Public License, version 2 (or at your option any later version). See
* http://www.gnu.org/ for further details of the GPL. */
package freenet.l10n;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.NoSuchElementException;
import freenet.clients.http.TranslationToadlet;
import freenet.support.HTMLEncoder;
import freenet.support.HTMLNode;
import freenet.support.Logger;
import freenet.support.SimpleFieldSet;
import freenet.support.io.Closer;
import freenet.support.io.FileUtil;
/**
* This is the core of all the localization stuff. This method can get
* localized strings from any SimpleFieldSet file, and can, if necessary,
* use a custom ClassLoader. The language can be changed at anytime.
*
* Note : do not use this class *as is*, use NodeL10n.getBase() or
* PluginL10n.getBase().
*
* Note : this class also supports using/saving/editing overridden translations.
* @author Florent Daignière <[email protected]>
* @author Artefact2
*/
public class BaseL10n {
/**
* Central list of languages and the codes to identify them.
* When adding new ones, use ISO639-2 or 3 if there is a code for the desired language available
* there. If not, fall back to RFC5646 (= "IETF language tags").
*
* TODO: Code quality: Switch from this manually maintained list to a predefined one. Use
* standard Java class {@link Locale}.
* Discussion at https://github.com/freenet/fred/pull/500 has shown that the IETF list is
* the best choice. It is a combination of ISO639-3 codes (for which we already have class
* {@link ISO639_3}) and a standard country list. And most importantly: It is understood
* by standard Java class {@link Locale}.
* Bugtracker entry for this: http://bugs.hyphanet.org/view.php?id=6857
*
* @see "http://www.omniglot.com/language/names.htm"
* @see "http://loc.gov/standards/iso639-2/php/code_list.php"
* @see "http://tools.ietf.org/html/rfc5646" */
public enum LANGUAGE {
// Windows language codes must be preceded with WINDOWS and be in upper case hex, 4 digits.
// See http://www.autohotkey.com/docs/misc/Languages.htm
CROATIAN("hr", "Hrvatski", "hrv", new String[] { "WINDOWS041A" }),
ENGLISH("en", "English", "eng", new String[] { "WINDOWS0409", "WINDOWS0809", "WINDOWS0C09", "WINDOWS1009", "WINDOWS1409", "WINDOWS1809", "WINDOWS1C09", "WINDOWS2009", "WINDOWS2409", "WINDOWS2809", "WINDOWS2C09", "WINDOWS3009", "WINDOWS3409"}),
HUNGARIAN("hu", "magyar", "hun", new String[] { "WINDOWS040E" }),
SPANISH("es", "Español", "spa", new String[] { "WINDOWS040A", "WINDOWS080A", "WINDOWS0C0A", "WINDOWS100A", "WINDOWS140A", "WINDOWS180A", "WINDOWS1C0A", "WINDOWS200A", "WINDOWS240A", "WINDOWS280A", "WINDOWS2C0A", "WINDOWS300A", "WINDOWS340A", "WINDOWS380A", "WINDOWS3C0A", "WINDOWS400A", "WINDOWS440A", "WINDOWS480A", "WINDOWS4C0A", "WINDOWS500A"}),
DANISH("da", "Dansk", "dan", new String[] { "WINDOWS0406" }),
DUTCH("nl", "Nederlands", "nld", new String[] { "WINDOWS0413", "WINDOWS0813"}),
GERMAN("de", "Deutsch", "deu", new String[] { "WINDOWS0407", "WINDOWS0807", "WINDOWS0C07", "WINDOWS1007", "WINDOWS1407"}),
FINNISH("fi", "Suomi", "fin", new String[] { "WINDOWS040B"}),
FRENCH("fr", "Français", "fra", new String[] { "WINDOWS040C", "WINDOWS080C", "WINDOWS0C0C", "WINDOWS100C", "WINDOWS140C", "WINDOWS180C"}),
ITALIAN("it", "Italiano", "ita", new String[] { "WINDOWS0410", "WINDOWS0810"}),
// TODO: This does not adhere to RFC5646. Fix it as part of changing the whole list to
// RFC5646. Find a way to rename this without breaking the language in all plugins.
NORWEGIAN("nb-no", "Bokmål", "nob", new String[] { "WINDOWS0414", "WINDOWS0814"}),
POLISH("pl", "Polski", "pol", new String[] { "WINDOWS0415"}),
SWEDISH("sv", "Svenska", "swe", new String[] { "WINDOWS041D", "WINDOWS081D"}),
// TODO: This does not adhere to RFC5646. Fix it as part of changing the whole list to
// RFC5646. Find a way to rename this without breaking the language in all plugins.
CHINESE("zh-cn", "中文(简体)", "chn", new String[] { "WINDOWS0804", "WINDOWS1004" }),
// simplified chinese, used on mainland, Singapore and Malaysia
// TODO: This does not adhere to RFC5646. Fix it as part of changing the whole list to
// RFC5646. Find a way to rename this without breaking the language in all plugins.
CHINESE_TAIWAN("zh-tw", "中文(繁體)", "zh-tw", new String[] { "WINDOWS0404", "WINDOWS0C04", "WINDOWS1404" }),
// traditional chinese, used in Taiwan, Hong Kong and Macau
RUSSIAN("ru", "Русский", "rus", new String[] { "WINDOWS0419" }), // Just one variant for russian. Belorussian is separate, code page 423, speakers may or may not speak russian, I'm not including it.
JAPANESE("ja", "日本語", "jpn", new String[] { "WINDOWS0411" }),
PORTUGUESE("pt-PT", "Português do Portugal", "pt", new String[] { "WINDOWS0816" }),
// TODO: This does not adhere to RFC5646. Fix it as part of changing the whole list to
// RFC5646. Find a way to rename this without breaking the language in all plugins.
BRAZILIAN_PORTUGUESE("pt-br", "Português do Brasil", "pt-br", new String[] { "WINDOWS0416" }),
GREEK("el", "Ελληνικά", "ell", new String[] { "WINDOWS0408" }),
UNLISTED("unlisted", "unlisted", "unlisted", new String[] {});
/** The identifier we use internally : MUST BE UNIQUE! */
public final String shortCode;
/** The identifier shown to the user */
public final String fullName;
/** The mapping with the installer's l10n (@see bug #2424); MUST BE UNIQUE! */
public final String isoCode;
public final String[] aliases;
private LANGUAGE(String shortCode, String fullName, String isoCode, String[] aliases) {
this.shortCode = shortCode;
this.fullName = fullName;
this.isoCode = isoCode;
this.aliases = aliases;
}
LANGUAGE(LANGUAGE l) {
this(l.shortCode, l.fullName, l.isoCode, l.aliases);
}
/**
* Create a new LANGUAGE object from either its short code, its full
* name or its ISO code.
* @param whatever Short code, full name or ISO code.
* @return LANGUAGE
*/
public static LANGUAGE mapToLanguage(String whatever) {
for (LANGUAGE currentLanguage : LANGUAGE.values()) {
if (currentLanguage.shortCode.equalsIgnoreCase(whatever) ||
currentLanguage.fullName.equalsIgnoreCase(whatever) ||
currentLanguage.isoCode.equalsIgnoreCase(whatever) ||
currentLanguage.toString().equalsIgnoreCase(whatever)) {
return currentLanguage;
}
if(currentLanguage.aliases != null) {
for(String s : currentLanguage.aliases)
if(whatever.equalsIgnoreCase(s)) return currentLanguage;
}
}
return null;
}
public static String[] valuesWithFullNames() {
LANGUAGE[] allValues = values();
ArrayList<String> result = new ArrayList<String>(allValues.length);
for (int i = 0; i < allValues.length; i++) {
// We will return the full names sorted alphabetically. To ensure that the user
// notices the special "UNLISTED" language code, we add it to the end of the list
// after sorting, so now we skip it.
if(allValues[i] != UNLISTED)
result.add(allValues[i].fullName);
}
Collections.sort(result);
result.add(UNLISTED.fullName);
return result.toArray(new String[result.size()]);
}
public static LANGUAGE getDefault() {
return ENGLISH;
}
}
/**
* State enum for {@link L10nStringIterator}. Declared here for
* {@link #getStrings(String, FallbackState)}.
*/
private enum FallbackState {
CURRENT_LANG,
FALLBACK_LANG,
KEY,
END
}
/**
* Iterator that returns the strings associated with a key in order of preference. First the
* value in the current language (if any), then the value in the fallback language (if any),
* and then just the key itself.
*/
private class L10nStringIterator implements Iterator<String> {
private final String key;
private FallbackState state;
public L10nStringIterator(String key, FallbackState state) {
this.key = key;
this.state = state;
}
@Override
public boolean hasNext() {
return state != FallbackState.END;
}
@Override
public String next() {
if (state == FallbackState.CURRENT_LANG) {
state = FallbackState.FALLBACK_LANG;
String value = getString(key, true);
if (value != null) {
return value;
}
}
if (state == FallbackState.FALLBACK_LANG) {
state = FallbackState.KEY;
if (getSelectedLanguage() != LANGUAGE.getDefault()) {
String value = getFallbackString(key);
if (value != null) {
return value;
}
}
}
if (state == FallbackState.KEY) {
state = FallbackState.END;
return key;
}
throw new NoSuchElementException();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
private LANGUAGE lang;
private String l10nFilesBasePath;
private String l10nFilesMask;
private String l10nOverrideFilesMask;
private SimpleFieldSet currentTranslation = null;
private SimpleFieldSet fallbackTranslation = null;
private SimpleFieldSet translationOverride;
private ClassLoader cl;
private static ClassLoader getClassLoaderFallback() {
ClassLoader _cl;
// getClassLoader() can return null on some implementations if the boot classloader was used.
_cl = BaseL10n.class.getClassLoader();
if (_cl == null) {
_cl = ClassLoader.getSystemClassLoader();
}
return _cl;
}
public BaseL10n(String l10nFilesBasePath, String l10nFilesMask, String l10nOverrideFilesMask) {
this(l10nFilesBasePath, l10nFilesMask, l10nOverrideFilesMask, LANGUAGE.getDefault());
}
public BaseL10n(String l10nFilesBasePath, String l10nFilesMask, String l10nOverrideFilesMask, final LANGUAGE lang) {
this(l10nFilesBasePath, l10nFilesMask, l10nOverrideFilesMask, lang, getClassLoaderFallback());
}
/**
* Create a new BaseL10n object.
*
* Note : you shouldn't have to run this yourself. Use PluginL10n or NodeL10n.
* @param l10nFilesBasePath Base path of the l10n files, ex. "com/mycorp/myproject/l10n"
* @param l10nFilesMask Mask of the l10n files, ex. "messages_${lang}.l10n"
* @param l10nOverrideFilesMask Same as l10nFilesMask, but for overridden messages.
* @param lang Language to use.
* @param cl ClassLoader to use.
*/
public BaseL10n(String l10nFilesBasePath, String l10nFilesMask, String l10nOverrideFilesMask, final LANGUAGE lang, final ClassLoader cl) {
if (!l10nFilesBasePath.endsWith("/")) {
l10nFilesBasePath += "/";
}
this.l10nFilesBasePath = l10nFilesBasePath;
this.l10nFilesMask = l10nFilesMask;
this.l10nOverrideFilesMask = l10nOverrideFilesMask;
this.cl = cl;
this.setLanguage(lang);
}
/**
* Get the full base name of the L10n file used by the current language.
* @return String
*/
public String getL10nFileName(LANGUAGE lang) {
return this.l10nFilesBasePath + this.l10nFilesMask.replace("${lang}", lang.shortCode);
}
/**
* Get the full base name of the L10n override file used by the current language.
* @return String
*/
public String getL10nOverrideFileName(LANGUAGE lang) {
return this.l10nOverrideFilesMask.replace("${lang}", lang.shortCode);
}
/**
* Use a new language, and load the SimpleFieldSets accordingly.
* @param selectedLanguage New language to use.
* @throws MissingResourceException If the l10n file could not be found.
*/
public void setLanguage(final LANGUAGE selectedLanguage) throws MissingResourceException {
if (selectedLanguage == null) {
throw new MissingResourceException("LANGUAGE given is null !", this.getClass().getName(), "");
}
this.lang = selectedLanguage;
Logger.normal(this.getClass(), "Changing the current language to : " + this.lang);
try {
this.loadOverrideFileOrBackup();
} catch (IOException e) {
this.translationOverride = null;
Logger.error(this, "IOError while accessing the file!" + e.getMessage(), e);
}
this.currentTranslation = this.loadTranslation(lang);
if (this.currentTranslation == null) {
Logger.error(this, "The translation file for " + lang + " is invalid. The node will load an empty template.");
this.currentTranslation = null;
}
}
/**
* Try loading the override file, or the backup override file if it
* exists.
* @throws IOException
*/
private void loadOverrideFileOrBackup() throws IOException {
try (InputStream overrideInputStream = cl.getResourceAsStream(getL10nOverrideFileName(lang))) {
if (overrideInputStream != null) {
Logger.normal(this, "Override file found in classpath.");
this.translationOverride = SimpleFieldSet.readFrom(overrideInputStream, false, false);
return;
}
}
final File tmpFile = new File(this.getL10nOverrideFileName(this.lang));
if (tmpFile.exists() && tmpFile.canRead() && tmpFile.length() > 0) {
Logger.normal(this, "Override file detected : let's try to load it");
this.translationOverride = SimpleFieldSet.readFrom(tmpFile, false, false);
} else {
// try to restore a backup
final File backup = new File(tmpFile.getParentFile(), tmpFile.getName() + ".bak");
if (backup.exists() && backup.length() > 0) {
Logger.normal(this, "Override-backup file detected : let's try to load it");
this.translationOverride = SimpleFieldSet.readFrom(backup, false, false);
} else {
this.translationOverride = null;
}
}
}
/**
* Load the l10n file for a custom language and return its parsed SimpleFieldSet.
* @param lang Language to use.
* @return SimpleFieldSet
*/
private SimpleFieldSet loadTranslation(LANGUAGE lang) {
SimpleFieldSet result = null;
InputStream in = null;
try {
// Returns null on lookup failures:
in = this.cl.getResourceAsStream(this.getL10nFileName(lang));
if (in != null) {
result = SimpleFieldSet.readFrom(in, false, false);
} else {
System.err.println("Could not get resource : " + this.getL10nFileName(lang));
}
} catch (Exception e) {
System.err.println("Error while loading the l10n file from " + this.getL10nFileName(lang) + " :" + e.getMessage());
e.printStackTrace();
result = null;
} finally {
Closer.close(in);
}
return result;
}
/**
* Load the fallback translation. Synchronized.
*/
private synchronized void loadFallback() {
if (this.fallbackTranslation == null) {
this.fallbackTranslation = loadTranslation(LANGUAGE.getDefault());
if(fallbackTranslation == null)
fallbackTranslation = new SimpleFieldSet(true);
}
}
/**
* Get the language currently used by this BaseL10n.
* @return LANGUAGE
*/
public LANGUAGE getSelectedLanguage() {
return this.lang;
}
/**
* Returns true if a key is overridden.
* @param key Key to check override status
* @return boolean
*/
public boolean isOverridden(String key) {
if (this.translationOverride == null) {
return false;
}
return this.translationOverride.get(key) != null;
}
/**
* Override a custom key with a new value.
* @param key Key to override.
* @param value New value of that key.
*/
public void setOverride(String key, String value) {
key = key.trim();
value = value.trim();
// Is the override already declared ? if not, create it.
if (this.translationOverride == null) {
this.translationOverride = new SimpleFieldSet(false);
}
// If there is no need to keep it in the override, remove it...
// unless the original/default is the same as the translation
if (value.isEmpty() || (currentTranslation != null && value.equals(this.currentTranslation.get(key)))) {
this.translationOverride.removeValue(key);
} else {
value = value.replaceAll("(\r|\n|\t)+", "");
// Set the value of the override
this.translationOverride.putOverwrite(key, value);
Logger.normal(this.getClass(), "Got a new translation key: set the Override!");
}
// Save the file to disk
saveTranslationFile();
}
/**
* Save the SimpleFieldSet of overridden keys in a file.
*/
private void saveTranslationFile() {
FileOutputStream fos = null;
File finalFile = new File(this.getL10nOverrideFileName(this.lang));
try {
// We don't set deleteOnExit on it : if the save operation fails, we want a backup
File tempFile = File.createTempFile(finalFile.getName(), ".bak", finalFile.getParentFile());
Logger.minor(this.getClass(), "The temporary filename is : " + tempFile);
fos = new FileOutputStream(tempFile);
this.translationOverride.writeToBigBuffer(fos);
fos.close();
fos = null;
FileUtil.moveTo(tempFile, finalFile);
Logger.normal(this.getClass(), "Override file saved successfully!");
} catch (IOException e) {
Logger.error(this.getClass(), "Error while saving the translation override: " + e.getMessage(), e);
} finally {
Closer.close(fos);
}
}
/**
* Get a copy of the currently used SimpleFieldSet.
* @return SimpleFieldSet
*/
public SimpleFieldSet getCurrentLanguageTranslation() {
return (this.currentTranslation == null ? null : new SimpleFieldSet(currentTranslation));
}
/**
* Get a copy of the currently used SimpleFieldSet (overridden messages).
* @return SimpleFieldSet
*/
public SimpleFieldSet getOverrideForCurrentLanguageTranslation() {
return (this.translationOverride == null ? null : new SimpleFieldSet(translationOverride));
}
/**
* Get the SimpleFieldSet of the default language (should be english).
* @return SimpleFieldSet
*/
public SimpleFieldSet getDefaultLanguageTranslation() {
this.loadFallback();
return new SimpleFieldSet(this.fallbackTranslation);
}
/**
* Get a localized string. Return "" (empty string) if it doesn't exist.
* @param key Key to search for.
* @return String
*/
public String getString(String key) {
return getStrings(key).iterator().next();
}
/**
* Get a localized string. Return "" (empty string) if it doesn't exist.
* @param key Key to search for.
* @param replacementValue Replacement value for all ${*}.
* @return String
*/
public String getString(String key, String replacementValue) {
String string = getStrings(key).iterator().next();
return string.replaceAll("\\$\\{.*}", replacementValue);
}
/**
* Get a localized string.
* @param key Key to search for.
* @param returnNullIfNotFound If this is true, will return null if the key is not found.
* @return String
*/
public String getString(String key, boolean returnNullIfNotFound) {
if (!returnNullIfNotFound) {
return getString(key);
}
String result = null;
if (this.translationOverride != null) {
result = this.translationOverride.get(key);
}
if (result != null) {
return result;
}
if (this.currentTranslation != null) {
result = this.currentTranslation.get(key);
}
if (result == null) {
Logger.normal(this.getClass(), "The translation for " + key + " hasn't been found (" + this.getSelectedLanguage() + ")! please tell the maintainer.");
}
return result;
}
/**
* Enumerate strings associated with a key in order of preference.
*/
private Iterable<String> getStrings(final String key) {
return getStrings(key, FallbackState.CURRENT_LANG);
}
/**
* Enumerate strings associated with a key in order of preference, starting with a specified
* one.
*/
private Iterable<String> getStrings(final String key, final FallbackState initialState) {
return new Iterable<String>() {
@Override
public Iterator<String> iterator() {
return new L10nStringIterator(key, initialState);
}
};
}
/**
* Get a localized string and put it in a HTMLNode for the translation page.
* @param key Key to search for.
* @return HTMLNode
*/
public HTMLNode getHTMLNode(String key) {
return getHTMLNode(key, null, null);
}
/**
* Get a localized string and put it in a HTMLNode for the translation page.
* @param key Key to search for.
* @param patterns Patterns to replace. May be null, if so values must also be null.
* @param values Values to replace patterns with.
* @return HTMLNode
*/
public HTMLNode getHTMLNode(String key, String[] patterns, String[] values) {
String value = this.getString(key, true);
if (value != null) {
if(patterns != null)
return new HTMLNode("#", getString(key, patterns, values));
else
return new HTMLNode("#", value);
}
HTMLNode translationField = new HTMLNode("span", "class", "translate_it");
if(patterns != null)
translationField.addChild("#", getDefaultString(key, patterns, values));
else
translationField.addChild("#", getDefaultString(key));
translationField.addChild("a", "href", TranslationToadlet.TOADLET_URL + "?translate=" + key).addChild("small", " (translate it in your native language!)");
return translationField;
}
/**
* Get the value for a key in the fallback translation, or null.
*/
private String getFallbackString(String key) {
this.loadFallback();
String result = this.fallbackTranslation.get(key);
if (result == null) {
Logger.error(this.getClass(), "The default translation for " + key + " hasn't been found!");
System.err.println("The default translation for " + key + " hasn't been found!");
new Exception().printStackTrace();
}
return result;
}
/**
* Get the default value for a key.
* @param key Key to search for.
* @return the matching string in the fallback language (English); the raw key if there is no entry for it in the fallback language.
*/
public String getDefaultString(String key) {
return getStrings(key, FallbackState.FALLBACK_LANG).iterator().next();
}
/**
* Get the default value for a key.
* @param key Key to search for.
* @return the matching string in the fallback language (English); the raw key if there is no entry for it in the fallback language. Patterns are replaced by the matching values.
*/
public String getDefaultString(String key, String[] patterns, String[] values) {
assert (patterns.length == values.length);
String result = getDefaultString(key);
for (int i = 0; i < patterns.length; i++) {
result = result.replaceAll("\\$\\{" + patterns[i] + "\\}", quoteReplacement(values[i]));
}
return result;
}
/**
* Get a localized string, and replace on-the-fly some values.
* @param key Key to search for.
* @param patterns Patterns to replace, ${ and } are not included.
* @param values Replacement values.
* @return String
*/
public String getString(String key, String[] patterns, String[] values) {
assert (patterns.length == values.length);
String result = getString(key);
for (int i = 0; i < patterns.length; i++) {
result = result.replaceAll("\\$\\{" + patterns[i] + "\\}", quoteReplacement(values[i]));
}
return result;
}
/**
* Get a localized string, and replace on-the-fly a value.
* @param key Key to search for.
* @param pattern Pattern to replace, ${ and } not included.
* @param value Replacement value.
* @return String
*/
public String getString(String key, String pattern, String value) {
return getString(key, new String[]{pattern}, new String[]{value}); // FIXME code efficiently!
}
/**
* Escape null, $ and \.
* @param s String to parse
* @return String
*/
private String quoteReplacement(String s) {
if (s == null) {
return "(null)";
}
if ((s.indexOf('\\') == -1) && (s.indexOf('$') == -1)) {
return s;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '\\') {
sb.append('\\');
sb.append('\\');
} else if (c == '$') {
sb.append('\\');
sb.append('$');
} else {
sb.append(c);
}
}
return sb.toString();
}
/**
* Parse a localized string and put the result in a HTMLNode.
* @param node The result will be put in this HTMLNode.
* @param key Key to search for.
* @param patterns Patterns to replace, ${ and } are not included.
* @param values Replacement values.
* @deprecated Use {@link #addL10nSubstitution(HTMLNode, String, String[], HTMLNode[])} instead.
*/
@Deprecated
public void addL10nSubstitution(HTMLNode node, String key, String[] patterns, String[] values) {
String result = HTMLEncoder.encode(getString(key));
assert (patterns.length == values.length);
for (int i = 0; i < patterns.length; i++) {
result = result.replaceAll("\\$\\{" + patterns[i] + "\\}", quoteReplacement(values[i]));
}
node.addChild("%", result);
}
/**
* Loads an L10n string, replaces variables such as ${link} or ${bold} in it with {@link HTMLNode}s
* and adds the result to the given HTMLNode.
*
* This is *much* safer than the deprecated {@link #addL10nSubstitution(HTMLNode, String, String[], String[])}.
* Callers won't accidentally pass in unencoded strings and cause vulnerabilities.
* Callers should try to reuse parameters if possible.
* We automatically close each tag: When a pattern ${name} is matched, we search for
* ${/name}. If we find it, we make the tag enclose everything between the two; if we
* can't find it, we just add it with no children. It is not possible to create an
* HTMLNode representing a tag closure, so callers will need to change their code to
* not pass in /link or similar, and in some cases will need to change the l10n
* strings themselves to always close the tag properly, rather than using a generic
* /link for multiple links as we use in some places.
*
* <p><b>Examples</b>:
* <p>TranslationLookup.string=This is a ${link}link${/link} about ${text}.</p>
* <p>
* <code>addL10nSubstitution(html, "TranslationLookup.string", new String[] { "link", "text" },
* new HTMLNode[] { HTMLNode.link("/[email protected]"), HTMLNode.text("blah") });</code>
* </p>
* <br>
* <p>TranslationLookup.string=${bold}This${/bold} is a bold text.</p>
* <p>
* <code>addL10nSubstitution(html, "TranslationLookup.string", new String[] { "bold" },
* new HTMLNode[] { HTMLNode.STRONG });</code>
* </p>
*
* @param node The {@link HTMLNode} to which the L10n should be added after substitution was done.
* @param key The key of the L10n string which shall be used.
* @param patterns Specifies things such as ${link} which shall be replaced in the L10n string with {@link HTMLNode}s.
* @param values For each entry in the previous array parameter, this array specifies the {@link HTMLNode} with which it shall be replaced.
*/
public void addL10nSubstitution(HTMLNode node, String key, String[] patterns, HTMLNode[] values) {
List<HTMLNode> newContent = getHTMLWithSubstitutions(key, patterns, values);
node.addChildren(newContent);
}
/**
* Attempt to parse any substitution variables found in a l10n string. Intended for use in
* tests.
*/
void attemptParse(String value) throws L10nParseException {
String[] patterns = new String[0];
HTMLNode[] values = new HTMLNode[0];
performHTMLSubstitutions(value, patterns, values);
}
/**
* Look up a l10n string and replace substitution variables to generate a list of
* {@link HTMLNode}s.
*/
private List<HTMLNode> getHTMLWithSubstitutions(String key, String[] patterns, HTMLNode[] values) {
for (String value : getStrings(key)) {
// catch errors caused by bad translation strings
try {
return performHTMLSubstitutions(value, patterns, values);
} catch (L10nParseException e) {
Logger.error(this, "Error in l10n value \""+value+"\" for "+key, e);
}
}
// this should never happen, because the last item from getStrings() will be the key itself
return Collections.singletonList(new HTMLNode("#"));
}
/**
* Convert a string to a list of {@link HTMLNode}s, replacing substitution variables found in
* {@code patterns} with corresponding nodes from {@code values}.
*/
private List<HTMLNode> performHTMLSubstitutions(String value, String[] patterns,
HTMLNode[] values) throws L10nParseException {
HTMLNode tempNode = new HTMLNode("#");
addHTMLSubstitutions(tempNode, value, patterns, values);
return tempNode.getChildren();
}
/**
* Adds a string to an {@link HTMLNode}, replacing substitution variables found in
* {@code patterns} with corresponding nodes from {@code values}.
*/
private void addHTMLSubstitutions(HTMLNode node, String value,
String[] patterns, HTMLNode[] values) throws L10nParseException {
int x;
while(!value.isEmpty() && (x = value.indexOf("${")) != -1) {
String before = value.substring(0, x);
if(!before.isEmpty())
node.addChild("#", before);
value = value.substring(x);
int y = value.indexOf('}');
if(y == -1) {
throw new L10nParseException("Unclosed braces");
}
String lookup = value.substring(2, y);
value = value.substring(y+1);
if(lookup.startsWith("/")) {
throw new L10nParseException("Starts with /");
}
HTMLNode subnode = null;
for(int i=0;i<patterns.length;i++) {
if(patterns[i].equals(lookup)) {
subnode = values[i];
break;
}
}
String searchFor = "${/"+lookup+"}";
x = value.indexOf(searchFor);
if(x == -1) {
// It goes up to the end of the tag. It has no contents.
if(subnode != null) {
node.addChild(subnode.clone());
}
} else {
// It has contents. Must recurse.
String inner = value.substring(0, x);
String rest = value.substring(x + searchFor.length());
if(subnode != null) {
subnode = subnode.clone();
node.addChild(subnode);
} else {
subnode = node;
}
addHTMLSubstitutions(subnode, inner, patterns, values);
value = rest;
}
}
if(!value.isEmpty())
node.addChild("#", value);
}
public String[] getAllNamesWithPrefix(String prefix){
if(fallbackTranslation==null){
return new String[]{};
}
List<String> toReturn=new ArrayList<String>();
Iterator<String> it= fallbackTranslation.keyIterator();
while(it.hasNext()){
String key=it.next();
if(key.startsWith(prefix)){
toReturn.add(key);
}
}
return toReturn.toArray(new String[toReturn.size()]);
}
}
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.