View Javadoc

1   /**
2    * This file Copyright (c) 2013-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.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 org.apache.commons.lang.StringUtils;
50  
51  import com.vaadin.data.Property;
52  import com.vaadin.ui.AbstractField;
53  import com.vaadin.ui.Component;
54  import com.vaadin.ui.Field;
55  import com.vaadin.ui.HasComponents;
56  
57  /**
58   * Default implementation of {@link info.magnolia.ui.api.i18n.I18NAuthoringSupport}.
59   */
60  public class DefaultI18NAuthoringSupport implements I18NAuthoringSupport {
61  
62      private I18nContentSupport i18nContentSupport;
63  
64      private boolean enabled = true;
65  
66      private Locale authorLocale;
67  
68      public DefaultI18NAuthoringSupport() {
69          this.i18nContentSupport = Components.getComponent(I18nContentSupport.class);
70      }
71  
72      /**
73       * Returns the available locales for the given page, area or component node.<br>
74       * Please note though that this default implementation exclusively resolves locales through {@link #i18nContentSupport},
75       * i.e. as configured in /server/i18n/content/locales, regardless of the passed node.
76       *
77       * @return the list of locales if both i18nAuthoringSupport and i18nContentSupport are enabled, <code>null</code> otherwise.
78       */
79      @Override
80      public List<Locale> getAvailableLocales(Node node) {
81          if (enabled && i18nContentSupport.isEnabled()) {
82              return new ArrayList<Locale>(i18nContentSupport.getLocales());
83          }
84          return null;
85      }
86  
87      /**
88       * Returns the default locale for the given page, area or component node.
89       *
90       * TODO: create interface method in {@link info.magnolia.ui.api.i18n.I18NAuthoringSupport}
91       */
92      public Locale getDefaultLocale(Node node) {
93          if (enabled && i18nContentSupport.isEnabled()) {
94              return i18nContentSupport.getDefaultLocale();
95          }
96          return null;
97      }
98  
99      @Override
100     public void i18nize(HasComponents fieldContainer, Locale locale) {
101         Iterator<Component> it = fieldContainer.iterator();
102         boolean isFallbackLanguage = i18nContentSupport.getFallbackLocale().equals(locale);
103         if (isEnabled() && i18nContentSupport.isEnabled() && locale != null) {
104             while (it.hasNext()) {
105                 Component c = it.next();
106                 if (c instanceof Field) {
107                     Field f = (Field) c;
108                     Property p = f.getPropertyDataSource();
109 
110                     if (p instanceof TransformedProperty) {
111                         final TransformedProperty i18nBaseProperty = (TransformedProperty) p;
112 
113                         if (i18nBaseProperty.hasI18NSupport()) {
114                             final Locale formerLocale = i18nBaseProperty.getTransformer().getLocale();
115                             final String basePropertyName = i18nBaseProperty.getTransformer().getBasePropertyName();
116                             final String localizedPropertyName = isFallbackLanguage ?
117                                     basePropertyName :
118                                     constructI18NPropertyName(basePropertyName, locale);
119                             i18nBaseProperty.getTransformer().setI18NPropertyName(localizedPropertyName);
120                             i18nBaseProperty.getTransformer().setLocale(locale);
121                             i18nBaseProperty.fireI18NValueChange();
122                             String currentCaption = c.getCaption();
123 
124                             if (StringUtils.isNotBlank(currentCaption)) {
125                                 if (formerLocale != null) {
126                                     currentCaption = currentCaption.replace(String.format("(%s)", formerLocale.getLanguage()), "");
127                                 }
128                                 f.setCaption(String.format("%s (%s)", currentCaption, locale.getLanguage()));
129                             }
130 
131                             // set locale on Vaadin field
132                             if (f instanceof AbstractField) {
133                                 ((AbstractField) f).setLocale(locale);
134                             }
135                         }
136 
137                     }
138                 }
139 
140                 // try to i18nize nested fields anyway
141                 if (c instanceof HasComponents) {
142                     i18nize((HasComponents) c, locale);
143                 }
144             }
145         }
146     }
147 
148     @Override
149     public String createI18NURI(Node node, Locale locale) {
150         // we are going to change the context language, this is ugly but is safe as only the current Thread is modified
151         Locale currentLocale = i18nContentSupport.getLocale();
152         String uri = null;
153         try {
154             // this is going to set the local in the aggregation state and hence wont change the i18nSupport object itself
155             i18nContentSupport.setLocale(locale);
156             uri = LinkUtil.createAbsoluteLink(node);
157         }
158         // make sure that we always reset to the original locale
159         finally {
160             i18nContentSupport.setLocale(currentLocale);
161         }
162         return uri;
163     }
164 
165     private String constructI18NPropertyName(CharSequence basePropertyName, Locale locale) {
166         return basePropertyName + "_" + locale.toString();
167     }
168 
169     public boolean isEnabled() {
170         return enabled;
171     }
172 
173     public void setEnabled(boolean enabled) {
174         this.enabled = enabled;
175     }
176 
177     @Override
178     public Locale getAuthorLocale() {
179         return authorLocale;
180     }
181 
182     public void setAuthorLocale(Locale locale) {
183         this.authorLocale = locale;
184     }
185 
186 }