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);
}
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) {
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) {
@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() {