diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-07-25 20:32:05 +0200 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-07-25 21:53:57 +0200 |
commit | 3380cba1de57f48e535de91e0c98c16030a36fa8 (patch) | |
tree | d3d82e78ae20b7508771e22eb5a475c1e2dea3e0 /plugins/sonar-core-plugin | |
parent | e7675ba5c8c7d6bb7550fba092dfb1a9abd3ec5c (diff) | |
download | sonarqube-3380cba1de57f48e535de91e0c98c16030a36fa8.tar.gz sonarqube-3380cba1de57f48e535de91e0c98c16030a36fa8.zip |
SONAR-75 define locale for violation messages
Diffstat (limited to 'plugins/sonar-core-plugin')
3 files changed, 45 insertions, 74 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java index 33714fbf456..44442fed141 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java @@ -44,7 +44,7 @@ import org.sonar.plugins.core.widgets.*; import java.util.List; -@Properties({ +@Properties({ @Property( key = CoreProperties.CORE_COVERAGE_PLUGIN_PROPERTY, defaultValue = "cobertura", @@ -118,6 +118,14 @@ import java.util.List; global = true ), @Property( + key = CoreProperties.CORE_VIOLATION_LOCALE_PROPERTY, + defaultValue = "en", + name = "Locale used for violation messages", + description = "This locale is used when generating violation messages. Settings of user browsers are " + + "ignored.", + project = true, + global = true), + @Property( key = "sonar.timemachine.period1", name = "Period 1", description = "Period used to compare measures and track new violations. Values are : <ul class='bullet'><li>Number of days before " + 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 67c9b4f467f..518a169bb4c 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 @@ -19,47 +19,36 @@ */ package org.sonar.plugins.core.i18n; -import java.io.IOException; -import java.io.InputStream; -import java.net.URI; -import java.net.URL; -import java.net.URLClassLoader; -import java.text.MessageFormat; -import java.util.Arrays; -import java.util.Enumeration; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.MissingResourceException; -import java.util.Properties; -import java.util.ResourceBundle; -import java.util.Set; - +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; import org.apache.commons.collections.EnumerationUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.sonar.api.BatchExtension; import org.sonar.api.ServerExtension; import org.sonar.api.i18n.I18n; import org.sonar.api.i18n.LanguagePack; import org.sonar.api.platform.PluginRepository; import org.sonar.api.utils.SonarException; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.URL; +import java.net.URLClassLoader; +import java.text.MessageFormat; +import java.util.*; -public final class I18nManager implements I18n, ServerExtension, BatchExtension { +public final class I18nManager implements I18n, ServerExtension { private static final Logger LOG = LoggerFactory.getLogger(I18nManager.class); public static final String packagePathToSearchIn = "org/sonar/i18n"; private PluginRepository pluginRepository; private LanguagePack[] languagePacks; - private Map<String, String> keys = Maps.newHashMap(); - private Properties unknownKeys = new Properties(); + private Map<String, String> keyToBundles = Maps.newHashMap(); private BundleClassLoader bundleClassLoader = new BundleClassLoader(); private List<Locale> registeredLocales = Lists.newArrayList(); @@ -76,20 +65,20 @@ public final class I18nManager implements I18n, ServerExtension, BatchExtension doStart(InstalledPlugin.create(pluginRepository)); } - protected void doStart(List<InstalledPlugin> installedPlugins) { + void doStart(List<InstalledPlugin> installedPlugins) { LOG.info("Loading i18n bundles"); Set<URI> alreadyLoadedResources = Sets.newHashSet(); LanguagePack englishPack = findEnglishPack(); for (InstalledPlugin plugin : installedPlugins) { - // look first in the classloader of the English I18n Plugin of the Sonar platform + // look first in the classloader of the English Language Pack searchAndStoreBundleNames(plugin.key, englishPack.getClass().getClassLoader(), alreadyLoadedResources); // then look in the classloader of the plugin searchAndStoreBundleNames(plugin.key, plugin.classloader, alreadyLoadedResources); } for (LanguagePack pack : languagePacks) { - if ( !pack.equals(englishPack)) { + if (!pack.equals(englishPack)) { addLanguagePack(pack); } } @@ -114,10 +103,9 @@ public final class I18nManager implements I18n, ServerExtension, BatchExtension for (String pluginKey : languagePack.getPluginKeys()) { String bundleBaseName = buildBundleBaseName(pluginKey); for (Locale locale : languagePack.getLocales()) { - String bundlePropertiesFile = new StringBuilder(bundleBaseName).append('_').append(locale.toString()).append(".properties") - .toString(); + String bundlePropertiesFile = new StringBuilder(bundleBaseName).append('_').append(locale.toString()).append(".properties").toString(); ClassLoader classloader = languagePack.getClass().getClassLoader(); - LOG.info("Adding locale {} for bundleName : {} from classloader : {}", new Object[] { locale, bundleBaseName, classloader }); + LOG.info("Adding locale {} for bundleName : {} from classloader : {}", new Object[]{locale, bundleBaseName, classloader}); bundleClassLoader.addResource(bundlePropertiesFile, classloader); registeredLocales.add(locale); } @@ -142,7 +130,7 @@ public final class I18nManager implements I18n, ServerExtension, BatchExtension } URL propertiesUrl = resources.get(0); - if ( !alreadyLoadedResources.contains(propertiesUrl.toURI())) { + if (!alreadyLoadedResources.contains(propertiesUrl.toURI())) { LOG.debug("Found the ResourceBundle base file : {} from classloader : {}", propertiesUrl, classloader); LOG.debug("Add bundleName : {} from classloader : {}", bundleBaseName, classloader); bundleClassLoader.addResource(bundleDefaultPropertiesFile, classloader); @@ -156,11 +144,11 @@ public final class I18nManager implements I18n, ServerExtension, BatchExtension Enumeration<String> keysToAdd = (Enumeration<String>) bundleContent.propertyNames(); while (keysToAdd.hasMoreElements()) { String key = keysToAdd.nextElement(); - if (keys.containsKey(key)) { + if (keyToBundles.containsKey(key)) { LOG.debug("DUPLICATE KEY : Key '{}' defined in bundle '{}' is already defined in bundle '{}'. It is ignored.", - new Object[] { key, bundleBaseName, keys.get(key) }); + new Object[]{key, bundleBaseName, keyToBundles.get(key)}); } else { - keys.put(key, bundleBaseName); + keyToBundles.put(key, bundleBaseName); } } } finally { @@ -184,7 +172,6 @@ public final class I18nManager implements I18n, ServerExtension, BatchExtension translatedMessage = findStandardMessage(locale, key, defaultText, objects); } } catch (MissingResourceException e) { - LOG.debug(e.getMessage()); if (translatedMessage == null) { // when no translation has been found, the key is returned return key; @@ -207,7 +194,7 @@ public final class I18nManager implements I18n, ServerExtension, BatchExtension String translation = defaultText; String ruleNameKey = ruleDescriptionKey.replace(".description", ".name"); - String bundleBaseName = keys.get(ruleNameKey); + String bundleBaseName = keyToBundles.get(ruleNameKey); if (bundleBaseName == null) { handleMissingBundle(ruleNameKey, defaultText, bundleBaseName); } else { @@ -237,7 +224,7 @@ public final class I18nManager implements I18n, ServerExtension, BatchExtension protected Locale defineLocaleToUse(final Locale locale) { Locale localeToUse = locale; - if ( !registeredLocales.contains(locale)) { + if (!registeredLocales.contains(locale)) { localeToUse = Locale.ENGLISH; } return localeToUse; @@ -262,7 +249,7 @@ public final class I18nManager implements I18n, ServerExtension, BatchExtension protected String findStandardMessage(final Locale locale, final String key, final String defaultText, final Object... objects) { String translation = defaultText; - String bundleBaseName = keys.get(key); + String bundleBaseName = keyToBundles.get(key); if (bundleBaseName == null) { handleMissingBundle(key, defaultText, bundleBaseName); } else { @@ -275,8 +262,6 @@ public final class I18nManager implements I18n, ServerExtension, BatchExtension throw new MissingResourceException("VOID KEY : Key '" + key + "' (from bundle '" + bundleBaseName + "') returns a void value.", bundleBaseName, key); } - LOG.debug("VOID KEY : Key '{}' (from bundle '{}') returns a void value. Default value '{}' is returned.", new Object[] { key, - bundleBaseName, defaultText }); } else { translation = value; } @@ -284,17 +269,13 @@ public final class I18nManager implements I18n, ServerExtension, BatchExtension if (translation == null) { throw e; } - LOG.debug("BUNDLE NOT LOADED : Failed loading bundle {} from classloader {}. Default value '{}' is returned.", new Object[] { - bundleBaseName, bundleClassLoader, defaultText }); } } if (objects.length > 0) { - LOG.debug("Translation : {}, {}, {}, {}", new String[] { locale.toString(), key, defaultText, Arrays.deepToString(objects) }); return MessageFormat.format(translation, objects); - } else { - return translation; } + return translation; } protected void handleMissingBundle(final String key, final String defaultText, String bundleBaseName) { @@ -302,23 +283,13 @@ public final class I18nManager implements I18n, ServerExtension, BatchExtension throw new MissingResourceException("UNKNOWN KEY : Key '" + key + "' not found in any bundle, and no default value provided. The key is returned.", bundleBaseName, key); } - LOG.debug("UNKNOWN KEY : Key '{}' not found in any bundle. Default value '{}' is returned.", key, defaultText); - unknownKeys.put(key, defaultText); - } - - /** - * @return the unknownKeys - */ - public Properties getUnknownKeys() { - return unknownKeys; } private static class BundleClassLoader extends URLClassLoader { - private Map<String, ClassLoader> resources = Maps.newHashMap(); public BundleClassLoader() { - super(new URL[] {}, null); + super(new URL[]{}, null); } public void addResource(String resourceName, ClassLoader classloader) { 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 b3f48fd42ce..4a09e952479 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 @@ -19,12 +19,11 @@ */ package org.sonar.plugins.core.i18n; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; +import com.google.common.collect.Lists; +import org.junit.Before; +import org.junit.Test; +import org.sonar.api.i18n.LanguagePack; +import org.sonar.api.platform.PluginRepository; import java.net.URL; import java.net.URLClassLoader; @@ -32,13 +31,9 @@ import java.util.Arrays; import java.util.List; import java.util.Locale; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.sonar.api.i18n.LanguagePack; -import org.sonar.api.platform.PluginRepository; - -import com.google.common.collect.Lists; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; public class I18nManagerTest { @@ -63,7 +58,7 @@ public class I18nManagerTest { TestClassLoader quebecPackClassLoader = new TestClassLoader(getClass().getClassLoader().getResource("I18n/QuebecPlugin.jar")); LanguagePack quebecPack = (LanguagePack) quebecPackClassLoader.loadClass(QUEBEC_PACK_CLASS_NAME).newInstance(); - manager = new I18nManager(mock(PluginRepository.class), new LanguagePack[] { frenchPack, quebecPack, englishPack }); + manager = new I18nManager(mock(PluginRepository.class), new LanguagePack[]{frenchPack, quebecPack, englishPack}); manager.doStart(plugins); } @@ -99,15 +94,12 @@ public class I18nManagerTest { public void shouldTranslateUnknownValue() { String result = manager.message(Locale.FRENCH, "unknown", "Default value for Unknown"); assertEquals("Default value for Unknown", result); - Assert.assertEquals(1, manager.getUnknownKeys().size()); - Assert.assertEquals("Default value for Unknown", manager.getUnknownKeys().getProperty("unknown")); } @Test public void shouldReturnKeyIfTranslationMissingAndNotDefaultProvided() throws Exception { String result = manager.message(Locale.ENGLISH, "unknown.key", null); assertEquals("unknown.key", result); - Assert.assertEquals(0, manager.getUnknownKeys().size()); } @Test @@ -156,7 +148,7 @@ public class I18nManagerTest { public static class TestClassLoader extends URLClassLoader { public TestClassLoader(URL url) { - super(new URL[] { url, classSource }, Thread.currentThread().getContextClassLoader()); + super(new URL[]{url, classSource}, Thread.currentThread().getContextClassLoader()); } protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { |