diff options
author | Fabrice Bellingard <fabrice.bellingard@sonarsource.com> | 2012-10-08 10:35:22 +0200 |
---|---|---|
committer | Fabrice Bellingard <fabrice.bellingard@sonarsource.com> | 2012-10-08 10:36:24 +0200 |
commit | 05b4be07a5fe257e1286663718267cddc7b02ff6 (patch) | |
tree | a75633370fc05ac3eeba544b0f8c2f1f808de8cd /sonar-core | |
parent | 05878a9361d89e086c4ec3e2b2ebd0de0761f0a2 (diff) | |
download | sonarqube-05b4be07a5fe257e1286663718267cddc7b02ff6.tar.gz sonarqube-05b4be07a5fe257e1286663718267cddc7b02ff6.zip |
SONAR-3783 Fix regression on fallback
Diffstat (limited to 'sonar-core')
4 files changed, 43 insertions, 27 deletions
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 727e7d03905..3125faea3fb 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 @@ -109,7 +109,15 @@ public class I18nManager implements I18n, ServerExtension, BatchExtension { String bundleKey = propertyToBundles.get(key); ResourceBundle resourceBundle = null; if (bundleKey != null) { - resourceBundle = getBundle(bundleKey, locale); + try { + // First, we check if the bundle exists in the language pack classloader + resourceBundle = ResourceBundle.getBundle(bundleKey, locale, languagePackClassLoader); + String message = resourceBundle.getString(key); + return MessageFormat.format(message, parameters); + } catch (MissingResourceException e1) { + // well, maybe the plugin has specified its own bundles, let's see + resourceBundle = getBundleFromCorrespondingPluginClassloader(bundleKey, locale); + } } return message(resourceBundle, key, defaultValue, parameters); } @@ -135,14 +143,7 @@ public class I18nManager implements I18n, ServerExtension, BatchExtension { filePath += "/" + filename; InputStream input = classloader.getResourceAsStream(filePath); if (input != null) { - try { - result = IOUtils.toString(input, "UTF-8"); - - } catch (IOException e) { - throw new SonarException("Fail to load file: " + filePath, e); - } finally { - IOUtils.closeQuietly(input); - } + result = readInputStream(filePath, input); } if (keepInCache) { @@ -157,27 +158,32 @@ public class I18nManager implements I18n, ServerExtension, BatchExtension { return result; } + String readInputStream(String filePath, InputStream input) { + String result = null; + try { + result = IOUtils.toString(input, "UTF-8"); + } catch (IOException e) { + throw new SonarException("Fail to load file: " + filePath, e); + } finally { + IOUtils.closeQuietly(input); + } + return result; + } + Set<String> getPropertyKeys() { return propertyToBundles.keySet(); } - ResourceBundle getBundle(String bundleKey, Locale locale) { - ResourceBundle bundle = null; - try { - // First, we check if the bundle exists in the language pack classloader - bundle = ResourceBundle.getBundle(bundleKey, locale, languagePackClassLoader); - } catch (MissingResourceException e1) { - // well, maybe the plugin has specified its own bundles, let's see - ClassLoader classloader = bundleToClassloaders.get(bundleKey); - if (classloader != null) { - try { - bundle = ResourceBundle.getBundle(bundleKey, locale, classloader); - } catch (MissingResourceException e2) { - // Well, here, there's nothing much we can do... - } + ResourceBundle getBundleFromCorrespondingPluginClassloader(String bundleKey, Locale locale) { + ClassLoader classloader = bundleToClassloaders.get(bundleKey); + if (classloader != null) { + try { + return ResourceBundle.getBundle(bundleKey, locale, classloader); + } catch (MissingResourceException e2) { + // Well, here, there's nothing much we can do... } } - return bundle; + return null; } ClassLoader getClassLoaderForProperty(String propertyKey, Locale locale) { 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 be28db66446..5a2d6e8205f 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 @@ -212,7 +212,16 @@ public class I18nManagerTest { @Test public void shouldLookInCoreClassloaderForPluginsThatDontEmbedAllLanguages() { assertThat(manager.message(Locale.ENGLISH, "forge_plugin.page", null)).isEqualTo("This is my plugin"); - assertThat(manager.message(Locale.FRENCH, "forge_plugin.page", null)).isEqualTo("C'est mon plugin"); + assertThat(manager.message(Locale.FRENCH, "forge_plugin.page", null)).isEqualTo("Mon plugin!"); + } + + // see SONAR-3783 => test that there will be no future regression on fallback for keys spread accross several classloaders + @Test + public void shouldFallbackOnOriginalPluginIfTranslationNotPresentInLanguagePack() { + // the "forge_plugin.page" has been translated in French + assertThat(manager.message(Locale.FRENCH, "forge_plugin.page", null)).isEqualTo("Mon plugin!"); + // but not the "forge_plugin.key_not_translated" key + assertThat(manager.message(Locale.FRENCH, "forge_plugin.key_not_translated", null)).isEqualTo("Key Not Translated"); } private URLClassLoader newForgeClassLoader() { diff --git a/sonar-core/src/test/resources/org/sonar/core/i18n/forgePlugin/org/sonar/l10n/forge.properties b/sonar-core/src/test/resources/org/sonar/core/i18n/forgePlugin/org/sonar/l10n/forge.properties index f2ccf6fc16f..4d907c9fbbb 100644 --- a/sonar-core/src/test/resources/org/sonar/core/i18n/forgePlugin/org/sonar/l10n/forge.properties +++ b/sonar-core/src/test/resources/org/sonar/core/i18n/forgePlugin/org/sonar/l10n/forge.properties @@ -1 +1,2 @@ -forge_plugin.page=This is my plugin
\ No newline at end of file +forge_plugin.page=This is my plugin +forge_plugin.key_not_translated=Key Not Translated
\ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/i18n/frenchPack/org/sonar/l10n/forge_fr.properties b/sonar-core/src/test/resources/org/sonar/core/i18n/frenchPack/org/sonar/l10n/forge_fr.properties index 96fb089d580..9d06fab3ec7 100644 --- a/sonar-core/src/test/resources/org/sonar/core/i18n/frenchPack/org/sonar/l10n/forge_fr.properties +++ b/sonar-core/src/test/resources/org/sonar/core/i18n/frenchPack/org/sonar/l10n/forge_fr.properties @@ -1 +1 @@ -forge_plugin.page=C'est mon plugin
\ No newline at end of file +forge_plugin.page=Mon plugin!
\ No newline at end of file |