/
LocalizationServiceImpl.java
298 lines (259 loc) · 11 KB
/
LocalizationServiceImpl.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* Copyright (C) 2010-2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.common;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.support.ResourceBundleMessageSource;
import com.evolveum.midpoint.common.configuration.api.MidpointConfiguration;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.util.LocalizableMessage;
import com.evolveum.midpoint.util.LocalizableMessageList;
import com.evolveum.midpoint.util.SingleLocalizableMessage;
import com.evolveum.midpoint.util.exception.CommonException;
import com.evolveum.midpoint.util.exception.SystemException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringTranslationArgumentType;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringTranslationType;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;
/**
* @author Viliam Repan (lazyman).
* @author Radovan Semancik
*/
public class LocalizationServiceImpl implements LocalizationService {
private static final Trace LOG = TraceManager.getTrace(LocalizationServiceImpl.class);
private final List<MessageSource> sources = new ArrayList<>();
private Locale overrideLocale = null; // for tests
public void init() {
URL url = buildMidpointHomeLocalizationFolderUrl();
ClassLoader classLoader = new URLClassLoader(new URL[] { url }, null);
sources.add(buildSource("Midpoint", classLoader));
sources.add(buildSource(SchemaConstants.BUNDLE_NAME, classLoader));
sources.add(buildSource("localization/Midpoint", null));
sources.add(buildSource(SchemaConstants.SCHEMA_LOCALIZATION_PROPERTIES_RESOURCE_BASE_PATH, null));
sources.add(buildSource(MidpointConfiguration.MIDPOINT_SYSTEM_PROPERTIES_BASE_PATH, null));
// model security messages as fallback
ResourceBundleMessageSource modelSecurity = new CachedResourceBundleMessageSource();
modelSecurity.setBasename("com.evolveum.midpoint.security");
sources.add(modelSecurity);
// spring security messages as a fallback
ResourceBundleMessageSource springSecurity = new CachedResourceBundleMessageSource();
springSecurity.setBasename("org.springframework.security.messages");
sources.add(springSecurity);
}
public Locale getOverrideLocale() {
return overrideLocale;
}
public void setOverrideLocale(Locale overrideLocale) {
this.overrideLocale = overrideLocale;
}
@Override
public String translate(String key, Object[] params, Locale locale) {
return translate(key, params, locale, null);
}
@Override
public String translate(String key, Object[] params, Locale locale, String defaultMessage) {
Object[] translated = translateParams(params, locale);
for (MessageSource source : sources) {
try {
String value = source.getMessage(key, translated, locale);
if (StringUtils.isNotEmpty(value)) {
LOG.trace("Resolved key {} to value {} using message source {}", key, value, source);
return value;
}
} catch (NoSuchMessageException ex) {
// nothing to do
}
}
return defaultMessage;
}
@Override
public String translate(LocalizableMessage msg, Locale locale, String defaultMessage) {
String rv = translate(msg, locale);
return rv != null ? rv : defaultMessage;
}
@Override
public String translate(LocalizableMessage msg, Locale locale) {
if (msg == null) {
return null;
} else if (msg instanceof SingleLocalizableMessage) {
return translate((SingleLocalizableMessage) msg, locale);
} else if (msg instanceof LocalizableMessageList) {
return translate((LocalizableMessageList) msg, locale);
} else {
throw new AssertionError("Unsupported localizable message type: " + msg);
}
}
// todo deduplicate with similar method in WebComponentUtil
public String translate(LocalizableMessageList msgList, Locale locale) {
String separator = translateIfPresent(msgList.getSeparator(), locale);
String prefix = translateIfPresent(msgList.getPrefix(), locale);
String suffix = translateIfPresent(msgList.getPostfix(), locale);
return msgList.getMessages().stream()
.map(m -> translate(m, locale))
.collect(Collectors.joining(separator, prefix, suffix));
}
private String translateIfPresent(LocalizableMessage msg, Locale locale) {
return msg != null ? translate(msg, locale) : "";
}
public String translate(SingleLocalizableMessage msg, Locale locale) {
String translated = translate(msg.getKey(), msg.getArgs(), locale);
if (StringUtils.isNotEmpty(translated)) {
return translated;
}
if (msg.getFallbackLocalizableMessage() != null) {
translated = translate(msg.getFallbackLocalizableMessage(), locale);
if (StringUtils.isNotEmpty(translated)) {
return translated;
}
}
return msg.getFallbackMessage();
}
private ResourceBundleMessageSource buildSource(String basename, ClassLoader classLoader) {
ResourceBundleMessageSource source = new CachedResourceBundleMessageSource();
source.setDefaultEncoding(StandardCharsets.UTF_8.name());
source.setFallbackToSystemLocale(false);
source.setBasename(basename);
if (classLoader == null) {
classLoader = LocalizationServiceImpl.class.getClassLoader();
}
source.setBundleClassLoader(classLoader);
return source;
}
private URL buildMidpointHomeLocalizationFolderUrl() {
String midpointHome = System.getProperty(MidpointConfiguration.MIDPOINT_HOME_PROPERTY);
File file = new File(midpointHome, "localization");
try {
return file.toURI().toURL();
} catch (IOException ex) {
throw new SystemException("Couldn't transform localization folder file to url", ex);
}
}
private Object[] translateParams(Object[] params, Locale locale) {
if (params == null) {
return null;
}
Object[] translated = new Object[params.length];
for (int i = 0; i < params.length; i++) {
Object param = params[i];
if (param instanceof Object[]) {
param = translateParams((Object[]) param, locale);
} else if (param instanceof LocalizableMessage) {
LocalizableMessage msg = (LocalizableMessage) param;
param = translate(msg, locale);
} else if (param instanceof PolyString) {
param = translate((PolyString) param, locale, true);
} else if (param instanceof PolyStringType) {
param = translate(((PolyStringType) param).toPolyString(), locale, true);
} else if (param instanceof PolyStringTranslationType) {
param = translate((PolyStringTranslationType) param, locale);
} else if (param instanceof PolyStringTranslationArgumentType) {
param = translate((PolyStringTranslationArgumentType) param, locale);
}
translated[i] = param;
}
return translated;
}
@Override
public <T extends CommonException> T translate(T e) {
if (e == null) {
return null;
}
if (e.getUserFriendlyMessage() == null) {
return e;
}
if (e.getTechnicalMessage() == null) {
e.setTechnicalMessage(translate(e.getUserFriendlyMessage(), Locale.US));
}
if (e.getLocalizedUserFriendlyMessage() == null) {
e.setLocalizedUserFriendlyMessage(translate(e.getUserFriendlyMessage(), Locale.getDefault()));
}
return e;
}
@Override
public String translate(PolyString polyString, Locale locale, boolean allowOrig) {
if (polyString == null) {
return null;
}
if (polyString.getLang() != null) {
String value = polyString.getLang().get(locale.getLanguage());
if (value != null) {
return value;
}
}
if (polyString.getTranslation() != null) {
String value = translate(polyString.getTranslation(), locale);
if (value != null) {
return value;
}
}
if (allowOrig) {
return polyString.getOrig();
} else {
return null;
}
}
private String translate(PolyStringTranslationType translation, Locale locale) {
String key = translation.getKey();
if (StringUtils.isEmpty(key)) {
return translateFromFallback(translation, locale, key);
}
String result;
List<PolyStringTranslationArgumentType> arguments = translation.getArgument();
if (arguments == null) {
result = translate(key, null, locale, null);
} else {
result = translate(key, arguments.toArray(), locale, null);
}
if (result != null) {
return result;
} else {
return translateFromFallback(translation, locale, null);
}
}
private String translateFromFallback(PolyStringTranslationType translation, Locale locale, String defaultValue) {
if (translation.getFallbackTranslation() != null) {
return translate(translation.getFallbackTranslation(), locale);
} else if (translation.getFallback() != null) {
return translation.getFallback();
} else {
return defaultValue;
}
}
private String translate(PolyStringTranslationArgumentType polyStringTranslationArgument, Locale locale) {
String value = polyStringTranslationArgument.getValue();
if (value != null) {
return value;
}
PolyStringTranslationType translation = polyStringTranslationArgument.getTranslation();
if (translation != null) {
return translate(translation, locale);
}
return null;
}
@NotNull
@Override
public Locale getDefaultLocale() {
if (overrideLocale == null) {
return Locale.getDefault();
} else {
return overrideLocale;
}
}
}