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.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
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
74
75
76
77
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
89
90
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
132 if (f instanceof AbstractField) {
133 ((AbstractField) f).setLocale(locale);
134 }
135 }
136
137 }
138 }
139
140
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
151 Locale currentLocale = i18nContentSupport.getLocale();
152 String uri = null;
153 try {
154
155 i18nContentSupport.setLocale(locale);
156 uri = LinkUtil.createAbsoluteLink(node);
157 }
158
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 }