Search in sources :

Example 1 with I18NProvider

use of com.vaadin.flow.i18n.I18NProvider in project flow by vaadin.

the class I18NProviderTest method with_defined_provider_locale_should_be_the_available_one.

@Test
public void with_defined_provider_locale_should_be_the_available_one() throws ServletException, ServiceException {
    config.setApplicationOrSystemProperty(InitParameters.I18N_PROVIDER, TestProvider.class.getName());
    initServletAndService(config);
    I18NProvider i18NProvider = VaadinService.getCurrent().getInstantiator().getI18NProvider();
    Assert.assertNotNull("No provider for ", i18NProvider);
    Assert.assertEquals("Locale was not the defined locale", i18NProvider.getProvidedLocales().get(0), VaadinSession.getCurrent().getLocale());
}
Also used : I18NProvider(com.vaadin.flow.i18n.I18NProvider) Test(org.junit.Test)

Example 2 with I18NProvider

use of com.vaadin.flow.i18n.I18NProvider in project flow by vaadin.

the class VaadinService method setLocale.

private void setLocale(VaadinRequest request, VaadinSession session) {
    I18NProvider provider = getInstantiator().getI18NProvider();
    List<Locale> providedLocales = provider.getProvidedLocales();
    if (providedLocales.size() == 1) {
        session.setLocale(providedLocales.get(0));
    } else {
        Optional<Locale> foundLocale = LocaleUtil.getExactLocaleMatch(request, providedLocales);
        if (!foundLocale.isPresent()) {
            foundLocale = LocaleUtil.getLocaleMatchByLanguage(request, providedLocales);
        }
        // or else leave as default locale
        if (foundLocale.isPresent()) {
            session.setLocale(foundLocale.get());
        } else if (!providedLocales.isEmpty()) {
            session.setLocale(providedLocales.get(0));
        }
    }
}
Also used : Locale(java.util.Locale) I18NProvider(com.vaadin.flow.i18n.I18NProvider)

Aggregations

I18NProvider (com.vaadin.flow.i18n.I18NProvider)2 Locale (java.util.Locale)1 Test (org.junit.Test)1