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.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
61
62
63
64
65
66
67
68
69 public class DefaultI18NAuthoringSupport<T> implements I18NAuthoringSupport<T> {
70
71 private static final Logger log = LoggerFactory.getLogger(DefaultI18NAuthoringSupport.class);
72
73 private I18nContentSupport i18nContentSupport;
74
75 private final Provider<Context> contextProvider;
76
77 private boolean enabled = true;
78
79 public DefaultI18NAuthoringSupport() {
80 this(Components.getComponent(I18nContentSupport.class), MgnlContext::getInstance);
81 }
82
83 public DefaultI18NAuthoringSupport(I18nContentSupport i18nContentSupportProvider, Provider<Context> contextProvider) {
84 this.i18nContentSupport = i18nContentSupportProvider;
85 this.contextProvider = contextProvider;
86 }
87
88 @Override
89 public String deriveLocalisedPropertyName(String base, Locale locale) {
90 return String.format("%s_%s", base, locale.toString());
91 }
92
93
94
95
96
97
98
99
100
101
102
103
104 @Override
105 public List<Locale> getAvailableLocales(T item) {
106 if (item != null && !(item instanceof Node)) {
107 return Collections.singletonList(getDefaultLocale(item));
108 }
109
110 if (enabled && i18nContentSupport.isEnabled()) {
111 return new ArrayList<>(i18nContentSupport.getLocales());
112 }
113 return Collections.emptyList();
114 }
115
116
117
118
119
120 @Override
121 public Locale getDefaultLocale(T item) {
122 if (item != null && !(item instanceof Node)) {
123 return Locale.getDefault();
124 }
125
126 if (enabled && i18nContentSupport.isEnabled()) {
127 return i18nContentSupport.getDefaultLocale();
128 }
129 return contextProvider.get().getLocale();
130 }
131
132 @Override
133 public List<Locale> getAvailableLocales(Item item) {
134 if (item instanceof JcrNodeAdapter) {
135 return getAvailableLocales((T) ((JcrNodeAdapter)item).getJcrItem());
136 }
137 return Collections.emptyList();
138 }
139
140 @Override
141 public Locale getDefaultLocale(Item item) {
142 if (item instanceof JcrNodeAdapter) {
143 return getDefaultLocale((T) ((JcrNodeAdapter) item).getJcrItem());
144 }
145 return contextProvider.get().getLocale();
146 }
147
148 @Override
149 public boolean isDefaultLocale(Locale locale, Item item) {
150 return Objects.equals(getDefaultLocale(item), locale);
151 }
152
153 @Override
154 public void i18nize(HasComponents fieldContainer, Locale locale) {
155 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");
156 }
157
158 @Override
159 public String createI18NURI(Node node, Locale locale) {
160
161 Locale currentLocale = i18nContentSupport.getLocale();
162 String uri;
163 try {
164
165 i18nContentSupport.setLocale(locale);
166 uri = LinkUtil.createAbsoluteLink(node);
167 }
168
169 finally {
170 i18nContentSupport.setLocale(currentLocale);
171 }
172 return uri;
173 }
174
175 public boolean isEnabled() {
176 return enabled;
177 }
178
179 public void setEnabled(boolean enabled) {
180 this.enabled = enabled;
181 }
182
183 @Override
184 @Deprecated
185 public Locale getAuthorLocale() {
186 log.warn("I18NAuthoringSupport.getAuthorLocale() is deprecated, returning null. Use SubAppContext.getAuthoringLocale() instead.");
187 return null;
188 }
189
190 @Deprecated
191 public void setAuthorLocale(Locale locale) {
192 log.warn("I18NAuthoringSupport.setAuthorLocale(Locale) is deprecated, not doing anything. Use SubAppContext.setAuthoringLocale(Locale) instead.");
193 }
194
195 }