I18nExtension.java 4.11 KB
/*
 * 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);
    }
}