View Javadoc
1   /**
2    * This file Copyright (c) 2013-2018 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.context.Context;
38  import info.magnolia.context.MgnlContext;
39  import info.magnolia.link.LinkUtil;
40  import info.magnolia.objectfactory.Components;
41  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
42  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
43  
44  import java.util.ArrayList;
45  import java.util.Collections;
46  import java.util.List;
47  import java.util.Locale;
48  import java.util.Objects;
49  
50  import javax.inject.Provider;
51  import javax.jcr.Node;
52  
53  import org.slf4j.Logger;
54  import org.slf4j.LoggerFactory;
55  
56  import com.vaadin.ui.HasComponents;
57  import com.vaadin.v7.data.Item;
58  
59  /**
60   * Default implementation of {@link info.magnolia.ui.api.i18n.I18NAuthoringSupport}.
61   */
62  public class DefaultI18NAuthoringSupport implements I18NAuthoringSupport {
63  
64      private static final Logger log = LoggerFactory.getLogger(DefaultI18NAuthoringSupport.class);
65  
66      private I18nContentSupport i18nContentSupport;
67  
68      private final Provider<Context> contextProvider;
69  
70      private boolean enabled = true;
71  
72      public DefaultI18NAuthoringSupport() {
73          this(Components.getComponent(I18nContentSupport.class), MgnlContext::getInstance);
74      }
75  
76      public DefaultI18NAuthoringSupport(I18nContentSupport i18nContentSupportProvider, Provider<Context> contextProvider) {
77          this.i18nContentSupport = i18nContentSupportProvider;
78          this.contextProvider = contextProvider;
79      }
80  
81      /**
82       * Returns the available locales for the given page, area or component node.<br>
83       * Please note though that this default implementation exclusively resolves locales through {@link #i18nContentSupport},
84       * i.e. as configured in /server/i18n/content/locales, regardless of the passed node.
85       *
86       * @return the list of locales if both i18nAuthoringSupport and i18nContentSupport are enabled, <code>null</code> otherwise.
87       */
88      @Override
89      public List<Locale> getAvailableLocales(Node node) {
90          return getAvailableLocales();
91      }
92  
93      @Override
94      public Locale getDefaultLocale(Node node) {
95          return getDefaultLocale();
96      }
97  
98      @Override
99      public String deriveLocalisedPropertyName(String base, Locale locale) {
100         return String.format("%s_%s", base, locale.toString());
101     }
102 
103     /**
104      * @return the list of locales if both i18nAuthoringSupport and i18nContentSupport are enabled, <code>an empty list</code> otherwise.
105      */
106     @Override
107     public List<Locale> getAvailableLocales() {
108         if (enabled && i18nContentSupport.isEnabled()) {
109             return new ArrayList<>(i18nContentSupport.getLocales());
110         }
111         return Collections.emptyList();
112     }
113 
114     /***
115      * @return the default locale if both i18nAuthoringSupport and i18nContentSupport are enabled, defaults to {@link MgnlContext#getLocale()} otherwise.
116      */
117     @Override
118     public Locale getDefaultLocale() {
119         if (enabled && i18nContentSupport.isEnabled()) {
120             return i18nContentSupport.getDefaultLocale();
121         }
122         return contextProvider.get().getLocale();
123     }
124 
125     @Override
126     public List<Locale> getAvailableLocales(Item item) {
127         if (item instanceof JcrNodeAdapter) {
128             return getAvailableLocales(((JcrNodeAdapter)item).getJcrItem());
129         }
130         return Collections.emptyList();
131     }
132 
133     @Override
134     public Locale getDefaultLocale(Item item) {
135         if (item instanceof JcrNodeAdapter) {
136             return getDefaultLocale(((JcrNodeAdapter) item).getJcrItem());
137         }
138         return contextProvider.get().getLocale();
139     }
140 
141     @Override
142     public boolean isDefaultLocale(Locale locale, Item item) {
143         return Objects.equals(getDefaultLocale(item), locale);
144     }
145 
146     @Override
147     public void i18nize(HasComponents fieldContainer, Locale locale) {
148         log.warn("I18NAuthoringSupport.i18nize is deprecated without a replacement as of version 5.4.1, see e.g. #FormView.Listener.localeChanged(..) implementation for a workaround hint");
149     }
150 
151     @Override
152     public String createI18NURI(Node node, Locale locale) {
153         // we are going to change the context language, this is ugly but is safe as only the current Thread is modified
154         Locale currentLocale = i18nContentSupport.getLocale();
155         String uri;
156         try {
157             // this is going to set the local in the aggregation state and hence wont change the i18nSupport object itself
158             i18nContentSupport.setLocale(locale);
159             uri = LinkUtil.createAbsoluteLink(node);
160         }
161         // make sure that we always reset to the original locale
162         finally {
163             i18nContentSupport.setLocale(currentLocale);
164         }
165         return uri;
166     }
167 
168     public boolean isEnabled() {
169         return enabled;
170     }
171 
172     public void setEnabled(boolean enabled) {
173         this.enabled = enabled;
174     }
175 
176     @Override
177     @Deprecated
178     public Locale getAuthorLocale() {
179         log.warn("I18NAuthoringSupport.getAuthorLocale() is deprecated, returning null. Use SubAppContext.getAuthoringLocale() instead.");
180         return null;
181     }
182 
183     @Deprecated
184     public void setAuthorLocale(Locale locale) {
185         log.warn("I18NAuthoringSupport.setAuthorLocale(Locale) is deprecated, not doing anything. Use SubAppContext.setAuthoringLocale(Locale) instead.");
186     }
187 
188 }