use of com.enonic.xp.i18n.MessageBundle in project xp by enonic.
the class LocaleScriptBean method localize.
public String localize(final String key, final List<String> locales, final ScriptValue values, String[] bundles) {
final String locale = getPreferredLocale(locales, bundles);
final MessageBundle bundle = getMessageBundle(locale, bundles);
if (bundle == null) {
return null;
}
final String localizedMessage = bundle.localize(key, toArray(values));
return localizedMessage != null ? localizedMessage : NOT_TRANSLATED_MESSAGE;
}
use of com.enonic.xp.i18n.MessageBundle in project xp by enonic.
the class LocalizeNoHttpTest method initialize.
@Override
protected void initialize() throws Exception {
super.initialize();
final LocaleService localeService = Mockito.mock(LocaleService.class);
final Set<Locale> locales = new LinkedHashSet<>();
locales.add(new Locale("en"));
Mockito.when(localeService.getLocales(eq(ApplicationKey.from("com.enonic.myapplication")), any())).thenReturn(locales);
final MessageBundle bundle = Mockito.mock(MessageBundle.class, this::answer);
Mockito.when(localeService.getBundle(eq(ApplicationKey.from("com.enonic.myapplication")), any(), any())).thenReturn(bundle);
addService(LocaleService.class, localeService);
}
use of com.enonic.xp.i18n.MessageBundle in project xp by enonic.
the class LocaleServiceImplTest method get_bundle_norwegian_nb.
@Test
public void get_bundle_norwegian_nb() {
final MessageBundle bundle = localeService.getBundle(ApplicationKey.from("myapplication"), Locale.forLanguageTag("nb"), "norwegian");
assertThat(bundle.asMap()).containsExactlyInAnyOrderEntriesOf(Map.of("a", "default", "b", "nb", "d", "no"));
}
use of com.enonic.xp.i18n.MessageBundle in project xp by enonic.
the class MessageBundleImplTest method testNonExistingKey.
@Test
public void testNonExistingKey() {
MessageBundle resourceBundle = createDefault();
assertNull(resourceBundle.localize("dummyKey"));
}
use of com.enonic.xp.i18n.MessageBundle in project xp by enonic.
the class MessageBundleImplTest method localDateTime.
@Test
public void localDateTime() {
LocalTime lt = LocalTime.of(13, 56, 4);
LocalDate ld = LocalDate.of(2021, 5, 3);
long ltms = lt.atDate(LocalDate.EPOCH).atZone(ZoneOffset.UTC).toInstant().toEpochMilli();
long ldms = ld.atTime(LocalTime.MIN).atZone(ZoneOffset.UTC).toInstant().toEpochMilli();
final Properties properties = new Properties();
properties.put("key1", "At {1,time,medium} on {0,date,short}");
MessageBundle resourceBundle = new MessageBundleImpl(properties, Locale.US);
assertEquals("At 1:56:04 PM on 5/3/21", resourceBundle.localize("key1", ldms, ltms));
}
Aggregations