Internationalization - MissingResourceException

Hey guys.

I’m working on a Vaadin Touchkit application (with maven). I want to add a function which makes it possible for the user to change default language (for example, if default is English, user should be able to change it to something else, like norwegian). I want to use properties (no xml) and ResourceBundle, making something easy from scratch.
I was thinking about a login form, with a button on the navigationbar which the user can toggle and swap between languages. The language he choose should be the default for the application.

I think I’ve implemented something that should work, but I’m getting an error:

java.util.MissingResourceException: Can't find bundle for base name no.example.testapp.languages.language, locale no
        at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1499)
        at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1322)
        at java.util.ResourceBundle.getBundle(ResourceBundle.java:1028)
        at no.example.testapp.ui.Constant.initBundle(Constant.java:16)
        at no.example.testapp.ui.LoginView.<init>(LoginView.java:32)
        at no.example.testapp.MyUIProvider.init(MyUIProvider.java:36)

I can’t seem to fix this error. I’ve checked a lot on the Internet, tried different methods without luck. Same error always.

So my structure look like this:

no.example.testapp → MyUIProvider.java
no.example.testapp.ui → LoginView.java and Constant.java
no.example.testapp.languages → language_no.properties and language_en.properties

This is what my Constant.java file looks like:

package no.example.testapp.ui

import java.util.Locale;
import java.util.ResourceBundle;

public class Constant 
{
    public static ResourceBundle _BUNDLE = null;
    public static Integer _LANGUAGE = 0; // 0 = norsk, 1 = engelsk
    static ClassLoader cl = Thread.currentThread().getContextClassLoader();
    
    public static void initBundle()
    {
        if(_LANGUAGE == 0)
        {
            _BUNDLE = ResourceBundle.getBundle("no.example.testapp.languages.language", new Locale("no"), cl);
        }
        else if(_LANGUAGE == 1)
        {
            _BUNDLE = ResourceBundle.getBundle("no.example.testapp.languages.language", new Locale("en"), cl);
        }
    }
}

This is how I set the text on a label on my LoginView class. This is not the whole class, just some of the importants parts (Constant.initBundle fails for example, line 32).

public LoginView()
    {
        /****/
        
        Constant.initBundle();
        
        /****/
        
        setCaption(Constant._BUNDLE.getString("LoginView.title")); //$NON-NLS-1$

        Button toggleButton = new Button("Toggle");
        toggleButton.addClickListener(new ClickListener()
        {
            
            @Override
            public void buttonClick(ClickEvent event)
            {
                if(Constant._LANGUAGE == 0) 
                {
                    Constant._LANGUAGE = 1;
                }
                else if(Constant._LANGUAGE == 1) 
                {
                    Constant._LANGUAGE = 0;
                }
                
                MyUIProvider.setView(new LoginView());
            }
        });
}

I’ve tried to follow this method:
https://www.youtube.com/watch?v=UDEA8PqmcuY

AS you see, I still get the error.

I tried to follow this guide as well: http://www.javacodegeeks.com/2012/02/customized-internationalization-i18n-in.html
Still same error.

Does anyone see why I keep getting this error? Any other ways to solve this problem?

I’ve seen the GasProvider project, but it was kinda hard.

2 years laters and noone knows how to load the ResourceBundle on Vaadin 7, or where to place the properties

For a maven project:


  • Place your properties file into src/main/resources/VAADIN
  • Default behavior of maven places this resources into …/target/classes (you can examine this in the war)
  • To get the properties file you just have to use “VAADIN.filename”

If you want to keep your structure and thus change the folder location of the resources in maven, follow this guide:

http://www.mkyong.com/maven/how-to-change-maven-resources-folder-location/