View Javadoc
1   /**
2    * This file Copyright (c) 2013-2016 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      /**
93       * Returns the default locale for the given page, area or component node.
94       *
95       * TODO: create interface method in {@link info.magnolia.ui.api.i18n.I18NAuthoringSupport}
96       */
97      public Locale getDefaultLocale(Node node) {
98          if (enabled && i18nContentSupport.isEnabled()) {
99              return i18nContentSupport.getDefaultLocale();
100         }
101         return null;
102     }
103 
104     @Override
105     public void i18nize(HasComponents fieldContainer, Locale locale) {
106         Locale defaultLocale = null;
107         Iterator<Component> fieldComponentIterator = fieldContainer.iterator();
108         if (isEnabled() && i18nContentSupport.isEnabled() && locale != null) {
109             while (fieldComponentIterator.hasNext()) {
110                 Component component = fieldComponentIterator.next();
111                 if (component instanceof Field) {
112                     Field field = (Field) component;
113                     Property propertyDataSource = field.getPropertyDataSource();
114 
115                     if (propertyDataSource instanceof TransformedProperty) {
116                         final TransformedProperty i18nBaseProperty = (TransformedProperty) propertyDataSource;
117 
118                         if (defaultLocale == null && i18nBaseProperty.getTransformer() instanceof BasicTransformer) {
119                             com.vaadin.data.Item item = ((BasicTransformer) i18nBaseProperty.getTransformer()).getRelatedFormItem();
120                             if (item instanceof JcrItemAdapter) {
121                                 javax.jcr.Item jcrItem = ((JcrItemAdapter) item).getJcrItem();
122                                 if (jcrItem.isNode()) {
123                                     defaultLocale = getDefaultLocale((Node) jcrItem);
124                                 }
125                             }
126                         }
127 
128                         boolean isDefaultLocale = locale.equals(defaultLocale);
129 
130                         if (i18nBaseProperty.hasI18NSupport()) {
131                             final Locale formerLocale = i18nBaseProperty.getTransformer().getLocale();
132                             final String basePropertyName = i18nBaseProperty.getTransformer().getBasePropertyName();
133                             final String localizedPropertyName = isDefaultLocale ?
134                                     basePropertyName :
135                                     constructI18NPropertyName(basePropertyName, locale);
136                             i18nBaseProperty.getTransformer().setI18NPropertyName(localizedPropertyName);
137                             i18nBaseProperty.getTransformer().setLocale(locale);
138                             i18nBaseProperty.fireI18NValueChange();
139                             String currentCaption = field.getCaption();
140 
141                             if (StringUtils.isNotBlank(currentCaption)) {
142                                 if (formerLocale != null) {
143                                     currentCaption = currentCaption.replace(String.format("(%s)", formerLocale.getLanguage()), "");
144                                 }
145                                 field.setCaption(String.format("%s (%s)", currentCaption, locale.getLanguage()));
146                             }
147 
148                             // set locale on Vaadin field
149                             if (field instanceof AbstractField) {
150                                 ((AbstractField) field).setLocale(locale);
151                             }
152 
153                         } else if (field instanceof AbstractCustomMultiField) {
154                             // propagate locale to multifield even when itself is not i18nized; its entries must be created in current locale.
155                             ((AbstractCustomMultiField) field).setLocale(locale);
156                         }
157 
158                     }
159                 }
160 
161                 // try to i18nize nested fields anyway
162                 if (component instanceof HasComponents) {
163                     i18nize((HasComponents) component, locale);
164                 }
165             }
166         }
167     }
168 
169     @Override
170     public String createI18NURI(Node node, Locale locale) {
171         // we are going to change the context language, this is ugly but is safe as only the current Thread is modified
172         Locale currentLocale = i18nContentSupport.getLocale();
173         String uri = null;
174         try {
175             // this is going to set the local in the aggregation state and hence wont change the i18nSupport object itself
176             i18nContentSupport.setLocale(locale);
177             uri = LinkUtil.createAbsoluteLink(node);
178         }
179         // make sure that we always reset to the original locale
180         finally {
181             i18nContentSupport.setLocale(currentLocale);
182         }
183         return uri;
184     }
185 
186     private String constructI18NPropertyName(CharSequence basePropertyName, Locale locale) {
187         return basePropertyName + "_" + locale.toString();
188     }
189 
190     public boolean isEnabled() {
191         return enabled;
192     }
193 
194     public void setEnabled(boolean enabled) {
195         this.enabled = enabled;
196     }
197 
198     @Override
199     @Deprecated
200     public Locale getAuthorLocale() {
201         log.warn("I18NAuthoringSupport.getAuthorLocale() is deprecated, returning null. Use SubAppContext.getAuthoringLocale() instead.");
202         return null;
203     }
204 
205     @Deprecated
206     public void setAuthorLocale(Locale locale) {
207         log.warn("I18NAuthoringSupport.setAuthorLocale(Locale) is deprecated, not doing anything. Use SubAppContext.setAuthoringLocale(Locale) instead.");
208     }
209 
210 }