hans-georg1
(hans-georg gloeckler)
June 9, 2018, 11:32am
1
I am using Vaadin 8, java EE7 and Wildfly.
Can you please give me some advice for internationalization.
What is here “best practice”.
I try to make it like the example in https://github.com/peholmst/VerySimpleI18N
How must i change the methode updateMessageStrings() , that it works with vaadin 8
package org.vaadin.webinar.i18n;
import java.util.Locale;
import com.vaadin.ui.ComboBox;
public class LanguageSelector extends ComboBox implements Translatable {
private static final Locale SWEDISH = new Locale("sv");
private static final Locale FINNISH = new Locale("fi");
public LanguageSelector() {
addItem(Locale.ENGLISH);
addItem(FINNISH);
addItem(SWEDISH);
setValue(Locale.ENGLISH);
setNullSelectionAllowed(false);
addValueChangeListener(e -> getUI().setLocale((Locale) getValue()));
}
@Override
public void updateMessageStrings() {
Messages messages = Messages.getInstance();
setValue(getLocale());
setItemCaption(Locale.ENGLISH, messages.getMessage("languageSelector.en"));
setItemCaption(SWEDISH, messages.getMessage("languageSelector.sv"));
setItemCaption(FINNISH, messages.getMessage("languageSelector.fi"));
}
}
Petr100
(Petr Mikoláš)
June 10, 2018, 10:55am
2
I use in every custom component this, from accept-header in browser but I use spring messagges, after switch everything is translated:
private ResourceBundle bundle;
private Locale browserLocale;
@Override
protected void init(VaadinRequest request) {
browserLocale = VaadinService.getCurrentRequest().getLocale(); // from accept-header
bundle = ResourceBundle.getBundle("messages", browserLocale);
setLocale(browserLocale);
}
.
.
.
grid.addColumn(Person::getName).setCaption(bundle.getString("label.name"));
hans-georg1
(hans-georg gloeckler)
June 11, 2018, 10:53am
3
Hmmm
is it possible to make it without spring.
I want to make it like in the vaadin-example https://github.com/peholmst/VerySimpleI18N
but it doesn’t work with vaadin 8
@SuppressWarnings("serial")
@Theme("appui")
@CDIUI("")
public class MainUI extends UI {
@Inject
CDIViewProvider viewProvider;
@Inject
LoginView loginView;
private Navigator navigator;
@Override
protected void init(VaadinRequest request) {
...
}
@Override
public void setLocale(Locale locale) {
super.setLocale(locale);
updateMessageStrings(getContent());
}
private void updateMessageStrings(Component component) {
if (component instanceof Translatable) {
((Translatable) component).updateMessageStrings();
}
if (component instanceof HasComponents) {
((HasComponents) component).iterator().forEachRemaining(this::updateMessageStrings);
}
}
}
@SuppressWarnings("serial")
@UIScoped
public class LoginView extends VerticalLayout implements View, Translatable {
private String welcomeMessageCaption = "Welcome, please Login - Test";
public LoginView() {
welcomeMessage = new Label(welcomeMessageCaption);
...
addComponent(welcomeMessage);
addComponent(languageSelector);
}
@Override
public void updateMessageStrings() {
final Messages messages = Messages.getInstance();
welcomeMessage.setCaption(messages.getMessage("auth.welcomemessage"));
languageSelector.setCaption(messages.getMessage("languageSelector.caption"));
}
}
public class LanguageSelector extends ComboBox<Locale> implements Translatable {
private static final long serialVersionUID = 1L;
private static final Locale SWEDISH = new Locale("sv");
private static final Locale FINNISH = new Locale("fi");
public LanguageSelector() {
setItems(getLanguageList());
setValue(Locale.ENGLISH);
setEmptySelectionAllowed(false);
addValueChangeListener(e -> {
getUI().setLocale((Locale) getValue());
});
}
@Override
public void updateMessageStrings() {
setValue(getLocale());
setItemCaptionGenerator(Locale::getLanguage);
setItems(getLanguageList());
}
private List<Locale> getLanguageList() {
List<Locale> languageList = new ArrayList<>();
languageList.add(Locale.ENGLISH);
languageList.add(Locale.GERMAN);
languageList.add(SWEDISH);
languageList.add(FINNISH);
return languageList;
}
}
hans-georg1
(hans-georg gloeckler)
June 12, 2018, 8:48am
4
I have solved the problem
Jose_Fco
(Jose Fco Rodriguez Torres)
October 27, 2018, 4:23pm
5
Hi hans, could you please explain how you solved?