]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-75 refactor I18nManager
authorSimon Brandhof <simon.brandhof@gmail.com>
Thu, 28 Jul 2011 12:43:51 +0000 (14:43 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Thu, 28 Jul 2011 12:44:57 +0000 (14:44 +0200)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java
sonar-core/src/main/java/org/sonar/core/i18n/I18nManager.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/core/i18n/I18nManagerTest.java [new file with mode: 0644]

index c9ac989598ef5cf5e82d14b6689ecf71e5663f43..bf045ccbcc7813a25cbdecd17d6a2718c5a6c385 100644 (file)
@@ -21,9 +21,9 @@ package org.sonar.plugins.core;
 
 import com.google.common.collect.Lists;
 import org.sonar.api.CoreProperties;
-import org.sonar.api.SonarPlugin;
 import org.sonar.api.Properties;
 import org.sonar.api.Property;
+import org.sonar.api.SonarPlugin;
 import org.sonar.api.checks.NoSonarFilter;
 import org.sonar.api.resources.Java;
 import org.sonar.plugins.core.batch.ExcludedResourceFilter;
@@ -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,12 +118,12 @@ import java.util.List;
         global = true
     ),
     @Property(
-       key = CoreProperties.CORE_VIOLATION_LOCALE_PROPERTY,
-       defaultValue = "en",
-       name = "Locale used for violation messages",
-       description = "Locale to be used when generating violation messages. It's up to each rule engine to support this global internationalization property",
-       project = true,
-       global = true),
+        key = CoreProperties.CORE_VIOLATION_LOCALE_PROPERTY,
+        defaultValue = "en",
+        name = "Locale used for violation messages",
+        description = "Locale to be used when generating violation messages. It's up to each rule engine to support this global internationalization property",
+        project = true,
+        global = true),
     @Property(
         key = "sonar.timemachine.period1",
         name = "Period 1",
@@ -243,7 +243,7 @@ public class CorePlugin extends SonarPlugin {
 
     // i18n
     extensions.add(I18nManager.class);
-
+//    extensions.add(org.sonar.core.i18n.I18nManager.class);
     return extensions;
   }
 }
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
new file mode 100644 (file)
index 0000000..a99ba33
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 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.core.i18n;
+
+import com.google.common.collect.Maps;
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.ServerExtension;
+import org.sonar.api.i18n.I18n;
+import org.sonar.api.platform.PluginMetadata;
+import org.sonar.api.platform.PluginRepository;
+
+import java.text.MessageFormat;
+import java.util.Locale;
+import java.util.Map;
+import java.util.ResourceBundle;
+
+public class I18nManager implements I18n, ServerExtension {
+
+  private PluginRepository pluginRepository;
+  private Map<String, ClassLoader> bundleToClassloader;
+
+  public I18nManager(PluginRepository pluginRepository) {
+    this.pluginRepository = pluginRepository;
+  }
+
+  I18nManager(Map<String, ClassLoader> bundleToClassloader) {
+    this.bundleToClassloader = bundleToClassloader;
+  }
+
+  public void start() {
+    ClassLoader coreClassLoader = pluginRepository.getPlugin("i18nen").getClass().getClassLoader();
+
+    bundleToClassloader = Maps.newHashMap();
+    for (PluginMetadata metadata : pluginRepository.getMetadata()) {
+      if (!metadata.isCore() && !"i18nen".equals(metadata.getBasePlugin())) {
+        ClassLoader classLoader = pluginRepository.getPlugin(metadata.getKey()).getClass().getClassLoader();
+        bundleToClassloader.put(metadata.getKey(), classLoader);
+
+      } else if (metadata.isCore()) {
+        bundleToClassloader.put(metadata.getKey(), coreClassLoader);
+      }
+    }
+  }
+
+  public String message(Locale locale, String key, String defaultValue, Object... parameters) {
+    String bundle = keyToBundle(key);
+    ResourceBundle resourceBundle = ResourceBundle.getBundle(bundle, locale, bundleToClassloader.get(bundle));
+    String value = resourceBundle.getString(key);
+    if (value==null) {
+      value = defaultValue;
+    }
+    if (value != null && parameters.length > 0) {
+      return MessageFormat.format(value, parameters);
+    }
+    return value;
+  }
+
+  String keyToBundle(String key) {
+    String pluginKey = StringUtils.substringBefore(key, ".");
+    if (bundleToClassloader.containsKey(pluginKey)) {
+      return pluginKey;
+    }
+    return "core";
+  }
+}
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
new file mode 100644 (file)
index 0000000..af57b76
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 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.core.i18n;
+
+import com.google.common.collect.Maps;
+import org.hamcrest.core.Is;
+import org.junit.Test;
+
+import java.util.Map;
+
+import static org.junit.Assert.assertThat;
+
+public class I18nManagerTest {
+  @Test
+  public void shouldExtractBundleKey() {
+    Map<String,ClassLoader> bundleToClassLoaders = Maps.newHashMap();
+    bundleToClassLoaders.put("core", getClass().getClassLoader());
+    bundleToClassLoaders.put("checkstyle", getClass().getClassLoader());
+    bundleToClassLoaders.put("sqale", getClass().getClassLoader());
+    I18nManager i18n = new I18nManager(bundleToClassLoaders);
+
+    assertThat(i18n.keyToBundle("by"), Is.is("core"));
+    assertThat(i18n.keyToBundle("violations_drilldown.page"), Is.is("core"));
+    assertThat(i18n.keyToBundle("checkstyle.rule1.name"), Is.is("checkstyle"));
+    assertThat(i18n.keyToBundle("sqale.console.page"), Is.is("sqale"));
+  }
+}