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);
}
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");
}
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;
}
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;
}
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());
}
Aggregations