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);
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() {
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();
}
public void stop() {
- i18nClassloader=null;
- propertyToBundles=null;
- fileContentCache=null;
+ i18nClassloader = null;
+ propertyToBundles = null;
+ fileContentCache = null;
}
@CheckForNull
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
*/
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;
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);
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");