vaadin / flow

Vaadin Flow is a Java framework binding Vaadin web components to Java. This is part of Vaadin 10+.
Apache License 2.0
591 stars 164 forks source link

Provide a way to customize the translations for Hilla #19399

Open jcgueriaud1 opened 1 month ago

jcgueriaud1 commented 1 month ago

Description of the bug

It seems not possible to change the TranslationProvider for Hilla and use the i18n object, because the TranslationFileRequestHandler requires DefaultI18NProvider

https://github.com/vaadin/flow/blob/main/flow-server/src/main/java/com/vaadin/flow/i18n/TranslationFileRequestHandler.java#L72

Expected behavior

I want to be able to update the TranslationProvider to update the default lang or use a database.

One solution could be a new interface or at least test if the TranslationProvider is a subclass of the DefaultI18NProvider

Minimal reproducible example

https://github.com/jcgueriaud1/spring-petclinic-vaadin-flow/tree/hybrid

And add this class:

package org.springframework.samples.petclinic.backend.system;

import java.util.List;
import java.util.Locale;

import org.springframework.stereotype.Component;

import com.vaadin.flow.i18n.DefaultI18NProvider;

@Component
public class TranslationProvider extends DefaultI18NProvider {

    public TranslationProvider() {
        super(List.of(Locale.forLanguageTag("de"), Locale.forLanguageTag("en"), Locale.forLanguageTag("es")));
    }

}

Versions

knoobie commented 1 month ago

See here https://github.com/vaadin/flow/pull/18680#discussion_r1507403653 why it's done like this

jcgueriaud1 commented 1 month ago

Thanks, that's a big limitation. I have 2 languages German and English and my application is in German by default.

I don't understand German 😭

knoobie commented 1 month ago

Zeit deutsch zu lernen 😉 (time to learn German)

If you switch the order of the languages.. English should be used first, or?

jcgueriaud1 commented 1 month ago

If you switch the order of the languages

The DefaultI18NProvider doesn't allow you to do that. So I implemented my own which is not used

knoobie commented 1 month ago

I would have expected you can register a bean like so new DefaultI18NProvider(yourLocales) and the default bean backs off and yours would win with your locale order

jcgueriaud1 commented 1 month ago

That's exactly the purpose of the ticket 😄

knoobie commented 1 month ago

The following should also work:


@Configuration
public class MyBeans {

  @Bean
  public I18NProvider myProvider() {
    return new DefaultI18NProvider(List.of(Locale.GERMAN));
  }
}
jcgueriaud1 commented 1 month ago

This workaround works in my case.