I18nExtension.java
4.11 KB
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.i18n.I18n
* com.day.cq.wcm.api.Page
* javax.servlet.http.HttpServletRequest
* org.apache.commons.lang3.LocaleUtils
* org.apache.commons.lang3.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.scripting.sightly.SightlyException
* org.apache.sling.scripting.sightly.extension.RuntimeExtension
* org.apache.sling.scripting.sightly.render.RenderContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.sightly.internal.extensions;
import com.adobe.cq.sightly.internal.extensions.ExtensionUtils;
import com.day.cq.i18n.I18n;
import com.day.cq.wcm.api.Page;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import javax.script.Bindings;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.LocaleUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.scripting.sightly.SightlyException;
import org.apache.sling.scripting.sightly.extension.RuntimeExtension;
import org.apache.sling.scripting.sightly.render.RenderContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service(value={RuntimeExtension.class})
@Properties(value={@Property(name="org.apache.sling.scripting.sightly.extension.name", value={"i18nTranslation"}), @Property(name="service.ranking", intValue={1000})})
public class I18nExtension
implements RuntimeExtension {
private static final Logger LOGGER = LoggerFactory.getLogger(I18nExtension.class);
public static final String FUNCTION = "i18nTranslation";
private static final int ARG_COUNT = 2;
public /* varargs */ Object call(RenderContext renderContext, Object ... arguments) {
Bindings bindings;
Page currentPage;
SlingHttpServletRequest request = (SlingHttpServletRequest)renderContext.getBindings().get("request");
if (arguments.length != 2) {
throw new SightlyException(String.format("Extension %s requires %d arguments.", "i18nTranslation", 2));
}
String text = ExtensionUtils.objectToString(arguments[0]);
Map options = (Map)arguments[1];
String hint = ExtensionUtils.objectToString(options.get("hint"));
if ("user".equals(options.get("source"))) {
LOGGER.warn("Deprecated option detected: source='user'. This will be removed, please use locale='request.locale' instead!");
return I18n.get((HttpServletRequest)request, (String)text, (String)hint);
}
Locale locale = null;
Object localeOption = options.get("locale");
if (localeOption != null && Locale.class.isAssignableFrom(localeOption.getClass())) {
locale = (Locale)localeOption;
} else {
String localeString = ExtensionUtils.objectToString(localeOption);
if (StringUtils.isNotEmpty((CharSequence)localeString)) {
try {
locale = LocaleUtils.toLocale((String)localeString);
}
catch (IllegalArgumentException e) {
LOGGER.warn("Invalid locale detected: {}. Will attempt to use the page locale.", (Object)localeString);
}
}
}
if (locale == null && (currentPage = (Page)(bindings = renderContext.getBindings()).get("currentPage")) != null) {
locale = currentPage.getLanguage(false);
}
if (locale != null) {
ResourceBundle resourceBundle = request.getResourceBundle(locale);
return I18n.get((ResourceBundle)resourceBundle, (String)text, (String)hint);
}
return I18n.get((HttpServletRequest)request, (String)text, (String)hint);
}
}