This is an automated email from the ASF dual-hosted git repository. pauls pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-sightly.git
The following commit(s) were added to refs/heads/master by this push: new fa08f60 SLING-12179: Make i18n dependency optional (#24) fa08f60 is described below commit fa08f6028c56779436c331790aa76601b9085d80 Author: Karl Pauls <karlpa...@gmail.com> AuthorDate: Tue Dec 5 11:32:12 2023 +0100 SLING-12179: Make i18n dependency optional (#24) --- bnd.bnd | 1 + .../engine/extension/I18nRuntimeExtension.java | 24 ++++++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/bnd.bnd b/bnd.bnd index 4ab1356..1b10f37 100644 --- a/bnd.bnd +++ b/bnd.bnd @@ -8,6 +8,7 @@ Provide-Capability: io.sightly;version:Version="1.0", \\ io.sightly;version:Version="1.4" Require-Capability: io.sightly.runtime; filter:="(&(version>=1.0)(!(version>=2.0)))" Import-Package: org.apache.sling.scripting.sightly.compiler.*;resolution:=optional, \\ + org.apache.sling.i18n.*;resolution:=optional, \\ org.apache.sling.scripting.sightly.java.compiler.*;resolution:=optional, \\ org.apache.sling.commons.compiler.*;resolution:=optional, \\ org.apache.sling.commons.classloader.*;resolution:=optional, \\ diff --git a/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/I18nRuntimeExtension.java b/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/I18nRuntimeExtension.java index 39a8236..2f6a6b8 100644 --- a/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/I18nRuntimeExtension.java +++ b/src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/I18nRuntimeExtension.java @@ -61,11 +61,27 @@ public class I18nRuntimeExtension implements RuntimeExtension { return get(bindings, text, locale, basename, hint); } + private volatile boolean logged; + + private Object getResourceBundleProvider(SlingScriptHelper slingScriptHelper) { + Class clazz; + try { + clazz = getClass().getClassLoader().loadClass("org.apache.sling.i18n.ResourceBundleProvider"); + } catch (Throwable t) { + if (!logged) { + LOG.warn("i18n package not available"); + logged = true; + } + return null; + } + return slingScriptHelper.getService(clazz); + } + private String get(final Bindings bindings, String text, String locale, String basename, String hint) { final SlingScriptHelper slingScriptHelper = BindingsUtils.getHelper(bindings); final SlingHttpServletRequest request = BindingsUtils.getRequest(bindings); - final ResourceBundleProvider resourceBundleProvider = slingScriptHelper.getService(ResourceBundleProvider.class); + final Object resourceBundleProvider = getResourceBundleProvider(slingScriptHelper); if (resourceBundleProvider != null) { String key = text; if (StringUtils.isNotEmpty(hint)) { @@ -99,12 +115,12 @@ public class I18nRuntimeExtension implements RuntimeExtension { return text; } - private String getTranslation(ResourceBundleProvider resourceBundleProvider, String basename, String key, Locale locale) { + private String getTranslation(Object resourceBundleProvider, String basename, String key, Locale locale) { ResourceBundle resourceBundle; if (StringUtils.isNotEmpty(basename)) { - resourceBundle = resourceBundleProvider.getResourceBundle(basename, locale); + resourceBundle = ((ResourceBundleProvider) resourceBundleProvider).getResourceBundle(basename, locale); } else { - resourceBundle = resourceBundleProvider.getResourceBundle(locale); + resourceBundle = ((ResourceBundleProvider) resourceBundleProvider).getResourceBundle(locale); } if (resourceBundle != null && resourceBundle.containsKey(key)) { return resourceBundle.getString(key);