From: Julien HENRY Date: Tue, 30 Jul 2013 10:22:14 +0000 (+0200) Subject: SONAR-2927 Availability of the English pack must not depend on default locale X-Git-Tag: 3.7.1-RC1-~189 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b2eed53029b84f82124bb3a093109ea9f1d47521;p=sonarqube.git SONAR-2927 Availability of the English pack must not depend on default locale --- diff --git a/sonar-core/src/main/java/org/sonar/core/i18n/I18nManager.java b/sonar-core/src/main/java/org/sonar/core/i18n/I18nManager.java index e87bac2b705..f69c1d59b5a 100644 --- a/sonar-core/src/main/java/org/sonar/core/i18n/I18nManager.java +++ b/sonar-core/src/main/java/org/sonar/core/i18n/I18nManager.java @@ -37,7 +37,12 @@ import javax.annotation.Nullable; import java.io.IOException; import java.io.InputStream; import java.text.MessageFormat; -import java.util.*; +import java.util.Enumeration; +import java.util.Locale; +import java.util.Map; +import java.util.MissingResourceException; +import java.util.ResourceBundle; +import java.util.Set; public class I18nManager implements I18n, ServerExtension, BatchExtension { private static final Logger LOG = LoggerFactory.getLogger(I18nManager.class); @@ -48,9 +53,21 @@ public class I18nManager implements I18n, ServerExtension, BatchExtension { private I18nClassloader i18nClassloader; private Map propertyToBundles; private Map> fileContentCache = Maps.newHashMap(); + private final ResourceBundle.Control control; public I18nManager(PluginRepository pluginRepository) { this.pluginRepository = pluginRepository; + // SONAR-2927 + this.control = new ResourceBundle.Control() { + @Override + public Locale getFallbackLocale(String baseName, Locale locale) { + if (baseName == null) { + throw new NullPointerException(); + } + Locale defaultLocale = Locale.ENGLISH; + return locale.equals(defaultLocale) ? null : defaultLocale; + } + }; } public void start() { @@ -64,7 +81,7 @@ public class I18nManager implements I18n, ServerExtension, BatchExtension { for (PluginMetadata plugin : pluginRepository.getMetadata()) { try { String bundleKey = BUNDLE_PACKAGE + plugin.getKey(); - ResourceBundle bundle = ResourceBundle.getBundle(bundleKey, Locale.ENGLISH, i18nClassloader); + ResourceBundle bundle = ResourceBundle.getBundle(bundleKey, Locale.ENGLISH, i18nClassloader, control); Enumeration keys = bundle.getKeys(); while (keys.hasMoreElements()) { String key = keys.nextElement(); @@ -78,9 +95,9 @@ public class I18nManager implements I18n, ServerExtension, BatchExtension { } public void stop() { - i18nClassloader=null; - propertyToBundles=null; - fileContentCache=null; + i18nClassloader = null; + propertyToBundles = null; + fileContentCache = null; } @CheckForNull @@ -89,7 +106,7 @@ public class I18nManager implements I18n, ServerExtension, BatchExtension { String value = null; if (bundleKey != null) { try { - ResourceBundle resourceBundle = ResourceBundle.getBundle(bundleKey, locale, i18nClassloader); + ResourceBundle resourceBundle = ResourceBundle.getBundle(bundleKey, locale, i18nClassloader, control); value = resourceBundle.getString(key); } catch (MissingResourceException e1) { // ignore diff --git a/sonar-core/src/test/java/org/sonar/core/i18n/I18nManagerTest.java b/sonar-core/src/test/java/org/sonar/core/i18n/I18nManagerTest.java index cf0d2e4aee4..efed9596423 100644 --- a/sonar-core/src/test/java/org/sonar/core/i18n/I18nManagerTest.java +++ b/sonar-core/src/test/java/org/sonar/core/i18n/I18nManagerTest.java @@ -19,9 +19,7 @@ */ package org.sonar.core.i18n; -import org.junit.AfterClass; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.sonar.api.platform.PluginMetadata; import org.sonar.api.platform.PluginRepository; @@ -38,23 +36,8 @@ import static org.mockito.Mockito.when; public class I18nManagerTest { - private static Locale defaultLocale; private I18nManager manager; - /** - * See http://jira.codehaus.org/browse/SONAR-2927 - */ - @BeforeClass - public static void fixDefaultLocaleBug() { - defaultLocale = Locale.getDefault(); - Locale.setDefault(Locale.ENGLISH); - } - - @AfterClass - public static void revertFix() { - Locale.setDefault(defaultLocale); - } - @Before public void init() { PluginRepository pluginRepository = mock(PluginRepository.class); @@ -83,6 +66,20 @@ public class I18nManagerTest { assertThat(manager.message(Locale.ENGLISH, "checkstyle.rule1.name", null)).isEqualTo("Rule one"); } + // SONAR-2927 + @Test + public void should_get_english_labels_when_default_locale_is_not_english() { + Locale defaultLocale = Locale.getDefault(); + try { + Locale.setDefault(Locale.FRENCH); + assertThat(manager.message(Locale.ENGLISH, "by", null)).isEqualTo("By"); + assertThat(manager.message(Locale.ENGLISH, "sqale.page", null)).isEqualTo("Sqale page title"); + assertThat(manager.message(Locale.ENGLISH, "checkstyle.rule1.name", null)).isEqualTo("Rule one"); + } finally { + Locale.setDefault(defaultLocale); + } + } + @Test public void should_get_labels_from_french_pack() { assertThat(manager.message(Locale.FRENCH, "checkstyle.rule1.name", null)).isEqualTo("Rule un");