Directory

← Back

I18N4Vaadin

A small add-on for creating localized applications

Author

Rating

Popularity

<100

I18N4Vaadin is a small and simple add-on for making it easier to localize Vaadin applications. By using I18N4Vaadin, you do not have to implement a new localization system for every application and you can even change the language of your application on the fly without having to restart it.

This add-on is no longer maintained.

Sample code

@Message(key = "greeting", value = "Hello {0}! How is it going?")
private void sayHello() {
    Notification.show(bundle.greeting(name.getValue()));
}
@VaadinUI
@Root
public class DemoUI extends UI implements LocaleChangedListener {

    @I18nSupportedLocales({
        @Locale(language = "en"),
        @Locale(language = "sv"),
        @Locale(language = "fi")
    })
    @Inject
    private I18N i18n;
    @Inject
    private DemoUIBundle bundle;
    @Inject
    private CDIViewProvider viewProvider;
    private ComboBox languageChanger;

    @Override
    protected void init(VaadinRequest request) {
        VerticalLayout content = new VerticalLayout();
        content.setMargin(true);
        content.setSpacing(true);
        content.setSizeFull();
        setContent(content);

        languageChanger = new ComboBox();
        languageChanger.setContainerDataSource(new BeanItemContainer<java.util.Locale>(java.util.Locale.class, i18n.getSupportedLocales()));
        languageChanger.setImmediate(true);
        languageChanger.addValueChangeListener(new Property.ValueChangeListener() {
            @Override
            public void valueChange(Property.ValueChangeEvent event) {
                if (languageChanger.getValue() != null) {
                    i18n.setLocale((java.util.Locale) languageChanger.getValue());
                }
            }
        });
        content.addComponent(languageChanger);

        Panel viewContent = new Panel();
        viewContent.setSizeFull();
        content.addComponent(viewContent);
        content.setExpandRatio(viewContent, 1);

        Navigator navigator = new Navigator(this, viewContent);
        navigator.addProvider(viewProvider);
        navigator.navigateTo("demo");
        setNavigator(navigator);

        updateStrings();
    }

    @Override
    public void attach() {
        super.attach();
        // We cannot use CDI events due to the way Vaadin CDI is currently implemented
        i18n.addLocaleChangedListener(this);
    }

    @Override
    public void detach() {
        // Remember to clean up afterwards, otherwise we will get memory leaks (the I18N instance is session scoped)
        i18n.removeLocaleChangedListener(this);
        super.detach();
    }

    @Messages({
        @Message(key = "application.title", value = "I18N4Vaadin CDI Demo Application"),
        @Message(key = "languageChanger.caption", value = "Change language")
    })
    private void updateStrings() {
        getPage().setTitle(bundle.application_title());
        languageChanger.setCaption(bundle.languageChanger_caption());
        languageChanger.setValue(i18n.getLocale());
    }

    @Override
    public void localeChanged(LocaleChangedEvent event) {
        updateStrings();
    }
}
public class DemoUI extends UI implements LocaleChangedListener, I18NProvider {

    private I18N i18n = new SimpleI18N(Arrays.asList(new Locale("en"), new Locale("sv"), new Locale("fi")));
    private DemoUIBundle bundle = new DemoUIBundle();
    private ComboBox languageChanger;

    static {
        I18NHolder.setStrategy(new I18NProvidingUIStrategy());
    }

    @Override
    protected void init(VaadinRequest request) {
        VerticalLayout content = new VerticalLayout();
        content.setMargin(true);
        content.setSpacing(true);
        content.setSizeFull();
        setContent(content);

        languageChanger = new ComboBox();
        languageChanger.setContainerDataSource(new BeanItemContainer<java.util.Locale>(java.util.Locale.class, i18n.getSupportedLocales()));
        languageChanger.setImmediate(true);
        languageChanger.addValueChangeListener(new Property.ValueChangeListener() {
            @Override
            public void valueChange(Property.ValueChangeEvent event) {
                if (languageChanger.getValue() != null) {
                    i18n.setLocale((java.util.Locale) languageChanger.getValue());
                }
            }
        });
        content.addComponent(languageChanger);

        Panel viewContent = new Panel();
        viewContent.setSizeFull();
        content.addComponent(viewContent);
        content.setExpandRatio(viewContent, 1);

        Navigator navigator = new Navigator(this, viewContent);
        navigator.addView("demo", DemoView.class);
        navigator.navigateTo("demo");
        setNavigator(navigator);

        updateStrings();
    }

    @Override
    public void attach() {
        super.attach();
        i18n.addLocaleChangedListener(this);
    }

    @Override
    public void detach() {
        i18n.removeLocaleChangedListener(this);
        super.detach();
    }

    @Messages({
        @Message(key = "application.title", value = "I18N4Vaadin CDI Demo Application"),
        @Message(key = "languageChanger.caption", value = "Change language")
    })
    private void updateStrings() {
        getPage().setTitle(bundle.application_title());
        languageChanger.setCaption(bundle.languageChanger_caption());
        languageChanger.setValue(i18n.getLocale());
    }

    @Override
    public void localeChanged(LocaleChangedEvent event) {
        updateStrings();
    }

    @Override
    public I18N getI18N() {
        return i18n;
    }
}

Compatibility

(Loading compatibility data...)

Was this helpful? Need more help?
Leave a comment or a question below. You can also join the chat on Discord or ask questions on StackOverflow.

Version

Release 2.0.0 final. No API changes.

Released
2013-10-01
Maturity
STABLE
License
Apache License 2.0

Compatibility

Framework
Vaadin 7.1+
Vaadin 7.0+ in 2.0.0.beta2
Vaadin 6.0+ in 0.9.1
Browser
Browser Independent

I18N4Vaadin - Vaadin Add-on Directory

A small add-on for creating localized applications I18N4Vaadin - Vaadin Add-on Directory
I18N4Vaadin is a small and simple add-on for making it easier to localize Vaadin applications. By using I18N4Vaadin, you do not have to implement a new localization system for every application and you can even change the language of your application on the fly without having to restart it. This add-on is no longer maintained.
Online