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.core.i18n;
021    
022    import com.google.common.collect.Lists;
023    import org.apache.commons.lang.StringEscapeUtils;
024    import org.sonar.api.ServerComponent;
025    
026    import java.util.*;
027    
028    public class GwtI18n implements ServerComponent {
029      public static final String GWT_BUNDLE = I18nManager.BUNDLE_PACKAGE + "gwt";
030    
031      private I18nManager manager;
032      private String[] propertyKeys;
033    
034      public GwtI18n(I18nManager manager) {
035        this.manager = manager;
036      }
037    
038      public void start() {
039        doStart(getBundle(Locale.ENGLISH));
040      }
041    
042      void doStart(ResourceBundle englishBundle) {
043        List<String> keys = Lists.newArrayList();
044        Enumeration<String> enumeration = englishBundle.getKeys();
045        while (enumeration.hasMoreElements()) {
046          String propertyKey = enumeration.nextElement();
047          keys.add(propertyKey);
048        }
049        propertyKeys = keys.toArray(new String[keys.size()]);
050      }
051    
052      String[] getPropertyKeys() {
053        return propertyKeys;
054      }
055    
056      /**
057       * Used by the JRuby on Rails application
058       */
059      public String getJsDictionnary(Locale locale) {
060        ResourceBundle bundle = getBundle(locale);
061        return getJsDictionnary(bundle);
062      }
063    
064      String getJsDictionnary(ResourceBundle bundle) {
065        StringBuilder js = new StringBuilder("var l10n = {");
066        for (int index = 0; index < propertyKeys.length; index++) {
067          String key = propertyKeys[index];
068          String value = StringEscapeUtils.escapeJavaScript(bundle.getString(key));
069          if (index > 0) {
070            js.append(",");
071          }
072          js.append("\"").append(key).append("\": \"").append(value).append("\"");
073        }
074        js.append("};");
075        return js.toString();
076      }
077    
078      ResourceBundle getBundle(Locale locale) {
079        try {
080          return ResourceBundle.getBundle(GWT_BUNDLE, locale, manager.getLanguagePackClassLoader());
081        } catch (MissingResourceException e) {
082          throw new IllegalStateException("The English bundle for GWT extensions is not deployed", e);
083        }
084      }
085    }