001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2012 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.server.ui; 021 022 import com.google.common.collect.Maps; 023 import org.apache.commons.lang.StringUtils; 024 import org.sonar.api.ServerComponent; 025 import org.sonar.api.i18n.I18n; 026 import org.sonar.core.i18n.GwtI18n; 027 import org.sonar.core.i18n.RuleI18nManager; 028 029 import java.util.List; 030 import java.util.Locale; 031 import java.util.Map; 032 033 /** 034 * Bridge between JRuby webapp and Java I18n component 035 */ 036 public final class JRubyI18n implements ServerComponent { 037 038 private I18n i18n; 039 private Map<String, Locale> localesByRubyKey = Maps.newHashMap(); 040 private RuleI18nManager ruleI18nManager; 041 private GwtI18n gwtI18n; 042 043 public JRubyI18n(I18n i18n, RuleI18nManager ruleI18nManager, GwtI18n gwtI18n) { 044 this.i18n = i18n; 045 this.ruleI18nManager = ruleI18nManager; 046 this.gwtI18n = gwtI18n; 047 } 048 049 Locale getLocale(String rubyKey) { 050 Locale locale = localesByRubyKey.get(rubyKey); 051 if (locale == null) { 052 locale = toLocale(rubyKey); 053 localesByRubyKey.put(rubyKey, locale); 054 } 055 return locale; 056 } 057 058 Map<String, Locale> getLocalesByRubyKey() { 059 return localesByRubyKey; 060 } 061 062 static Locale toLocale(String rubyKey) { 063 Locale locale; 064 String[] fields = StringUtils.split(rubyKey, "-"); 065 if (fields.length == 1) { 066 locale = new Locale(fields[0]); 067 } else { 068 locale = new Locale(fields[0], fields[1]); 069 } 070 return locale; 071 } 072 073 public String message(String rubyLocale, String key, String defaultValue, Object... parameters) { 074 return StringUtils.defaultString(i18n.message(getLocale(rubyLocale), key, defaultValue, parameters), key); 075 } 076 077 public String getRuleName(String rubyLocale, String repositoryKey, String key) { 078 return ruleI18nManager.getName(repositoryKey, key, toLocale(rubyLocale)); 079 } 080 081 public String getRuleDescription(String rubyLocale, String repositoryKey, String key) { 082 return ruleI18nManager.getDescription(repositoryKey, key, toLocale(rubyLocale)); 083 } 084 085 public String getRuleParamDescription(String rubyLocale, String repositoryKey, String ruleKey, String paramKey) { 086 return ruleI18nManager.getParamDescription(repositoryKey, ruleKey, paramKey, toLocale(rubyLocale)); 087 } 088 089 public List<RuleI18nManager.RuleKey> searchRuleName(String rubyLocale, String searchText) { 090 return ruleI18nManager.searchNames(searchText, toLocale(rubyLocale)); 091 } 092 093 public String getJsDictionnary(String rubyLocale) { 094 return gwtI18n.getJsDictionnary(toLocale(rubyLocale)); 095 } 096 }