View Javadoc
1   /**
2    * This file Copyright (c) 2019 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.templating.elements.attribute;
35  
36  import info.magnolia.i18nsystem.I18nizer;
37  import info.magnolia.i18nsystem.LocaleProvider;
38  import info.magnolia.i18nsystem.TranslationService;
39  import info.magnolia.objectfactory.Components;
40  import info.magnolia.rendering.context.RenderingContext;
41  import info.magnolia.rendering.template.TemplateDefinition;
42  import info.magnolia.templating.elements.AbstractContentTemplatingElement;
43  import info.magnolia.templating.elements.AreaElement;
44  
45  import java.util.Optional;
46  
47  import javax.inject.Inject;
48  import javax.inject.Provider;
49  
50  import org.apache.commons.lang3.StringUtils;
51  
52  /**
53   * {@link ElementAttribute} for translated attributes.
54   */
55  public abstract class Translated implements ElementAttribute<AbstractContentTemplatingElement> {
56  
57      private final I18nizer i18nizer;
58      private final Provider<RenderingContext> renderingContextProvider;
59  
60      Translated(I18nizer i18nizer, Provider<RenderingContext> renderingContextProvider) {
61          this.i18nizer = i18nizer;
62          this.renderingContextProvider = renderingContextProvider;
63      }
64  
65      @Override
66      public Optional<String> getValue(AbstractContentTemplatingElement element) {
67          return Optional.ofNullable(getTranslation(element));
68      }
69  
70      abstract String getTranslation(AbstractContentTemplatingElement renderableDefinition);
71  
72      <D> D i18nize(D renderableDefinition) {
73          return i18nizer.decorate(renderableDefinition);
74      }
75  
76      String legacyTranslate(String keyOrAlreadyTranslatedValue, String i18nBasename) {
77          if (isMessageKey(keyOrAlreadyTranslatedValue)) {
78              return Components.getComponent(TranslationService.class).translate(Components.getComponent(LocaleProvider.class), i18nBasename, new String[]{keyOrAlreadyTranslatedValue});
79          } else {
80              return keyOrAlreadyTranslatedValue;
81          }
82      }
83  
84      boolean isMessageKey(String key) {
85          return !StringUtils.endsWith(key, ".") && StringUtils.contains(key, ".") && !StringUtils.contains(key, " ");
86      }
87  
88      String getI18nBasename(AbstractContentTemplatingElement element) {
89          TemplateDefinition parentDefinition = element instanceof AreaElement ? element.getTemplateDefinition() : renderingContextProvider.get().getParentAreaDefinition();
90          TemplateDefinition templateDefinition = element instanceof AreaElement ? ((AreaElement) element).getAreaDefinition() : element.getTemplateDefinition();
91          //compatibility with old i18n mechanism, remove when we get rid of i18nBasename
92          return StringUtils.isNotEmpty(templateDefinition.getI18nBasename()) ? templateDefinition.getI18nBasename() : (parentDefinition == null ? null : parentDefinition.getI18nBasename());
93      }
94  
95      /**
96       * {@link ElementAttribute} for {@link info.magnolia.rendering.template.RenderableDefinition#getTitle()}.
97       */
98      public static class Label extends Translated {
99  
100         @Inject
101         public Label(I18nizer i18nizer, Provider<RenderingContext> renderingContextProvider) {
102             super(i18nizer, renderingContextProvider);
103         }
104 
105         String getTranslation(AbstractContentTemplatingElement element) {
106             TemplateDefinition definition = element instanceof AreaElement ? ((AreaElement) element).getAreaDefinition() : element.getTemplateDefinition();
107             definition = i18nize(definition);
108             String label = null;
109             if (element instanceof AreaElement) {
110                 label = ((AreaElement) element).getLabel();
111             }
112             if (label == null) {
113                 label = definition.getTitle();
114             }
115             label = legacyTranslate(label, getI18nBasename(element));
116             if (label == null || isMessageKey(label)) {
117                 label = element instanceof AreaElement ? StringUtils.capitalize(((AreaElement) element).getName()) : definition.getName();
118             }
119             return label;
120         }
121     }
122 
123     /**
124      * {@link ElementAttribute} for {@link info.magnolia.rendering.template.RenderableDefinition#getDescription()}.
125      */
126     public static class Description extends Translated {
127 
128         @Inject
129         public Description(I18nizer i18nizer, Provider<RenderingContext> renderingContextProvider) {
130             super(i18nizer, renderingContextProvider);
131         }
132 
133         protected String getTranslation(AbstractContentTemplatingElement element) {
134             //this was already translated with i18nizer. This is here just to keep compatibility with old i18n mechanism. Remove when we drop support of old i18n
135 //            TemplateDefinition definition = element instanceof AreaElement ? ((AreaElement) element).getAreaDefinition() : element.getTemplateDefinition();
136             TemplateDefinition definition = i18nize(element.getTemplateDefinition());
137             String description = definition.getDescription();
138             final String i18nBasename = getI18nBasename(element);
139             description = this.legacyTranslate(description, i18nBasename);
140             return isMessageKey(definition.getDescription()) ? null : description;
141         }
142     }
143 }