1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
92 return StringUtils.isNotEmpty(templateDefinition.getI18nBasename()) ? templateDefinition.getI18nBasename() : (parentDefinition == null ? null : parentDefinition.getI18nBasename());
93 }
94
95
96
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
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
135
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 }