Narrows possible locale to those from available locales

This commit is contained in:
Lukasz Lenart
2016-03-11 11:03:36 +01:00
parent 609cb0bb97
commit 37842193ab
3 changed files with 21 additions and 16 deletions
@@ -173,12 +173,12 @@ public class I18nInterceptorTest {
@Test
public void testCountryOnlyLocale() throws Exception {
params.put(I18nInterceptor.DEFAULT_PARAMETER, "DK");
params.put(I18nInterceptor.DEFAULT_PARAMETER, "NL");
interceptor.intercept(mai);
assertNull(params.get(I18nInterceptor.DEFAULT_PARAMETER)); // should have been removed
Locale denmark = new Locale("DK");
Locale denmark = new Locale("NL");
assertNotNull(session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should be stored here
assertEquals(denmark, session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should create a locale object
}
@@ -197,31 +197,31 @@ public class I18nInterceptorTest {
@Test
public void testWithVariant() throws Exception {
params.put(I18nInterceptor.DEFAULT_PARAMETER, "fr_CA_xx");
params.put(I18nInterceptor.DEFAULT_PARAMETER, "ja_JP_JP");
interceptor.intercept(mai);
assertNull(params.get(I18nInterceptor.DEFAULT_PARAMETER)); // should have been removed
Locale variant = new Locale("fr", "CA", "xx");
Locale variant = new Locale("ja", "JP", "JP");
Locale locale = (Locale) session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE);
assertNotNull(locale); // should be stored here
assertEquals(variant, locale);
assertEquals("xx", locale.getVariant());
assertEquals("JP", locale.getVariant());
}
@Test
public void testWithVariantRequestOnly() throws Exception {
params.put(I18nInterceptor.DEFAULT_REQUESTONLY_PARAMETER, "fr_CA_xx");
params.put(I18nInterceptor.DEFAULT_REQUESTONLY_PARAMETER, "ja_JP_JP");
interceptor.intercept(mai);
assertNull(params.get(I18nInterceptor.DEFAULT_PARAMETER)); // should have been removed
assertNull(session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE));
Locale variant = new Locale("fr", "CA", "xx");
Locale variant = new Locale("ja", "JP", "JP");
Locale locale = mai.getInvocationContext().getLocale();
assertNotNull(locale); // should be stored here
assertEquals(variant, locale);
assertEquals("xx", locale.getVariant());
assertEquals("JP", locale.getVariant());
}
@Test
@@ -21,6 +21,7 @@ import com.opensymphony.xwork2.util.LocalizedTextUtil;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;
@@ -226,6 +227,10 @@ public class I18nInterceptor extends AbstractInterceptor {
LOG.debug("applied request locale=#0", locale);
}
}
if (locale != null && !Arrays.asList(Locale.getAvailableLocales()).contains(locale)) {
locale = Locale.getDefault();
}
return locale;
}
@@ -82,12 +82,12 @@ public class I18nInterceptorTest extends TestCase {
}
public void testCountryOnlyLocale() throws Exception {
params.put(I18nInterceptor.DEFAULT_PARAMETER, "DK");
params.put(I18nInterceptor.DEFAULT_PARAMETER, "NL");
interceptor.intercept(mai);
assertNull(params.get(I18nInterceptor.DEFAULT_PARAMETER)); // should have been removed
Locale denmark = new Locale("DK");
Locale denmark = new Locale("NL");
assertNotNull(session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should be stored here
assertEquals(denmark, session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should create a locale object
}
@@ -104,30 +104,30 @@ public class I18nInterceptorTest extends TestCase {
}
public void testWithVariant() throws Exception {
params.put(I18nInterceptor.DEFAULT_PARAMETER, "fr_CA_xx");
params.put(I18nInterceptor.DEFAULT_PARAMETER, "ja_JP_JP");
interceptor.intercept(mai);
assertNull(params.get(I18nInterceptor.DEFAULT_PARAMETER)); // should have been removed
Locale variant = new Locale("fr", "CA", "xx");
Locale variant = new Locale("ja", "JP", "JP");
Locale locale = (Locale) session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE);
assertNotNull(locale); // should be stored here
assertEquals(variant, locale);
assertEquals("xx", locale.getVariant());
assertEquals("JP", locale.getVariant());
}
public void testWithVariantRequestOnly() throws Exception {
params.put(I18nInterceptor.DEFAULT_REQUESTONLY_PARAMETER, "fr_CA_xx");
params.put(I18nInterceptor.DEFAULT_REQUESTONLY_PARAMETER, "ja_JP_JP");
interceptor.intercept(mai);
assertNull(params.get(I18nInterceptor.DEFAULT_PARAMETER)); // should have been removed
assertNull(session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE));
Locale variant = new Locale("fr", "CA", "xx");
Locale variant = new Locale("ja", "JP", "JP");
Locale locale = mai.getInvocationContext().getLocale();
assertNotNull(locale); // should be stored here
assertEquals(variant, locale);
assertEquals("xx", locale.getVariant());
assertEquals("JP", locale.getVariant());
}
public void testRealLocaleObjectInParams() throws Exception {