Java Code Examples for com.vaadin.flow.component.UI#setLocale()

The following examples show how to use com.vaadin.flow.component.UI#setLocale() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BootstrapHandler.java    From flow with Apache License 2.0 5 votes vote down vote up
protected BootstrapContext createAndInitUI(Class<? extends UI> uiClass,
        VaadinRequest request, VaadinResponse response,
        VaadinSession session) {

    UI ui = ReflectTools.createInstance(uiClass);
    ui.getInternals().setContextRoot(
            request.getService().getContextRootRelativePath(request));

    PushConfiguration pushConfiguration = ui.getPushConfiguration();

    ui.getInternals().setSession(session);
    ui.setLocale(session.getLocale());

    BootstrapContext context = createBootstrapContext(request, response, ui,
            request.getService()::getContextRootRelativePath);

    Optional<Push> push = context
            .getPageConfigurationAnnotation(Push.class);

    DeploymentConfiguration deploymentConfiguration = context.getSession()
            .getService().getDeploymentConfiguration();
    PushMode pushMode = push.map(Push::value)
            .orElseGet(deploymentConfiguration::getPushMode);
    setupPushConnectionFactory(pushConfiguration, context);
    pushConfiguration.setPushMode(pushMode);
    pushConfiguration.setPushUrl(deploymentConfiguration.getPushURL());
    push.map(Push::transport).ifPresent(pushConfiguration::setTransport);

    // Set thread local here so it is available in init
    UI.setCurrent(ui);
    ui.doInit(request, session.getNextUIid());
    session.addUI(ui);

    // After init and adding UI to session fire init listeners.
    session.getService().fireUIInitListeners(ui);

    initializeUIWithRouter(request, ui);

    return context;
}
 
Example 2
Source File: BinderTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void conversionWithLocaleBasedErrorMessage() {
    TestTextField ageField = new TestTextField();

    String fiError = "VIRHE";
    String otherError = "ERROR";

    StringToIntegerConverter converter = new StringToIntegerConverter(
            context -> context.getLocale().map(Locale::getLanguage)
                    .orElse("en").equals("fi") ? fiError : otherError);

    binder.forField(ageField).withConverter(converter).bind(Person::getAge,
            Person::setAge);
    binder.setBean(item);

    UI testUI = new UI();
    UI.setCurrent(testUI);

    testUI.add(ageField);

    ageField.setValue("not a number");
    assertEquals(otherError, ageField.getErrorMessage());

    testUI.setLocale(new Locale("fi", "FI"));

    // Re-validate to get the error message with correct locale
    binder.validate();
    assertEquals(fiError, ageField.getErrorMessage());
}
 
Example 3
Source File: BeanBinderTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    ui = new UI();
    ui.setLocale(Locale.ENGLISH);
    UI.setCurrent(ui);

    binder = new BeanValidationBinder<>(BeanToValidate.class);
    item = new BeanToValidate();
    item.setFirstname("Johannes");
    item.setAge(32);
}
 
Example 4
Source File: ValueContextTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void getLocale_localeComesFromComponentUI() {
    UI.setCurrent(null);

    UI ui = new UI();
    ui.setLocale(Locale.GERMAN);

    Text text = new Text("");
    ui.add(text);
    ValueContext context = new ValueContext(text);

    Assert.assertEquals(Locale.GERMAN, context.getLocale().get());
}