View Javadoc
1   /**
2    * This file Copyright (c) 2010-2014 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.gui.i18n;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.gui.control.Control;
38  import info.magnolia.cms.gui.control.Select;
39  import info.magnolia.cms.gui.dialog.Dialog;
40  import info.magnolia.cms.gui.dialog.DialogControlImpl;
41  import info.magnolia.cms.i18n.I18nContentSupport;
42  import info.magnolia.cms.i18n.I18nContentSupportFactory;
43  import info.magnolia.cms.util.BooleanUtil;
44  import info.magnolia.context.MgnlContext;
45  import info.magnolia.link.LinkUtil;
46  
47  import java.util.List;
48  import java.util.Locale;
49  
50  import org.apache.commons.lang.LocaleUtils;
51  import org.apache.commons.lang.StringUtils;
52  
53  
54  /**
55   * @author pbaerfuss
56   * @version $Id$
57   *
58   */
59  public class DefaultI18nAuthoringSupport implements I18nAuthoringSupport {
60  
61      private final I18nContentSupport i18nContentSupport;
62      private boolean enabled = false;
63  
64      public DefaultI18nAuthoringSupport() {
65          this.i18nContentSupport = I18nContentSupportFactory.getI18nSupport();
66      }
67  
68      @Override
69      public Control getLanguageChooser() {
70          if (isEnabled() && i18nContentSupport.isEnabled() && i18nContentSupport.getLocales().size()>1){
71              Select select = new Select();
72  
73              select.setName("locale");
74              select.setEvent("onchange", "window.location = this.value");
75  
76              Content currentPage = MgnlContext.getAggregationState().getMainContent();
77              String currentUri = createURI(currentPage, i18nContentSupport.getLocale());
78              select.setValue(currentUri);
79  
80              for (Locale locale : i18nContentSupport.getLocales()) {
81                  String uri = createURI(currentPage, locale);
82                  String label = StringUtils.capitalize(locale.getDisplayLanguage(locale));
83                  if(StringUtils.isNotEmpty(locale.getCountry())){
84                      label += " (" + StringUtils.capitalize(locale.getDisplayCountry()) + ")";
85                  }
86                  select.setOptions(label, uri);
87              }
88  
89              return select;
90          }
91          return null;
92      }
93  
94      protected String createURI(Content currentPage, Locale locale) {
95          // we are going to change the context language, this is ugly but is safe as only the current Thread is modified
96          Locale currentLocale = i18nContentSupport.getLocale();
97          String uri=null;
98          try {
99              // this is going to set the local in the aggregation state and hence wont change the i18nSupport object itself
100             i18nContentSupport.setLocale(locale);
101             uri = LinkUtil.createAbsoluteLink(currentPage);
102         }
103         // make sure that we always reset to the original locale
104         finally{
105             i18nContentSupport.setLocale(currentLocale);
106         }
107         return uri;
108     }
109 
110     @Override
111     public void i18nIze(Dialog dialog) {
112         // TODO: should this be set in the aggregation state?
113         Locale locale = LocaleUtils.toLocale(dialog.getConfigValue("locale", null));
114         boolean isFallbackLanguage = i18nContentSupport.getFallbackLocale().equals(locale);
115 
116         if (isEnabled() && i18nContentSupport.isEnabled() && locale != null) {
117             List<DialogControlImpl> tabs = dialog.getSubs();
118             for (DialogControlImpl tab : tabs) {
119                 List<DialogControlImpl> controls = tab.getSubs();
120                 for (DialogControlImpl control : controls) {
121                     boolean i18n = BooleanUtil.toBoolean(control.getConfigValue("i18n"), false);
122                     if (i18n) {
123                         if(!isFallbackLanguage){
124                             String newName = control.getName() + "_" + locale.toString();
125                             control.setName(newName);
126                         }
127                         String translatedLabel = control.getMessage(control.getLabel());
128                         control.setLabel(translatedLabel + " (" + locale.toString() + ")");
129                     }
130                 }
131             }
132         }
133     }
134 
135     @Override
136     public boolean isEnabled() {
137         return this.enabled;
138     }
139 
140     public void setEnabled(boolean enabled) {
141         this.enabled = enabled;
142     }
143 
144 }