View Javadoc

1   /**
2    * This file Copyright (c) 2013 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.ui.framework.i18n;
35  
36  import info.magnolia.cms.i18n.I18nContentSupport;
37  import info.magnolia.link.LinkUtil;
38  import info.magnolia.objectfactory.Components;
39  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
40  import info.magnolia.ui.form.field.transformer.TransformedProperty;
41  
42  import java.util.ArrayList;
43  import java.util.Iterator;
44  import java.util.List;
45  import java.util.Locale;
46  
47  import javax.jcr.Node;
48  
49  import com.vaadin.data.Property;
50  import com.vaadin.ui.Component;
51  import com.vaadin.ui.Field;
52  import com.vaadin.ui.HasComponents;
53  
54  /**
55   * Default implementation of {@link info.magnolia.ui.api.i18n.I18NAuthoringSupport}.
56   */
57  public class DefaultI18NAuthoringSupport implements I18NAuthoringSupport {
58  
59      private I18nContentSupport i18nContentSupport;
60  
61      private boolean enabled = true;
62  
63      public DefaultI18NAuthoringSupport() {
64          this.i18nContentSupport = Components.getComponent(I18nContentSupport.class);
65      }
66  
67      /**
68       * Returns the available locales for the given page, area or component node.<br>
69       * Please note though that this default implementation exclusively resolves locales through {@link i18nContentSupport},
70       * i.e. as configured in /server/i18n/content/locales, regardless of the passed node.
71       * 
72       * @return the list of locales if both i18nAuthoringSupport and i18nContentSupport are enabled, <code>null</code> otherwise.
73       */
74      @Override
75      public List<Locale> getAvailableLocales(Node node) {
76          if (enabled && i18nContentSupport.isEnabled()) {
77              return new ArrayList<Locale>(i18nContentSupport.getLocales());
78          }
79          return null;
80      }
81  
82      @Override
83      public void i18nize(HasComponents fieldContainer, Locale locale) {
84          Iterator<Component> it = fieldContainer.iterator();
85          boolean isFallbackLanguage = i18nContentSupport.getFallbackLocale().equals(locale);
86          if (isEnabled() && i18nContentSupport.isEnabled() && locale != null) {
87              while (it.hasNext()) {
88                  Component c = it.next();
89                  if (c instanceof  Field) {
90                      Field f = (Field) c;
91                      Property p = f.getPropertyDataSource();
92                      if (p instanceof TransformedProperty) {
93                          final TransformedProperty i18nBaseProperty = (TransformedProperty) p;
94                          if (!i18nBaseProperty.hasI18NSupport()) {
95                              continue;
96                          }
97                          final Locale formerLocale = i18nBaseProperty.getTransformer().getLocale();
98                          final String basePropertyName = i18nBaseProperty.getTransformer().getBasePropertyName();
99                          final String localizedPropertyName = isFallbackLanguage ?
100                                 basePropertyName :
101                                 constructI18NPropertyName(basePropertyName, locale);
102                         i18nBaseProperty.getTransformer().setI18NPropertyName(localizedPropertyName);
103                         i18nBaseProperty.getTransformer().setLocale(locale);
104                         i18nBaseProperty.fireI18NValueChange();
105                         String currentCaption = c.getCaption();
106                         if (formerLocale != null) {
107                             currentCaption = currentCaption.replace(String.format("(%s)", formerLocale.getLanguage()), "");
108                         }
109                         f.setCaption(String.format("%s (%s)", currentCaption, locale.getLanguage()));
110                     }
111                 } else if (c instanceof  HasComponents) {
112                     i18nize((HasComponents) c, locale);
113                 }
114             }
115         }
116     }
117 
118     @Override
119     public String createI18NURI(Node node, Locale locale){
120         // we are going to change the context language, this is ugly but is safe as only the current Thread is modified
121         Locale currentLocale = i18nContentSupport.getLocale();
122         String uri = null;
123         try {
124             // this is going to set the local in the aggregation state and hence wont change the i18nSupport object itself
125             i18nContentSupport.setLocale(locale);
126             uri = LinkUtil.createAbsoluteLink(node);
127         }
128         // make sure that we always reset to the original locale
129         finally{
130             i18nContentSupport.setLocale(currentLocale);
131         }
132         return uri;
133     }
134     private String constructI18NPropertyName(CharSequence basePropertyName, Locale locale) {
135         return basePropertyName + "_" + locale.toString();
136     }
137 
138     public boolean isEnabled() {
139         return enabled;
140     }
141 
142     public void setEnabled(boolean enabled) {
143         this.enabled = enabled;
144     }
145 
146 }