From: Fabrice Bellingard Date: Thu, 21 Jun 2012 08:25:54 +0000 (+0200) Subject: SONAR-2989 Support localization API in batch extensions X-Git-Tag: 3.2~299 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e84581345edc81645ba5cef49e17053390ecb2e0;p=sonarqube.git SONAR-2989 Support localization API in batch extensions --- diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java index 7d50521b4e9..60c05bf6d59 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java @@ -29,6 +29,8 @@ import org.sonar.batch.RemoteServerMetadata; import org.sonar.batch.ServerMetadata; import org.sonar.batch.config.BatchDatabaseSettingsLoader; import org.sonar.batch.config.BatchSettings; +import org.sonar.core.i18n.I18nManager; +import org.sonar.core.i18n.RuleI18nManager; import org.sonar.core.persistence.DaoUtils; import org.sonar.core.persistence.DatabaseVersion; import org.sonar.core.persistence.MyBatis; @@ -63,6 +65,8 @@ public class BootstrapModule extends Module { addCoreSingleton(ArtifactDownloader.class);// registered here because used by BootstrapClassLoader addCoreSingleton(JdbcDriverHolder.class); addCoreSingleton(EmailSettings.class); + addCoreSingleton(I18nManager.class); + addCoreSingleton(RuleI18nManager.class); URLClassLoader bootstrapClassLoader = getComponentByType(JdbcDriverHolder.class).getClassLoader(); // set as the current context classloader for hibernate, else it does not find the JDBC driver. 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 b17c26baff1..e582685d6ea 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 @@ -24,6 +24,7 @@ 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.platform.PluginMetadata; @@ -33,9 +34,15 @@ import org.sonar.api.utils.SonarException; import java.io.IOException; import java.io.InputStream; import java.text.MessageFormat; -import java.util.*; - -public class I18nManager implements I18n, ServerExtension { +import java.util.Collections; +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); public static final String ENGLISH_PACK_PLUGIN_KEY = "l10nen"; @@ -165,7 +172,6 @@ public class I18nManager implements I18n, ServerExtension { return null; } - ClassLoader getClassLoaderForProperty(String propertyKey) { String bundleKey = propertyToBundles.get(propertyKey); return (bundleKey != null ? bundleToClassloaders.get(bundleKey) : null); diff --git a/sonar-core/src/main/java/org/sonar/core/i18n/RuleI18nManager.java b/sonar-core/src/main/java/org/sonar/core/i18n/RuleI18nManager.java index 902514faa17..bf396aee978 100644 --- a/sonar-core/src/main/java/org/sonar/core/i18n/RuleI18nManager.java +++ b/sonar-core/src/main/java/org/sonar/core/i18n/RuleI18nManager.java @@ -21,12 +21,14 @@ package org.sonar.core.i18n; import com.google.common.collect.Lists; import org.apache.commons.lang.StringUtils; -import org.sonar.api.ServerComponent; +import org.sonar.api.BatchExtension; +import org.sonar.api.ServerExtension; +import org.sonar.api.i18n.RuleI18n; import java.util.List; import java.util.Locale; -public class RuleI18nManager implements ServerComponent { +public class RuleI18nManager implements RuleI18n, ServerExtension, BatchExtension { private static final String NAME_SUFFIX = ".name"; private static final String RULE_PREFIX = "rule."; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/i18n/I18n.java b/sonar-plugin-api/src/main/java/org/sonar/api/i18n/I18n.java index 86d048db74a..96e3b6e9100 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/i18n/I18n.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/i18n/I18n.java @@ -19,14 +19,17 @@ */ package org.sonar.api.i18n; +import org.sonar.api.BatchComponent; import org.sonar.api.ServerComponent; import java.util.Locale; /** + * Main component that provides translation facilities. + * * @since 2.10 */ -public interface I18n extends ServerComponent { +public interface I18n extends ServerComponent, BatchComponent { /** * Searches the message of the key for the locale in the list of available bundles. @@ -36,10 +39,10 @@ public interface I18n extends ServerComponent { * If additional parameters are given (in the objects list), the result is used as a message pattern * to use in a MessageFormat object along with the given parameters. * - * @param locale the locale to translate into - * @param key the key of the pattern to translate + * @param locale the locale to translate into + * @param key the key of the pattern to translate * @param defaultValue the default pattern returned when the key is not found in any bundle - * @param parameters the parameters used to format the message from the translated pattern. + * @param parameters the parameters used to format the message from the translated pattern. * @return the message formatted with the translated pattern and the given parameters */ String message(final Locale locale, final String key, final String defaultValue, final Object... parameters); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/i18n/RuleI18n.java b/sonar-plugin-api/src/main/java/org/sonar/api/i18n/RuleI18n.java new file mode 100644 index 00000000000..a2a083a787f --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/i18n/RuleI18n.java @@ -0,0 +1,75 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.i18n; + +import org.sonar.api.BatchComponent; +import org.sonar.api.ServerComponent; + +import java.util.Locale; + +/** + * {@link I18n}-companion component that provides translation facilities for rule names, descriptions and parameter names. + * + * @since 3.2 + */ +public interface RuleI18n extends ServerComponent, BatchComponent { + + /** + * Returns the localized name of the rule identified by its repository key and rule key. + *
+ * If the name is not found in the given locale, then the default name is returned (the English one). + * As a rule must have a name (this is a constraint in Sonar), this method never returns null. + * + * @param repositoryKey the repository key + * @param ruleKey the rule key + * @param locale the locale to translate into + * @return the translated name of the rule, or the default English one if the given locale is not supported + */ + String getName(String repositoryKey, String ruleKey, Locale locale); + + /** + * Returns the localized description of the rule identified by its repository key and rule key. + *
+ * If the description is not found in the given locale, then the default description is returned (the English one). + * As a rule must have a description (this is a constraint in Sonar), this method never returns null. + * + * @param repositoryKey the repository key + * @param ruleKey the rule key + * @param locale the locale to translate into + * @return the translated description of the rule, or the default English one if the given locale is not supported + */ + String getDescription(String repositoryKey, String ruleKey, Locale locale); + + /** + * Returns the localized name of the rule parameter identified by the rules's key and repository key, and by the parameter key. + *
+ * If the name is not found in the given locale, then the English translation is searched and return if found. Otherwise, + * this method returns null (= if no translation can be found). + * + * @param repositoryKey the repository key + * @param ruleKey the rule key + * @param paramKey the parameter key + * @param locale the locale to translate into + * @return the translated name of the rule parameter, or the default English one if the given locale is not supported, or null if + * no translation can be found. + */ + String getParamDescription(String repositoryKey, String ruleKey, String paramKey, Locale locale); + +}