> bundlesByName = new HashMap<>(bundles);
bundlesByName.put(bundleName, bundlesByLocale);
-
this.bundles = bundlesByName;
}
}
@@ -380,30 +331,31 @@ private synchronized ResourceBundle cacheBundle(String bundleName, Locale locale
* Since we're really just guessing at possible bundles to use,
* we don't ever throw MissingResourceException
.
*/
- private ResourceBundle findBundleByLocale(String bundleName, Locale locale, Map bundlesByLocale) {
+ private ResourceBundle findBundleByLocale(
+ String bundleName, Locale locale, Map bundlesByLocale) {
ResourceBundle rb = null;
- if (!StringUtils.isNotEmpty(locale.getCountry()) && defaultLanguage.equals(locale.getLanguage())) {
- /*
- category.debug("Requested language '" + locale.getLanguage() +
- "' matches default: Attempting to guess bundle " +
- "using default country '" + defaultCountry + '\'');
- */
- Locale withDefaultCountry = new Locale(locale.getLanguage(), defaultCountry);
- rb = (ResourceBundle) bundlesByLocale.get(withDefaultCountry);
+ if (locale.getCountry() != null
+ && !locale.getCountry().isEmpty()
+ && Locale.getDefault().getLanguage().equals(locale.getLanguage())) {
+ Locale withDefaultCountry =
+ new Locale(locale.getLanguage(), Locale.getDefault().getCountry());
+ rb = bundlesByLocale.get(withDefaultCountry);
if (rb == null) {
rb = getBundleIgnoreException(bundleName, withDefaultCountry);
}
- } else if (!StringUtils.isNotEmpty(locale.getLanguage()) && defaultCountry.equals(locale.getCountry())) {
- Locale withDefaultLanguage = new Locale(defaultLanguage, locale.getCountry());
- rb = (ResourceBundle) bundlesByLocale.get(withDefaultLanguage);
+ } else if (locale.getLanguage() != null
+ && !locale.getLanguage().isEmpty()
+ && Locale.getDefault().getCountry().equals(locale.getCountry())) {
+ Locale withDefaultLanguage = new Locale(Locale.getDefault().getLanguage(), locale.getCountry());
+ rb = bundlesByLocale.get(withDefaultLanguage);
if (rb == null) {
rb = getBundleIgnoreException(bundleName, withDefaultLanguage);
}
}
- if (rb == null && !defaultLocale.equals(locale)) {
- rb = getBundleIgnoreException(bundleName, defaultLocale);
+ if (rb == null && !Locale.getDefault().equals(locale)) {
+ rb = getBundleIgnoreException(bundleName, Locale.getDefault());
}
return rb;
diff --git a/src/main/java/org/codehaus/plexus/i18n/I18NTokenizer.java b/src/main/java/org/codehaus/plexus/i18n/I18NTokenizer.java
index 0a7a06a..015b55a 100644
--- a/src/main/java/org/codehaus/plexus/i18n/I18NTokenizer.java
+++ b/src/main/java/org/codehaus/plexus/i18n/I18NTokenizer.java
@@ -16,12 +16,7 @@
* limitations under the License.
*/
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.Locale;
-import java.util.NoSuchElementException;
-import java.util.StringTokenizer;
+import java.util.*;
/**
* Parses the HTTP Accept-Language
header as per section
@@ -32,7 +27,7 @@
*
* TODO Move this class out of here as its purely web related.
*/
-public class I18NTokenizer implements Iterator {
+public class I18NTokenizer implements Iterator {
/**
* Separates elements of the Accept-Language
HTTP
* header.
@@ -48,12 +43,12 @@ public class I18NTokenizer implements Iterator {
* The default quality value for an AcceptLanguage
* object.
*/
- private static final Float DEFAULT_QUALITY = new Float(1.0f);
+ private static final float DEFAULT_QUALITY = 1.0f;
/**
* The parsed locales.
*/
- private ArrayList locales = new ArrayList(3);
+ private final List locales = new ArrayList<>(3);
/**
* Parses the Accept-Language
header.
@@ -76,7 +71,7 @@ public I18NTokenizer(String header) {
if ((index = q.indexOf('=')) != -1) {
try {
acceptLang.quality = Float.valueOf(q.substring(index + 1));
- } catch (NumberFormatException useDefault) {
+ } catch (NumberFormatException ignored) {
}
}
}
@@ -96,7 +91,7 @@ public I18NTokenizer(String header) {
}
// Sort by quality in descending order.
- Collections.sort(locales, Collections.reverseOrder());
+ locales.sort(Collections.reverseOrder());
}
/**
@@ -113,11 +108,11 @@ public boolean hasNext() {
* @return The next highest-rated Locale
.
* @throws NoSuchElementException No more locales.
*/
- public Object next() {
+ public Locale next() {
if (locales.isEmpty()) {
throw new NoSuchElementException();
}
- return ((AcceptLanguage) locales.remove(0)).locale;
+ return locales.remove(0).locale;
}
/**
@@ -131,7 +126,7 @@ public final void remove() {
* Struct representing an element of the HTTP
* Accept-Language
header.
*/
- private class AcceptLanguage implements Comparable {
+ private static class AcceptLanguage implements Comparable {
/**
* The language and country.
*/
@@ -143,8 +138,8 @@ private class AcceptLanguage implements Comparable {
*/
Float quality = DEFAULT_QUALITY;
- public final int compareTo(Object acceptLang) {
- return quality.compareTo(((AcceptLanguage) acceptLang).quality);
+ public final int compareTo(AcceptLanguage acceptLang) {
+ return quality.compareTo(acceptLang.quality);
}
}
}
diff --git a/src/test/java/org/codehaus/plexus/i18n/DefaultI18NTest.java b/src/test/java/org/codehaus/plexus/i18n/DefaultI18NTest.java
index 964faef..bb1b06c 100644
--- a/src/test/java/org/codehaus/plexus/i18n/DefaultI18NTest.java
+++ b/src/test/java/org/codehaus/plexus/i18n/DefaultI18NTest.java
@@ -54,9 +54,15 @@
* .
*/
+import javax.inject.Inject;
+
import java.util.Locale;
-import org.codehaus.plexus.PlexusTestCase;
+import org.codehaus.plexus.testing.PlexusTest;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Tests the API of the
@@ -66,83 +72,73 @@
* @author Daniel Rall
* @author Jason van Zyl
*/
-public class DefaultI18NTest extends PlexusTestCase {
+@PlexusTest
+public class DefaultI18NTest {
+ @Inject
private I18N i18n;
- protected void setUp() throws Exception {
- super.setUp();
-
+ @BeforeEach
+ protected void setUp() {
/* Set an unsupported locale to default to ensure we do not get unexpected matches */
Locale.setDefault(new Locale("jp"));
-
- i18n = (I18N) lookup(I18N.ROLE);
}
+ @Test
public void testLocalization() {
String s0 = i18n.getString(null, null, "key1");
-
- assertEquals("Unable to retrieve localized text for locale: default", s0, "[] value1");
+ assertEquals("[] value1", s0, "Unable to retrieve localized text for locale: default");
String s1 = i18n.getString(null, new Locale("en", "US"), "key2");
-
- assertEquals("Unable to retrieve localized text for locale: en-US", s1, "[en_US] value2");
+ assertEquals("[en_US] value2", s1, "Unable to retrieve localized text for locale: en-US");
String s2 = i18n.getString("org.codehaus.plexus.i18n.BarBundle", new Locale("ko", "KR"), "key3");
-
- assertEquals("Unable to retrieve localized text for locale: ko-KR", s2, "[ko] value3");
+ assertEquals("[ko] value3", s2, "Unable to retrieve localized text for locale: ko-KR");
String s3 = i18n.getString("org.codehaus.plexus.i18n.BarBundle", new Locale("ja"), "key1");
-
- assertEquals("Unable to fall back from non-existant locale: jp", "[] value1", s3);
+ assertEquals("[] value1", s3, "Unable to fall back from non-existant locale: jp");
String s4 = i18n.getString("org.codehaus.plexus.i18n.FooBundle", new Locale("fr"), "key3");
-
- assertEquals("Unable to retrieve localized text for locale: fr", s4, "[fr] value3");
+ assertEquals("[fr] value3", s4, "Unable to retrieve localized text for locale: fr");
String s5 = i18n.getString("org.codehaus.plexus.i18n.FooBundle", new Locale("fr", "FR"), "key3");
-
- assertEquals("Unable to retrieve localized text for locale: fr-FR", s5, "[fr] value3");
+ assertEquals("[fr] value3", s5, "Unable to retrieve localized text for locale: fr-FR");
String s6 = i18n.getString("org.codehaus.plexus.i18n.i18n", null, "key1");
-
- assertEquals("Unable to retrieve localized properties for locale: default", "[] value1", s6);
+ assertEquals("[] value1", s6, "Unable to retrieve localized properties for locale: default");
Locale old = Locale.getDefault();
Locale.setDefault(Locale.FRENCH);
try {
String s7 = i18n.getString("org.codehaus.plexus.i18n.i18n", Locale.ENGLISH, "key1");
-
- assertEquals("Not picking up new default locale: fr", "[fr] value1", s7);
+ assertEquals("[fr] value1", s7, "Not picking up new default locale: fr");
String s8 = i18n.getString("org.codehaus.plexus.i18n.i18n", Locale.ITALIAN, "key1");
-
- assertEquals("Unable to retrieve localized properties for locale: it", "[it] value1", s8);
+ assertEquals("[it] value1", s8, "Unable to retrieve localized properties for locale: it");
} finally {
Locale.setDefault(old);
}
}
+ @Test
public void testLocalizedMessagesWithFormatting() {
// Format methods
String s6 = i18n.format("org.codehaus.plexus.i18n.i18n", null, "thanks.message", "jason");
-
- assertEquals(s6, "Thanks jason!");
+ assertEquals("Thanks jason!", s6);
String s7 = i18n.format("org.codehaus.plexus.i18n.i18n", null, "thanks.message1", "jason", "van zyl");
-
- assertEquals(s7, "Thanks jason van zyl!");
+ assertEquals("Thanks jason van zyl!", s7);
String s8 = i18n.format(
"org.codehaus.plexus.i18n.i18n", null, "thanks.message2", new Object[] {"jason", "van zyl"});
- assertEquals(s8, "Thanks jason van zyl!");
+ assertEquals("Thanks jason van zyl!", s8);
}
+ @Test
public void testLocalizedMessagesWithNonStandardLocale() {
String s0 = i18n.getString("name", new Locale("xx"));
-
assertEquals("plexus", s0);
}
}
diff --git a/src/test/java/org/codehaus/plexus/i18n/I18NTokenizerTest.java b/src/test/java/org/codehaus/plexus/i18n/I18NTokenizerTest.java
index 016a4b1..3a5607d 100644
--- a/src/test/java/org/codehaus/plexus/i18n/I18NTokenizerTest.java
+++ b/src/test/java/org/codehaus/plexus/i18n/I18NTokenizerTest.java
@@ -56,9 +56,9 @@
import java.util.Locale;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Test case for the locale tokenizer.
@@ -67,29 +67,15 @@
* @author Jason van Zyl
* @version $Id: I18NTokenizerTest.java 831 2004-06-16 17:01:12Z jvanzyl $
*/
-public class I18NTokenizerTest extends TestCase {
- private static final String HEADER = "en, es;q=0.8, zh-TW;q=0.1";
-
- public I18NTokenizerTest(String name) {
- super(name);
- }
-
- public static Test suite() {
- return new TestSuite(I18NTokenizerTest.class);
- }
-
+public class I18NTokenizerTest {
+ @Test
public void testLocaleTokenizer() {
- try {
- I18NTokenizer tok = new I18NTokenizer(HEADER);
- Locale locale = (Locale) tok.next();
- assertEquals("Either wrong language or order parsing: " + locale, locale.getLanguage(), "en");
- locale = (Locale) tok.next();
- assertEquals("Either wrong language or order parsing: " + locale, locale.getLanguage(), "es");
- locale = (Locale) tok.next();
- assertEquals("Either wrong country or order parsing: " + locale, locale.getCountry(), "TW");
- } catch (Exception e) {
- e.printStackTrace();
- fail();
- }
+ I18NTokenizer tok = new I18NTokenizer("en, es;q=0.8, zh-TW;q=0.1");
+ Locale locale = tok.next();
+ assertEquals("en", locale.getLanguage(), "Either wrong language or order parsing: " + locale);
+ locale = tok.next();
+ assertEquals("es", locale.getLanguage(), "Either wrong language or order parsing: " + locale);
+ locale = tok.next();
+ assertEquals("TW", locale.getCountry(), "Either wrong country or order parsing: " + locale);
}
}