diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2011-06-13 10:40:52 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2011-06-13 10:40:52 +0200 |
commit | aa92267c3ae906a14383c26ef2610ae358807115 (patch) | |
tree | 88e51ad2c7ad68b0faf55998dfa2e330741ee7e0 /sonar-server | |
parent | c257dc801bcb060cb9a5c2f8c516607721bc57e4 (diff) | |
download | sonarqube-aa92267c3ae906a14383c26ef2610ae358807115.tar.gz sonarqube-aa92267c3ae906a14383c26ef2610ae358807115.zip |
SONAR-75 Apply first version of contribution by www.serli.com
Diffstat (limited to 'sonar-server')
13 files changed, 214 insertions, 21 deletions
diff --git a/sonar-server/pom.xml b/sonar-server/pom.xml index a89e43411ea..8b65aa8e98c 100644 --- a/sonar-server/pom.xml +++ b/sonar-server/pom.xml @@ -458,6 +458,12 @@ <version>${project.version}</version> <scope>provided</scope> </dependency> + <dependency> + <groupId>org.codehaus.sonar.plugins</groupId> + <artifactId>sonar-i18n-plugin</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> </dependencies> </profile> diff --git a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java index 511d71a7f8e..3f3ee2f382c 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java @@ -58,6 +58,7 @@ import org.sonar.server.rules.*; import org.sonar.server.startup.*; import org.sonar.server.ui.AuthenticatorFactory; import org.sonar.server.ui.CodeColorizers; +import org.sonar.server.ui.JRubyI18n; import org.sonar.server.ui.Views; /** @@ -178,6 +179,7 @@ public final class Platform { servicesContainer.as(Characteristics.CACHE).addComponent(DefaultMetricFinder.class); servicesContainer.as(Characteristics.CACHE).addComponent(ProfilesConsole.class); servicesContainer.as(Characteristics.CACHE).addComponent(RulesConsole.class); + servicesContainer.as(Characteristics.CACHE).addComponent(JRubyI18n.class); servicesContainer.start(); } diff --git a/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java b/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java index 2992bba8585..7e1e99c67cc 100644 --- a/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java +++ b/sonar-server/src/main/java/org/sonar/server/ui/JRubyFacade.java @@ -22,7 +22,6 @@ package org.sonar.server.ui; import org.apache.commons.configuration.Configuration; import org.picocontainer.PicoContainer; import org.slf4j.LoggerFactory; -import org.sonar.api.Plugins; import org.sonar.api.Property; import org.sonar.api.platform.PluginMetadata; import org.sonar.api.platform.PluginRepository; @@ -58,6 +57,7 @@ import java.util.Set; public final class JRubyFacade { private static final JRubyFacade SINGLETON = new JRubyFacade(); + private JRubyI18n i18n; public static JRubyFacade getInstance() { return SINGLETON; @@ -130,9 +130,6 @@ public final class JRubyFacade { } - - - public List<ViewProxy<Widget>> getWidgets(String resourceScope, String resourceQualifier, String resourceLanguage) { return getContainer().getComponent(Views.class).getWidgets(resourceScope, resourceQualifier, resourceLanguage); } @@ -244,7 +241,7 @@ public final class JRubyFacade { } public void ruleSeverityChanged(int parentProfileId, int activeRuleId, int oldSeverityId, int newSeverityId, String userName) { - getProfilesManager().ruleSeverityChanged(parentProfileId, activeRuleId, RulePriority.values()[oldSeverityId], + getProfilesManager().ruleSeverityChanged(parentProfileId, activeRuleId, RulePriority.values()[oldSeverityId], RulePriority.values()[newSeverityId], userName); } @@ -317,6 +314,13 @@ public final class JRubyFacade { return component; } + public String getI18nMessage(String rubyLocale, String key, String defaultValue, Object... parameters) { + if (i18n == null) { + i18n = getContainer().getComponent(JRubyI18n.class); + } + return i18n.message(rubyLocale, key, defaultValue, parameters); + } + public PicoContainer getContainer() { return Platform.getInstance().getContainer(); } diff --git a/sonar-server/src/main/java/org/sonar/server/ui/JRubyI18n.java b/sonar-server/src/main/java/org/sonar/server/ui/JRubyI18n.java new file mode 100644 index 00000000000..72a1d0e39a0 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/ui/JRubyI18n.java @@ -0,0 +1,69 @@ +/* + * 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.server.ui; + +import com.google.common.collect.Maps; +import org.apache.commons.lang.StringUtils; +import org.sonar.api.ServerComponent; +import org.sonar.api.i18n.I18n; + +import java.util.Locale; +import java.util.Map; + +/** + * Bridge between JRuby webapp and Java I18n component + */ +public final class JRubyI18n implements ServerComponent { + + private I18n i18n; + private Map<String,Locale> localesByRubyKey = Maps.newHashMap(); + + public JRubyI18n(I18n i18n) { + this.i18n = i18n; + } + + Locale getLocale(String rubyKey) { + Locale locale = localesByRubyKey.get(rubyKey); + if (locale == null) { + locale = toLocale(rubyKey); + localesByRubyKey.put(rubyKey, locale); + } + return locale; + } + + Map<String, Locale> getLocalesByRubyKey() { + return localesByRubyKey; + } + + static Locale toLocale(String rubyKey) { + Locale locale; + String[] fields = StringUtils.split(rubyKey, "-"); + if (fields.length==1) { + locale = new Locale(fields[0]); + } else { + locale = new Locale(fields[0], fields[1]); + } + return locale; + } + + public String message(String rubyLocale, String key, String defaultValue, Object... parameters) { + return i18n.message(getLocale(rubyLocale), key, defaultValue, parameters); + } +} diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/server_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/server_controller.rb index 00d93e56597..fe5697b7f76 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/server_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/server_controller.rb @@ -48,7 +48,7 @@ class Api::ServerController < Api::ApiController end def system - @server=Server.new(java_facade) + @server=Server.new json=[ {:system_info => server_properties_to_json(@server.system_info)}, {:system_statistics => server_properties_to_json(@server.system_statistics)}, diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/application_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/application_controller.rb index cc87fe0dc86..af52d5bf243 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/application_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/application_controller.rb @@ -29,7 +29,7 @@ class ApplicationController < ActionController::Base end def java_facade - @java_facade ||= Java::OrgSonarServerUi::JRubyFacade.new + Java::OrgSonarServerUi::JRubyFacade.getInstance() end def available_locales diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/system_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/system_controller.rb index dcbc9724087..e836b77eed0 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/system_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/system_controller.rb @@ -24,7 +24,7 @@ class SystemController < ApplicationController before_filter :admin_required def index - @server=Server.new(java_facade) + @server=Server.new respond_to do |format| format.html diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb index 2922bcf0c92..3704dbf3e2c 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb @@ -55,6 +55,10 @@ module ApplicationHelper end end + # i18n + def message(key, default, *parameters) + Java::OrgSonarServerUi::JRubyFacade.getInstance().getI18nMessage(I18n.locale, key, default, parameters.to_java) + end # deprecated since 2.5. Use trend_icon() instead def tendency_icon(metric_or_measure, small=true, no_tendency_img=true) @@ -142,7 +146,7 @@ module ApplicationHelper end def configuration(key, default = nil) - prop_value = controller.java_facade.getContainer().getComponent(Java::OrgApacheCommonsConfiguration::Configuration.java_class).getProperty(key) + prop_value = Java::OrgSonarServerUi::JRubyFacade.getInstance().getContainer().getComponent(Java::OrgApacheCommonsConfiguration::Configuration.java_class).getProperty(key) prop_value.nil? ? default : prop_value end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/i18n_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/i18n_helper.rb new file mode 100644 index 00000000000..d6a764ffd83 --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/i18n_helper.rb @@ -0,0 +1,61 @@ +# 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 + # +module I18nHelper + + def self.java_locale() + locale_parts = I18n.locale.to_s.split('-') + return java.util.Locale.new(locale_parts[0]) if locale_parts.size == 1 + return java.util.Locale.new(locale_parts[0], locale_parts[1]) if locale_parts.size >= 2 + end + + def self.i18n + Java::OrgSonarServerUi::JRubyFacade.getInstance().getI18n() + end + + def self.translation(key, defaultText, *objects) + i18n.translation(java_locale(), key, defaultText, objects.to_java) + end + + def self.trans_widget(widget, key, default_text, *objects) + i18n.translation(java_locale(), "view." + widget.widget_key + "." + key, default_text, objects.to_java) + end + + def self.trans_page(view_id, key, default_text, *objects) + i18n.translation(java_locale(), "view." + view_id + "." + key, default_text, objects.to_java) + end + + def self.trans_tab(view_id, key, default_text, *objects) + i18n.translation(java_locale(), "view." + view_id + "." + key, default_text, objects.to_java) + end + + def self.trans_column(column_key, default_text, *objects) + i18n.translation(java_locale(), "general_columns." + column_key, default_text, objects.to_java) + end + + def self.trans_app_view(path, key, default_text, *objects) + i18n.translation(java_locale(), "app.view." + path + "." + key, default_text, objects.to_java) + end + + def self.trans_app_helper(path, key, default_text, *objects) + i18n.translation(java_locale(), "app.helper." + path + "." + key, default_text, objects.to_java) + end + + def self.trans_app_controller(path, key, default_text, *objects) + i18n.translation(java_locale(), "app.controller." + path + "." + key, default_text, objects.to_java) + end + + def self.trans_app_model(path, key, default_text, *objects) + i18n.translation(java_locale(), "app.model." + path + "." + key, default_text, objects.to_java) + end +end
\ No newline at end of file diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/metrics_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/metrics_helper.rb index 1f5fdab3e33..2be31148e2a 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/metrics_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/metrics_helper.rb @@ -19,14 +19,14 @@ # module MetricsHelper - def domains(metrics) - metrics.map {|m| m.domain}.uniq.compact.sort + def domains(metrics, translate=false) + metrics.map {|m| m.domain(translate)}.uniq.compact.sort end def options_grouped_by_domain(metrics, selected_key='') metrics_per_domain={} metrics.each do |metric| - domain=metric.domain || '' + domain=metric.domain(true) || '' metrics_per_domain[domain]||=[] metrics_per_domain[domain]<<metric end @@ -36,7 +36,7 @@ module MetricsHelper html += "<optgroup label=\"#{html_escape(domain)}\">" metrics_per_domain[domain].each do |m| selected_attr = " selected='selected'" if (m.key==selected_key || m.id==selected_key) - html += "<option value='#{html_escape(m.key)}'#{selected_attr}>#{html_escape(m.short_name)}</option>" + html += "<option value='#{html_escape(m.key)}'#{selected_attr}>#{html_escape(m.short_name(true))}</option>" end html += '</optgroup>' end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/server.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/server.rb index 31401899472..f0baab37f59 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/server.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/server.rb @@ -19,10 +19,6 @@ # class Server - def initialize(java_facade) - @java_facade=java_facade - end - def info system_info + sonar_info + system_statistics + sonar_plugins + system_properties end @@ -70,7 +66,7 @@ class Server add_property(sonar_info, 'Database Login') {sonar_property('sonar.jdbc.username')} add_property(sonar_info, 'Database Driver') {"#{jdbc_metadata.getDriverName()} #{jdbc_metadata.getDriverVersion()}"} add_property(sonar_info, 'Database Driver Class') {sonar_property('sonar.jdbc.driverClassName')} - add_property(sonar_info, 'Database Dialect (Hibernate)') {"#{@java_facade.getDialect().getId()} (#{@java_facade.getDialect().getHibernateDialectClass().getName()})"} + add_property(sonar_info, 'Database Dialect (Hibernate)') {"#{Java::OrgSonarServerUi::JRubyFacade.getInstance().getDialect().getId()} (#{Java::OrgSonarServerUi::JRubyFacade.getInstance().getDialect().getHibernateDialectClass().getName()})"} add_property(sonar_info, 'Database Validation Query') {sonar_property('sonar.jdbc.validationQuery')} add_property(sonar_info, 'Hibernate Default Schema') {sonar_property('sonar.hibernate.default_schema')} add_property(sonar_info, 'External User Authentication') {sonar_property(org.sonar.api.CoreProperties.CORE_AUTHENTICATOR_CLASS)} @@ -83,7 +79,7 @@ class Server def sonar_plugins sonar_plugins=[] - @java_facade.getPluginsMetadata().select{|plugin| !plugin.isCore()}.sort.each do |plugin| + Java::OrgSonarServerUi::JRubyFacade.getInstance().getPluginsMetadata().select{|plugin| !plugin.isCore()}.sort.each do |plugin| add_property(sonar_plugins, plugin.getName()) {plugin.getVersion()} end sonar_plugins @@ -126,7 +122,7 @@ class Server end def sonar_property(key) - @java_facade.getContainer().getComponent(Java::OrgApacheCommonsConfiguration::Configuration.java_class).getProperty(key) + Java::OrgSonarServerUi::JRubyFacade.getInstance().getContainer().getComponent(Java::OrgApacheCommonsConfiguration::Configuration.java_class).getProperty(key) end def jdbc_metadata diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/layouts/_layout.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/layouts/_layout.html.erb index 7de0d6da97e..cce189d566f 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/layouts/_layout.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/layouts/_layout.html.erb @@ -14,7 +14,7 @@ » <a href="<%= ApplicationController.root_context -%>/sessions/logout">Log out</a> </li> <% else %> - <li><a href="<%= ApplicationController.root_context -%>/sessions/new">Log in</a></li> + <li><a href="<%= ApplicationController.root_context -%>/sessions/new"><%= message('app.view.layouts.layout.login', 'Log in') -%></a></li> <% end %> <li><a href="<%= ApplicationController.root_context -%>/profiles">Configuration</a></li> </ol> diff --git a/sonar-server/src/test/java/org/sonar/server/ui/JRubyI18nTest.java b/sonar-server/src/test/java/org/sonar/server/ui/JRubyI18nTest.java new file mode 100644 index 00000000000..fe150e2b754 --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/ui/JRubyI18nTest.java @@ -0,0 +1,51 @@ +/* + * 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.server.ui; + +import org.hamcrest.core.Is; +import org.junit.Test; +import org.sonar.api.i18n.I18n; + +import java.util.Locale; + +import static org.hamcrest.core.IsNot.not; +import static org.hamcrest.core.IsNull.nullValue; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; + +public class JRubyI18nTest { + @Test + public void shouldConvertLocales() { + assertThat(JRubyI18n.toLocale("fr"), Is.is(Locale.FRENCH)); + assertThat(JRubyI18n.toLocale("fr-CH"), Is.is(new Locale("fr", "CH"))); + } + + @Test + public void shouldCacheLocales() { + JRubyI18n i18n = new JRubyI18n(mock(I18n.class)); + assertThat(i18n.getLocalesByRubyKey().size(), Is.is(0)); + + i18n.getLocale("fr"); + + assertThat(i18n.getLocalesByRubyKey().size(), Is.is(1)); + assertThat(i18n.getLocalesByRubyKey().get("fr"), not(nullValue())); + + } +} |