Search in sources :

Example 6 with ResourceKeys

use of com.enonic.xp.resource.ResourceKeys in project xp by enonic.

the class LocaleServiceImpl method getAppLocales.

private Set<Locale> getAppLocales(final ApplicationKey applicationKey, final String... bundleNames) {
    LOG.debug("Create app locales for {}", applicationKey);
    final Set<Locale> locales = new LinkedHashSet<>();
    for (final String bundleName : bundleNames) {
        final String bundlePattern = "^" + Pattern.quote(bundleName.startsWith("/") ? bundleName : "/" + bundleName) + ".*\\.properties$";
        final ResourceKeys resourceKeys = resourceService.findFiles(applicationKey, bundlePattern);
        for (ResourceKey resourceKey : resourceKeys) {
            final Locale locale = localeFromResource(resourceKey.getName());
            locales.add(locale);
            if (locale.equals(LOCALE_NO)) {
                locales.add(LOCALE_NB);
                locales.add(LOCALE_NN);
            }
            if (locale.getLanguage().equals(LOCALE_NB.getLanguage()) || locale.getLanguage().equals(LOCALE_NN.getLanguage())) {
                locales.add(LOCALE_NO);
            }
        }
    }
    return locales;
}
Also used : Locale(java.util.Locale) LinkedHashSet(java.util.LinkedHashSet) ResourceKeys(com.enonic.xp.resource.ResourceKeys) ResourceKey(com.enonic.xp.resource.ResourceKey)

Example 7 with ResourceKeys

use of com.enonic.xp.resource.ResourceKeys in project xp by enonic.

the class LocaleServiceImplTest method getSupportedLocale_moreThanOneFound.

@Test
public void getSupportedLocale_moreThanOneFound() {
    final ResourceKeys resourceKeys = ResourceKeys.from("myapplication:/site/i18n/myphrases.properties", "myapplication:/site/i18n/myphrases_en.properties", "myapplication:/site/i18n/myphrases_en_US.properties", "myapplication:/site/i18n/myphrases_en_US_1.properties", "myapplication:/site/i18n/myphrases_fr.properties", "myapplication:/site/i18n/myphrases_ca.properties");
    when(resourceService.findFiles(any(), anyString())).thenReturn(resourceKeys);
    List<Locale> preferredLocales = localeList("no", "ca-ES", "en");
    Locale supportedLocale = localeService.getSupportedLocale(preferredLocales, ApplicationKey.from("myapplication"), "/myphrases");
    assertNotNull(supportedLocale);
    assertEquals("ca", supportedLocale.toLanguageTag());
}
Also used : Locale(java.util.Locale) ResourceKeys(com.enonic.xp.resource.ResourceKeys) Test(org.junit.jupiter.api.Test)

Example 8 with ResourceKeys

use of com.enonic.xp.resource.ResourceKeys in project xp by enonic.

the class LocaleServiceImplTest method getSupportedLocale_nb_file_supports_no_locale.

@Test
public void getSupportedLocale_nb_file_supports_no_locale() {
    final ResourceKeys resourceKeys = ResourceKeys.from("myapplication:/site/i18n/myphrases.properties", "myapplication:/site/i18n/myphrases_nb.properties");
    when(resourceService.findFiles(any(), anyString())).thenReturn(resourceKeys);
    List<Locale> preferredLocales = localeList("no");
    Locale supportedLocale = localeService.getSupportedLocale(preferredLocales, ApplicationKey.from("myapplication"), "/myphrases");
    assertNotNull(supportedLocale);
    assertEquals("no", supportedLocale.toLanguageTag());
}
Also used : Locale(java.util.Locale) ResourceKeys(com.enonic.xp.resource.ResourceKeys) Test(org.junit.jupiter.api.Test)

Example 9 with ResourceKeys

use of com.enonic.xp.resource.ResourceKeys in project xp by enonic.

the class LocaleServiceImplTest method getSupportedLocale_no_file_supports_nb_locale.

@Test
public void getSupportedLocale_no_file_supports_nb_locale() {
    final ResourceKeys resourceKeys = ResourceKeys.from("myapplication:/site/i18n/myphrases.properties", "myapplication:/site/i18n/myphrases_no.properties");
    when(resourceService.findFiles(any(), anyString())).thenReturn(resourceKeys);
    List<Locale> preferredLocales = localeList("nb");
    Locale supportedLocale = localeService.getSupportedLocale(preferredLocales, ApplicationKey.from("myapplication"), "/myphrases");
    assertNotNull(supportedLocale);
    assertEquals("nb", supportedLocale.toLanguageTag());
}
Also used : Locale(java.util.Locale) ResourceKeys(com.enonic.xp.resource.ResourceKeys) Test(org.junit.jupiter.api.Test)

Example 10 with ResourceKeys

use of com.enonic.xp.resource.ResourceKeys in project xp by enonic.

the class LocaleServiceImplTest method getSupportedLocale_no_file_supports_nn_locale.

@Test
public void getSupportedLocale_no_file_supports_nn_locale() {
    final ResourceKeys resourceKeys = ResourceKeys.from("myapplication:/site/i18n/myphrases.properties", "myapplication:/site/i18n/myphrases_no.properties");
    when(resourceService.findFiles(any(), anyString())).thenReturn(resourceKeys);
    List<Locale> preferredLocales = localeList("nn");
    Locale supportedLocale = localeService.getSupportedLocale(preferredLocales, ApplicationKey.from("myapplication"), "/myphrases");
    assertNotNull(supportedLocale);
    assertEquals("nn", supportedLocale.toLanguageTag());
}
Also used : Locale(java.util.Locale) ResourceKeys(com.enonic.xp.resource.ResourceKeys) Test(org.junit.jupiter.api.Test)

Aggregations

ResourceKeys (com.enonic.xp.resource.ResourceKeys)13 Test (org.junit.jupiter.api.Test)12 Locale (java.util.Locale)11 Application (com.enonic.xp.app.Application)1 ApplicationKey (com.enonic.xp.app.ApplicationKey)1 MessageBundle (com.enonic.xp.i18n.MessageBundle)1 ResourceKey (com.enonic.xp.resource.ResourceKey)1 LinkedHashSet (java.util.LinkedHashSet)1