View Javadoc
1   /**
2    * This file Copyright (c) 2013-2015 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.AbstractCustomMultiField;
41  import info.magnolia.ui.form.field.transformer.TransformedProperty;
42  import info.magnolia.ui.form.field.transformer.basic.BasicTransformer;
43  import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
44  
45  import java.util.ArrayList;
46  import java.util.Iterator;
47  import java.util.List;
48  import java.util.Locale;
49  
50  import javax.jcr.Node;
51  
52  import org.apache.commons.lang3.StringUtils;
53  import org.slf4j.Logger;
54  import org.slf4j.LoggerFactory;
55  
56  import com.vaadin.data.Property;
57  import com.vaadin.ui.AbstractField;
58  import com.vaadin.ui.Component;
59  import com.vaadin.ui.Field;
60  import com.vaadin.ui.HasComponents;
61  
62  /**
63   * Default implementation of {@link info.magnolia.ui.api.i18n.I18NAuthoringSupport}.
64   */
65  public class DefaultI18NAuthoringSupport implements I18NAuthoringSupport {
66  
67      private static final Logger log = LoggerFactory.getLogger(DefaultI18NAuthoringSupport.class);
68  
69      private I18nContentSupport i18nContentSupport;
70  
71      private boolean enabled = true;
72  
73      public DefaultI18NAuthoringSupport() {
74          this.i18nContentSupport = Components.getComponent(I18nContentSupport.class);
75      }
76  
77      /**
78       * Returns the available locales for the given page, area or component node.<br>
79       * Please note though that this default implementation exclusively resolves locales through {@link #i18nContentSupport},
80       * i.e. as configured in /server/i18n/content/locales, regardless of the passed node.
81       *
82       * @return the list of locales if both i18nAuthoringSupport and i18nContentSupport are enabled, <code>null</code> otherwise.
83       */
84      @Override
85      public List<Locale> getAvailableLocales(Node node) {
86          if (enabled && i18nContentSupport.isEnabled()) {
87              return new ArrayList<Locale>(i18nContentSupport.getLocales());
88          }
89          return null;
90      }
91  
92      @Override
93      public Locale getDefaultLocale(Node node) {
94          if (enabled && i18nContentSupport.isEnabled()) {
95              return i18nContentSupport.getDefaultLocale();
96          }
97          return null;
98      }
99  
100     @Override
101     public void i18nize(HasComponents fieldContainer, Locale locale) {
102         Locale defaultLocale = null;
103         Iterator<Component> fieldComponentIterator = fieldContainer.iterator();
104         if (isEnabled() && i18nContentSupport.isEnabled() && locale != null) {
105             while (fieldComponentIterator.hasNext()) {
106                 Component component = fieldComponentIterator.next();
107                 if (component instanceof Field) {
108                     Field field = (Field) component;
109                     Property propertyDataSource = field.getPropertyDataSource();
110 
111                     if (propertyDataSource instanceof TransformedProperty) {
112                         final TransformedProperty i18nBaseProperty = (TransformedProperty) propertyDataSource;
113 
114                         if (defaultLocale == null && i18nBaseProperty.getTransformer() instanceof BasicTransformer) {
115                             com.vaadin.data.Item item = ((BasicTransformer) i18nBaseProperty.getTransformer()).getRelatedFormItem();
116                             if (item instanceof JcrItemAdapter) {
117                                 javax.jcr.Item jcrItem = ((JcrItemAdapter) item).getJcrItem();
118                                 if (jcrItem.isNode()) {
119                                     defaultLocale = getDefaultLocale((Node) jcrItem);
120                                 }
121                             }
122                         }
123 
124                         boolean isDefaultLocale = locale.equals(defaultLocale);
125 
126                         if (i18nBaseProperty.hasI18NSupport()) {
127                             final Locale formerLocale = i18nBaseProperty.getTransformer().getLocale();
128                             final String basePropertyName = i18nBaseProperty.getTransformer().getBasePropertyName();
129                             final String localizedPropertyName = isDefaultLocale ?
130                                     basePropertyName :
131                                     constructI18NPropertyName(basePropertyName, locale);
132                             i18nBaseProperty.getTransformer().setI18NPropertyName(localizedPropertyName);
133                             i18nBaseProperty.getTransformer().setLocale(locale);
134                             i18nBaseProperty.fireI18NValueChange();
135                             String currentCaption = field.getCaption();
136 
137                             if (StringUtils.isNotBlank(currentCaption)) {
138                                 if (formerLocale != null) {
139                                     currentCaption = currentCaption.replace(String.format("(%s)", formerLocale.getLanguage()), "");
140                                 }
141                                 field.setCaption(String.format("%s (%s)", currentCaption, locale.getLanguage()));
142                             }
143 
144                             // set locale on Vaadin field
145                             if (field instanceof AbstractField) {
146                                 ((AbstractField) field).setLocale(locale);
147                             }
148 
149                         } else if (field instanceof AbstractCustomMultiField) {
150                             // propagate locale to multifield even when itself is not i18nized; its entries must be created in current locale.
151                             ((AbstractCustomMultiField) field).setLocale(locale);
152                         }
153 
154                     }
155                 }
156 
157                 // try to i18nize nested fields anyway
158                 if (component instanceof HasComponents) {
159                     i18nize((HasComponents) component, locale);
160                 }
161             }
162         }
163     }
164 
165     @Override
166     public String createI18NURI(Node node, Locale locale) {
167         // we are going to change the context language, this is ugly but is safe as only the current Thread is modified
168         Locale currentLocale = i18nContentSupport.getLocale();
169         String uri = null;
170         try {
171             // this is going to set the local in the aggregation state and hence wont change the i18nSupport object itself
172             i18nContentSupport.setLocale(locale);
173             uri = LinkUtil.createAbsoluteLink(node);
174         }
175         // make sure that we always reset to the original locale
176         finally {
177             i18nContentSupport.setLocale(currentLocale);
178         }
179         return uri;
180     }
181 
182     private String constructI18NPropertyName(CharSequence basePropertyName, Locale locale) {
183         return basePropertyName + "_" + locale.toString();
184     }
185 
186     public boolean isEnabled() {
187         return enabled;
188     }
189 
190     public void setEnabled(boolean enabled) {
191         this.enabled = enabled;
192     }
193 
194     @Override
195     @Deprecated
196     public Locale getAuthorLocale() {
197         log.warn("I18NAuthoringSupport.getAuthorLocale() is deprecated, returning null. Use SubAppContext.getAuthoringLocale() instead.");
198         return null;
199     }
200 
201     @Deprecated
202     public void setAuthorLocale(Locale locale) {
203         log.warn("I18NAuthoringSupport.setAuthorLocale(Locale) is deprecated, not doing anything. Use SubAppContext.setAuthoringLocale(Locale) instead.");
204     }
205 
206 }