Search in sources :

Example 1 with MessageBundle

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;
}
Also used : MessageBundle(com.enonic.xp.i18n.MessageBundle)

Example 2 with MessageBundle

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);
}
Also used : Locale(java.util.Locale) LinkedHashSet(java.util.LinkedHashSet) MessageBundle(com.enonic.xp.i18n.MessageBundle) LocaleService(com.enonic.xp.i18n.LocaleService)

Example 3 with MessageBundle

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"));
}
Also used : MessageBundle(com.enonic.xp.i18n.MessageBundle) Test(org.junit.jupiter.api.Test)

Example 4 with MessageBundle

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"));
}
Also used : MessageBundle(com.enonic.xp.i18n.MessageBundle) Test(org.junit.jupiter.api.Test)

Example 5 with MessageBundle

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));
}
Also used : MessageBundle(com.enonic.xp.i18n.MessageBundle) LocalTime(java.time.LocalTime) Properties(java.util.Properties) LocalDate(java.time.LocalDate) Test(org.junit.jupiter.api.Test)

Aggregations

MessageBundle (com.enonic.xp.i18n.MessageBundle)26 Test (org.junit.jupiter.api.Test)22 Properties (java.util.Properties)6 Locale (java.util.Locale)3 ApplicationKey (com.enonic.xp.app.ApplicationKey)2 LocaleService (com.enonic.xp.i18n.LocaleService)2 LinkedHashSet (java.util.LinkedHashSet)2 Application (com.enonic.xp.app.Application)1 ResourceKeys (com.enonic.xp.resource.ResourceKeys)1 LocalDate (java.time.LocalDate)1 LocalTime (java.time.LocalTime)1