From e0e528211faad3fdf469c83bcbc7aecf6c25a234 Mon Sep 17 00:00:00 2001 From: Fabrice Bellingard Date: Fri, 22 Jul 2011 16:42:41 +0200 Subject: [PATCH] SONAR-2589 Fix bug with I18n rule description If a rule description is not available in the locale of the user, the english one is not used. This is fixed now. --- .../sonar/plugins/core/i18n/I18nManager.java | 22 +++++++++++++----- .../plugins/core/i18n/I18nManagerTest.java | 7 ++++++ .../src/test/resources/I18n/EnglishPlugin.jar | Bin 1113 -> 1561 bytes .../org/sonar/i18n/test.properties | 3 ++- .../org/sonar/i18n/test/anotherfakerule.html | 2 ++ 5 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 plugins/sonar-core-plugin/src/test/resources/I18n/EnglishPlugin/org/sonar/i18n/test/anotherfakerule.html diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/i18n/I18nManager.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/i18n/I18nManager.java index 1e23ab2c109..67c9b4f467f 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/i18n/I18nManager.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/i18n/I18nManager.java @@ -211,13 +211,15 @@ public final class I18nManager implements I18n, ServerExtension, BatchExtension if (bundleBaseName == null) { handleMissingBundle(ruleNameKey, defaultText, bundleBaseName); } else { - Locale localeToUse = defineLocaleToUse(locale); - String htmlFilePath = computeHtmlFilePath(bundleBaseName, ruleDescriptionKey, localeToUse); - ClassLoader classLoader = bundleClassLoader.getClassLoaderForBundle(bundleBaseName, localeToUse); - InputStream stream = classLoader.getResourceAsStream(htmlFilePath); + InputStream stream = extractRuleDescription(locale, ruleDescriptionKey, bundleBaseName); + if (stream == null && !Locale.ENGLISH.equals(locale)) { + // let's try in the ENGLISH bundles + stream = extractRuleDescription(Locale.ENGLISH, ruleDescriptionKey, bundleBaseName); + } if (stream == null) { - throw new MissingResourceException("MISSING RULE DESCRIPTION : file '" + htmlFilePath - + "' not found in any bundle. Default value is returned.", bundleBaseName, ruleDescriptionKey); + // Definitely, no HTML file found for the description of this rule... + throw new MissingResourceException("MISSING RULE DESCRIPTION : HTML file not found in any bundle. Default value is returned.", + bundleBaseName, ruleDescriptionKey); } translation = IOUtils.toString(stream, "UTF-8"); } @@ -225,6 +227,14 @@ public final class I18nManager implements I18n, ServerExtension, BatchExtension return translation; } + private InputStream extractRuleDescription(final Locale locale, final String ruleDescriptionKey, String bundleBaseName) { + Locale localeToUse = defineLocaleToUse(locale); + String htmlFilePath = computeHtmlFilePath(bundleBaseName, ruleDescriptionKey, localeToUse); + ClassLoader classLoader = bundleClassLoader.getClassLoaderForBundle(bundleBaseName, localeToUse); + InputStream stream = classLoader.getResourceAsStream(htmlFilePath); + return stream; + } + protected Locale defineLocaleToUse(final Locale locale) { Locale localeToUse = locale; if ( !registeredLocales.contains(locale)) { diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/i18n/I18nManagerTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/i18n/I18nManagerTest.java index bf66390c9e6..b3f48fd42ce 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/i18n/I18nManagerTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/i18n/I18nManagerTest.java @@ -146,6 +146,13 @@ public class I18nManagerTest { assertThat(result, is("

Fake Rule

\nThis is the description of the fake rule.")); } + @Test + public void shouldReturnEnglishRuleDescriptionFromMissingHTMLFileInFrench() throws Exception { + String result = manager.message(Locale.FRENCH, "rule.test.anotherfakerule.description", "foo"); + assertThat(result, is("

Another Fake Rule

\nThis is the description of the fake rule.")); + + } + public static class TestClassLoader extends URLClassLoader { public TestClassLoader(URL url) { diff --git a/plugins/sonar-core-plugin/src/test/resources/I18n/EnglishPlugin.jar b/plugins/sonar-core-plugin/src/test/resources/I18n/EnglishPlugin.jar index 14d66ce530db105466df99c95563913d6feafd83..f593384a2ab95bcf9ad7a712b67802c28a915e46 100755 GIT binary patch delta 636 zcmcb~F_UM)JLXB%-zI+4VqR1IZL%Drz95jcYq{RG(v^XM!3BslCdV;~*C*!Xmt>?C zr6p#k7M13t>SdJV=E!zAay1z6u)N=w6rb=ht|hLf;RoZzDJ6AkHsJ*Dub`OPrj^$m6FMkIfaOhc|LEDDbcx=oe_>j#z$5&MfSfM&5&e#y|E} z6kJI1sNhWJ`)2k!GP%4a24UH(36uR9YXyORx2yS_eIOcWS1l0B0X-|0rdLpuUyxc< zl9^f@+HDQ=qX6q~TUWmc(m9FB9dpe&3QfBWC%#$s;oColEtLlSl3{N|A||lb^eM+N zTOPd|G)vk0z`QH%yBt?eTr+9K+&fO@s@sLOsn2|Pr2k>iHHWAAX6G~SJz$vpkx6E9 z64PZ+1WdMIwgSgMDYHG67&ywTYLCtD0p5&EBFwmh59lK>SkicI@azi2MX-Jdv82(7X>vTHvJSEaj1WZ8a1f+Hbg~7DFdr8K7sF~M28IY` GkO}~_mD>IQ delta 240 zcmbQqbCYAjJLb7@?z4AIIQ}LKCGHHt*lQE2c=ugtcR` z#gkRqwGO7W6LP~pIvKm`y=~g9nY@8L@8yH~X@1K#=0{Y;Prk@3Gr5HMGRPg1y;!Wk z4rynx7u*VT!KY_>eGWj|ZGc#D@^u!~$*Wi;6)@Zel!bvMjfWT~$1{pf{=q8D$HBnC N@RyN+!Ho$d4FJU6QAnother Fake Rule +This is the description of the fake rule. \ No newline at end of file -- 2.39.5