Skip to content

Commit 995bd6b

Browse files
Copilotslachiewicz
andauthored
Fix DefaultI18N to prevent fallback to JVM default locale (#26)
--------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: slachiewicz <[email protected]>
1 parent 1efacb5 commit 995bd6b

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

src/main/java/org/codehaus/plexus/i18n/DefaultI18N.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,10 @@ private synchronized ResourceBundle cacheBundle(String bundleName, Locale locale
298298
if (rb == null) {
299299
bundlesByLocale = (bundlesByLocale == null ? new HashMap<>(3) : new HashMap<>(bundlesByLocale));
300300
try {
301-
rb = ResourceBundle.getBundle(bundleName, locale);
301+
rb = ResourceBundle.getBundle(
302+
bundleName,
303+
locale,
304+
ResourceBundle.Control.getNoFallbackControl(ResourceBundle.Control.FORMAT_DEFAULT));
302305
} catch (MissingResourceException e) {
303306
rb = findBundleByLocale(bundleName, locale, bundlesByLocale);
304307
if (rb == null) {
@@ -371,7 +374,10 @@ private ResourceBundle findBundleByLocale(
371374
*/
372375
private ResourceBundle getBundleIgnoreException(String bundleName, Locale locale) {
373376
try {
374-
return ResourceBundle.getBundle(bundleName, locale);
377+
return ResourceBundle.getBundle(
378+
bundleName,
379+
locale,
380+
ResourceBundle.Control.getNoFallbackControl(ResourceBundle.Control.FORMAT_DEFAULT));
375381
} catch (MissingResourceException ignored) {
376382
return null;
377383
}

src/test/java/org/codehaus/plexus/i18n/DefaultI18NTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public void testLocalization() {
110110
Locale.setDefault(Locale.FRENCH);
111111
try {
112112
String s7 = i18n.getString("org.codehaus.plexus.i18n.i18n", Locale.ENGLISH, "key1");
113-
assertEquals("[fr] value1", s7, "Not picking up new default locale: fr");
113+
assertEquals("[] value1", s7, "Should not fall back to default locale, use root bundle instead");
114114

115115
String s8 = i18n.getString("org.codehaus.plexus.i18n.i18n", Locale.ITALIAN, "key1");
116116
assertEquals("[it] value1", s8, "Unable to retrieve localized properties for locale: it");
@@ -141,4 +141,25 @@ public void testLocalizedMessagesWithNonStandardLocale() {
141141
String s0 = i18n.getString("name", new Locale("xx"));
142142
assertEquals("plexus", s0);
143143
}
144+
145+
@Test
146+
public void testNoFallbackToDefaultLocale() {
147+
// Save the current default locale
148+
Locale oldDefault = Locale.getDefault();
149+
150+
try {
151+
// Set default locale to German
152+
Locale.setDefault(Locale.GERMAN);
153+
154+
// Request a locale that doesn't have a bundle (Hebrew - Israel)
155+
// Expected: should get the root bundle with "[] value1"
156+
// Bug: currently gets the German bundle with "[de] value1"
157+
String result = i18n.getString("org.codehaus.plexus.i18n.i18n", new Locale("iw", "IL"), "key1");
158+
159+
assertEquals("[] value1", result, "Should get root bundle, not default locale bundle");
160+
} finally {
161+
// Restore the original default locale
162+
Locale.setDefault(oldDefault);
163+
}
164+
}
144165
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
key1 = [de] value1
2+
thanks.message = Danke {0}!
3+
thanks.message1 = Danke {0} {1}!
4+
thanks.message2 = Danke {0} {1}!

0 commit comments

Comments
 (0)