]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2927 Availability of the English pack must not depend on default locale
authorJulien HENRY <julien.henry@sonarsource.com>
Tue, 30 Jul 2013 10:22:14 +0000 (12:22 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Tue, 30 Jul 2013 14:13:25 +0000 (16:13 +0200)
sonar-core/src/main/java/org/sonar/core/i18n/I18nManager.java
sonar-core/src/test/java/org/sonar/core/i18n/I18nManagerTest.java

index e87bac2b7053ed226cdcd681b755bdfbd6b914ba..f69c1d59b5adc35363512769b493e911d6adf9b5 100644 (file)
@@ -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<String, String> propertyToBundles;
   private Map<String, Map<Locale, String>> 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<String> 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
index cf0d2e4aee4e6019ec821524218f8199e352fdfa..efed959642310d27302413bf0ab8b87ef64d54f4 100644 (file)
@@ -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");