Skip to content

Commit 2923510

Browse files
committed
avoid loading default Locale when not found
1 parent cf62018 commit 2923510

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

‎api-tests/src/test/java/org/ocpsoft/rewrite/transposition/LocaleTranspositionTest.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,12 @@ public void testLocaleTranspositionWithAccent() throws Exception
7676
Assert.assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
7777
Assert.assertEquals("library page", action.getResponseContent());
7878
}
79+
80+
@Test
81+
public void testMissingLocaleTransposition() throws Exception
82+
{
83+
//FIXME a MissingResourceException is thrown
84+
HttpAction<HttpGet> action = get("/zz/library");
85+
Assert.assertEquals(500, action.getResponse().getStatusLine().getStatusCode());
86+
}
7987
}

‎api/src/main/java/org/ocpsoft/rewrite/transposition/LocaleTransposition.java‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Locale;
1919
import java.util.Map;
2020
import java.util.MissingResourceException;
21+
import java.util.PropertyResourceBundle;
2122
import java.util.ResourceBundle;
2223
import java.util.concurrent.ConcurrentHashMap;
2324

@@ -44,17 +45,19 @@
4445
*/
4546
public class LocaleTransposition implements Transposition<String>
4647
{
47-
4848
// shared thread safe map between a String(representing the language) and a ResourceBundle.
4949
private static Map<String, ResourceBundle> bundleMap = new ConcurrentHashMap<String, ResourceBundle>();
50-
50+
5151
private final String languageParam;
5252
private final String bundleName;
5353

5454
private LocaleTransposition(final String languageParam, final String bundleName)
5555
{
5656
Assert.notNull(languageParam, "Language must not be null.");
5757
Assert.notNull(bundleName, "Bundle must not be null.");
58+
59+
//TODO ensure that we can find a resource bundle with the provided name in the classpath
60+
5861
this.languageParam = languageParam;
5962
this.bundleName = bundleName;
6063
}
@@ -107,13 +110,10 @@ public String transpose(Rewrite event, EvaluationContext context, String value)
107110
Locale locale = new Locale(targetLang);
108111
try
109112
{
110-
// if no bundle can be found for the targetLang, the system default is used.
111-
ResourceBundle loadedBundle = ResourceBundle.getBundle(bundleName, locale);
112-
113-
// we only use the bundle if it's what we asked for (avoid using the default locale when not found)
114-
if(loadedBundle.getLocale().getLanguage().equals(targetLang)){
115-
bundleMap.put(targetLang, loadedBundle);
116-
}
113+
ResourceBundle loadedBundle = ResourceBundle.getBundle(bundleName, locale,
114+
ResourceBundle.Control.getNoFallbackControl(ResourceBundle.Control.FORMAT_DEFAULT));
115+
116+
bundleMap.put(targetLang, loadedBundle);
117117
}
118118
catch (MissingResourceException e)
119119
{
@@ -122,7 +122,8 @@ public String transpose(Rewrite event, EvaluationContext context, String value)
122122
}
123123
}
124124

125-
if (bundleMap.containsKey(targetLang)) {
125+
if (bundleMap.containsKey(targetLang))
126+
{
126127
try
127128
{
128129
// can we received more than one path section? e.g./search/service/

0 commit comments

Comments
 (0)