Search in sources :

Example 1 with ResourceBundleProvider

use of org.apache.sling.i18n.ResourceBundleProvider in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ButtonImplTest method setUp.

@Before
public void setUp() {
    ResourceBundleProvider resourceBundleProvider = Mockito.mock(ResourceBundleProvider.class);
    context.registerService(ResourceBundleProvider.class, resourceBundleProvider);
    Mockito.when(resourceBundleProvider.getResourceBundle(null)).thenReturn(rootResourceBundle);
    Mockito.when(resourceBundleProvider.getResourceBundle(null, null)).thenReturn(rootResourceBundle);
}
Also used : ResourceBundleProvider(org.apache.sling.i18n.ResourceBundleProvider) Before(org.junit.Before)

Example 2 with ResourceBundleProvider

use of org.apache.sling.i18n.ResourceBundleProvider in project sling by apache.

the class ValidationServiceImpl method getDefaultResourceBundle.

@Nonnull
private ResourceBundle getDefaultResourceBundle() {
    Locale locale = Locale.ENGLISH;
    // go from highest ranked to lowest ranked providers
    for (int i = resourceBundleProviders.size() - 1; i >= 0; i--) {
        ResourceBundleProvider resourceBundleProvider = resourceBundleProviders.get(i);
        ResourceBundle defaultResourceBundle = resourceBundleProvider.getResourceBundle(locale);
        if (defaultResourceBundle != null) {
            return defaultResourceBundle;
        }
    }
    throw new IllegalStateException("There is no resource provider in the system, providing a resource bundle for locale");
}
Also used : Locale(java.util.Locale) ResourceBundleProvider(org.apache.sling.i18n.ResourceBundleProvider) ResourceBundle(java.util.ResourceBundle) Nonnull(javax.annotation.Nonnull)

Example 3 with ResourceBundleProvider

use of org.apache.sling.i18n.ResourceBundleProvider in project sling by apache.

the class MockSlingHttpServletRequest method getResourceBundle.

@Override
public ResourceBundle getResourceBundle(String baseName, Locale locale) {
    // check of ResourceBundleProvider is registered in mock OSGI context
    ResourceBundle resourceBundle = null;
    ServiceReference<ResourceBundleProvider> serviceReference = bundleContext.getServiceReference(ResourceBundleProvider.class);
    if (serviceReference != null) {
        ResourceBundleProvider provider = (ResourceBundleProvider) bundleContext.getService(serviceReference);
        resourceBundle = provider.getResourceBundle(baseName, locale);
    }
    // if no ResourceBundleProvider exists return empty bundle
    if (resourceBundle == null) {
        resourceBundle = EMPTY_RESOURCE_BUNDLE;
    }
    return resourceBundle;
}
Also used : ResourceBundleProvider(org.apache.sling.i18n.ResourceBundleProvider) ResourceBundle(java.util.ResourceBundle)

Example 4 with ResourceBundleProvider

use of org.apache.sling.i18n.ResourceBundleProvider in project sling by apache.

the class I18nRuntimeExtension method get.

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);
    if (resourceBundleProvider != null) {
        String key = text;
        if (StringUtils.isNotEmpty(hint)) {
            key += " ((" + hint + "))";
        }
        if (StringUtils.isEmpty(locale)) {
            Enumeration<Locale> requestLocales = request.getLocales();
            while (requestLocales.hasMoreElements()) {
                Locale l = requestLocales.nextElement();
                String translation = getTranslation(resourceBundleProvider, basename, key, l);
                if (translation != null) {
                    return translation;
                }
            }
        } else {
            try {
                Locale l = LocaleUtils.toLocale(locale);
                String translation = getTranslation(resourceBundleProvider, basename, key, l);
                if (translation != null) {
                    return translation;
                }
            } catch (IllegalArgumentException e) {
                LOG.warn("Invalid locale detected: {}.", locale);
                return text;
            }
        }
    }
    LOG.warn("No translation found for string '{}' using expression provided locale '{}' or default locale '{}'", text, locale, request.getLocale().getLanguage());
    return text;
}
Also used : Locale(java.util.Locale) ResourceBundleProvider(org.apache.sling.i18n.ResourceBundleProvider) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Example 5 with ResourceBundleProvider

use of org.apache.sling.i18n.ResourceBundleProvider in project sling by apache.

the class MockSlingHttpServletRequestTest method testResourceBundleFromProvider.

@Test
public void testResourceBundleFromProvider() {
    ResourceBundleProvider provider = mock(ResourceBundleProvider.class);
    bundleContext.registerService(ResourceBundleProvider.class.getName(), provider, null);
    when(provider.getResourceBundle("base1", Locale.US)).thenReturn(new ListResourceBundle() {

        @Override
        protected Object[][] getContents() {
            return new Object[][] { { "key1", "value1" } };
        }
    });
    ResourceBundle bundle = request.getResourceBundle("base1", Locale.US);
    assertNotNull(bundle);
    assertEquals("value1", bundle.getString("key1"));
    ResourceBundle bundle2 = request.getResourceBundle("base2", Locale.US);
    assertNotNull(bundle2);
    assertFalse(bundle2.getKeys().hasMoreElements());
}
Also used : ResourceBundleProvider(org.apache.sling.i18n.ResourceBundleProvider) ListResourceBundle(java.util.ListResourceBundle) ListResourceBundle(java.util.ListResourceBundle) ResourceBundle(java.util.ResourceBundle) Test(org.junit.Test)

Aggregations

ResourceBundleProvider (org.apache.sling.i18n.ResourceBundleProvider)5 ResourceBundle (java.util.ResourceBundle)3 Locale (java.util.Locale)2 ListResourceBundle (java.util.ListResourceBundle)1 Nonnull (javax.annotation.Nonnull)1 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)1 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)1 Before (org.junit.Before)1 Test (org.junit.Test)1