tr.diff
new/src/com/goldencode/p2j/util/TranslationManager.java 2023-09-15 09:42:33 +0000 | ||
---|---|---|
94 | 94 |
/** Logger **/ |
95 | 95 |
private static CentralLogger LOG = CentralLogger.get(TranslationManager.class.getSimpleName()); |
96 | 96 | |
97 |
private static ResourceBundle NULL_BUNDLE = new ResourceBundle() |
|
98 |
{ |
|
99 |
@Override |
|
100 |
protected Object handleGetObject(String key) |
|
101 |
{ |
|
102 |
return null; |
|
103 |
} |
|
104 | ||
105 |
@Override |
|
106 |
public Enumeration<String> getKeys() |
|
107 |
{ |
|
108 |
return null; |
|
109 |
} |
|
110 |
}; |
|
111 | ||
97 | 112 |
/** Context local data */ |
98 | 113 |
private static final ContextLocal<TranslationManager> instance = new ContextLocal<TranslationManager>() |
99 | 114 |
{ |
... | ... | |
134 | 149 |
/** String expansion ratio */ |
135 | 150 |
private double expansionRatio = 0; |
136 | 151 | |
152 |
private HashMap<String, ResourceBundle> fwdRuntimeBundles = new HashMap<>(); |
|
153 | ||
137 | 154 |
/** |
138 | 155 |
* Returns the context local instance. |
139 | 156 |
* |
... | ... | |
259 | 276 |
{ |
260 | 277 |
return languages.stream().<Entry<String, String>>map( |
261 | 278 |
(lang) -> new SimpleImmutableEntry<String, String>(lang, |
262 |
bundleBaseName + "_" + getIdentifier(lang)))
|
|
279 |
getBundleName(bundleBaseName, lang)))
|
|
263 | 280 |
.iterator(); |
264 | 281 |
} |
265 | 282 | |
283 |
public static String getBundleName(String bundleBaseName, String lang) |
|
284 |
{ |
|
285 |
return bundleBaseName + "_" + getIdentifier(lang); |
|
286 |
} |
|
287 | ||
266 | 288 |
/** |
267 | 289 |
* Default constructor. |
268 | 290 |
*/ |
... | ... | |
275 | 297 |
expansionRatio = percent / 100.0; |
276 | 298 |
} |
277 | 299 | |
300 |
public String tr(String msgid, Object... args) |
|
301 |
{ |
|
302 |
for (String lang : currentLanguages) |
|
303 |
{ |
|
304 |
ResourceBundle b = fwdRuntimeBundles.get(lang); |
|
305 |
if (b == null) |
|
306 |
{ |
|
307 |
String bName = getBundleName("com.goldencode.p2j.util.tr.Translations", lang); |
|
308 |
try |
|
309 |
{ |
|
310 |
b = ResourceBundle.getBundle(bName, bundleControl); |
|
311 |
} |
|
312 |
catch (Exception ex) |
|
313 |
{ |
|
314 |
b = NULL_BUNDLE; |
|
315 |
} |
|
316 | ||
317 |
fwdRuntimeBundles.put(lang, b); |
|
318 |
} |
|
319 | ||
320 |
if (b != NULL_BUNDLE) |
|
321 |
{ |
|
322 |
try |
|
323 |
{ |
|
324 |
msgid = b.getString(msgid); |
|
325 |
return String.format(msgid, args); |
|
326 |
} |
|
327 |
catch (MissingResourceException ex) |
|
328 |
{ |
|
329 |
return msgid; |
|
330 |
} |
|
331 |
} |
|
332 |
} |
|
333 | ||
334 |
return String.format(msgid, args); |
|
335 |
} |
|
336 | ||
278 | 337 |
/** |
279 | 338 |
* Initializes supplied class (aka referent class) for the purpose of i18n. The method returns a unique |
280 | 339 |
* identifier associated with the supplied referent class. Any further i18n requests must contain this |
... | ... | |
439 | 498 |
{ |
440 | 499 |
currentLanguages = parseLanguages(languages, false); |
441 | 500 |
} |
442 | ||
443 | 501 |
/** |
444 | 502 |
* Resolves a resource bundle based on the current languages set with {@link #setCurrentLanguages(String)} |
445 | 503 |
* and associates it with the supplied referent. Any further translation requests for the referent will be |
new/test/com/goldencode/p2j/util/TranslationManagerTest.java 2023-09-15 10:58:23 +0000 | ||
---|---|---|
1 |
package com.goldencode.p2j.util; |
|
2 | ||
3 |
import static org.junit.jupiter.api.Assertions.*; |
|
4 | ||
5 |
import org.junit.jupiter.api.BeforeEach; |
|
6 |
import org.junit.jupiter.api.Test; |
|
7 | ||
8 |
public class TranslationManagerTest |
|
9 |
{ |
|
10 |
private TranslationManager instance; |
|
11 | ||
12 |
// instance can be used directly, but with files with many translations it may be more manageable |
|
13 |
// to declare a tr wrapper method. |
|
14 |
private String tr(String msgid, Object... args) |
|
15 |
{ |
|
16 |
return instance.tr(msgid, args); |
|
17 |
} |
|
18 | ||
19 |
@BeforeEach |
|
20 |
public void setUp() { |
|
21 |
instance = new TranslationManager(); |
|
22 |
// You may want to set up some initial values or mocks here |
|
23 |
} |
|
24 | ||
25 |
@Test |
|
26 |
public void testTranslationWithExistingLanguage() { |
|
27 |
instance.setCurrentLanguages("cs"); |
|
28 |
String result = tr("Hello!"); |
|
29 |
assertEquals("Ahoj!", result); |
|
30 |
} |
|
31 | ||
32 |
@Test |
|
33 |
public void testTranslationWithMissingLanguage() { |
|
34 |
// Assuming there's no translation for "missing" language |
|
35 |
instance.setCurrentLanguages("missing"); |
|
36 |
String msgid = "Hello!"; |
|
37 |
String result = tr(msgid); |
|
38 |
assertEquals("Hello!", result); |
|
39 |
} |
|
40 | ||
41 |
@Test |
|
42 |
public void testTranslationWithArguments() { |
|
43 |
instance.setCurrentLanguages("cs"); |
|
44 |
String result = tr("Greetings, %s!", "John"); |
|
45 |
assertEquals("Ahoj, John!", result); |
|
46 |
} |
|
47 | ||
48 |
@Test |
|
49 |
public void testMissingMessageID() { |
|
50 |
instance.setCurrentLanguages("cs"); |
|
51 |
String missingMsgid = "missing"; |
|
52 |
String result = tr(missingMsgid); |
|
53 |
assertEquals(missingMsgid, result); |
|
54 |
} |
|
55 |
} |
new/test/com/goldencode/p2j/util/tr/Translations_cs.java 2023-09-15 10:37:04 +0000 | ||
---|---|---|
1 |
package com.goldencode.p2j.util.tr; |
|
2 | ||
3 |
/* Automatically generated by GNU msgfmt. Do not modify! */ |
|
4 |
public class Translations_cs extends java.util.ResourceBundle { |
|
5 |
private static final java.lang.String[] table; |
|
6 |
static { |
|
7 |
java.lang.String[] t = new java.lang.String[8]; |
|
8 |
t[0] = ""; |
|
9 |
t[1] = "Content-Type: text/plain; charset=UTF-8\nLanguage: cs\nPlural-Forms: nplurals=3; plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2;\n"; |
|
10 |
t[2] = "Greetings, %s!"; |
|
11 |
t[3] = "Ahoj, %s!"; |
|
12 |
t[6] = "Hello!"; |
|
13 |
t[7] = "Ahoj!"; |
|
14 |
table = t; |
|
15 |
} |
|
16 |
public java.lang.Object handleGetObject (java.lang.String msgid) throws java.util.MissingResourceException { |
|
17 |
int hash_val = msgid.hashCode() & 0x7fffffff; |
|
18 |
int idx = (hash_val % 4) << 1; |
|
19 |
java.lang.Object found = table[idx]; |
|
20 |
if (found != null && msgid.equals(found)) |
|
21 |
return table[idx + 1]; |
|
22 |
return null; |
|
23 |
} |
|
24 |
public java.util.Enumeration getKeys () { |
|
25 |
return |
|
26 |
new java.util.Enumeration() { |
|
27 |
private int idx = 0; |
|
28 |
{ while (idx < 8 && table[idx] == null) idx += 2; } |
|
29 |
public boolean hasMoreElements () { |
|
30 |
return (idx < 8); |
|
31 |
} |
|
32 |
public java.lang.Object nextElement () { |
|
33 |
java.lang.Object key = table[idx]; |
|
34 |
do idx += 2; while (idx < 8 && table[idx] == null); |
|
35 |
return key; |
|
36 |
} |
|
37 |
}; |
|
38 |
} |
|
39 |
public java.util.ResourceBundle getParent () { |
|
40 |
return parent; |
|
41 |
} |
|
42 |
} |
new/test/com/goldencode/p2j/util/tr/Translations_cs.po 2023-09-15 10:35:31 +0000 | ||
---|---|---|
1 |
msgid "" |
|
2 |
msgstr "" |
|
3 |
"Content-Type: text/plain; charset=UTF-8\n" |
|
4 |
"Language: cs\n" |
|
5 |
"Plural-Forms: nplurals=3; plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2;\n" |
|
6 | ||
7 |
msgid "Hello!" |
|
8 |
msgstr "Ahoj!" |
|
9 | ||
10 |
msgid "Greetings, %s!" |
|
11 |
msgstr "Ahoj, %s!" |