Internationalization in Java

0

I'm having some difficulty in internalizing my java application.

My question is this: when I have a login screen, for example, and select the option in my JMenuItem: "English", my entire application, my layout, should change according to the selected option , leaving it all in English. I would like to know how to do this.

Here's my code of how I'm doing I'm trying to do:

    
asked by anonymous 02.09.2016 / 12:56

1 answer

1

So you can do this by using a language file. It would be something like:

1 - Method to retrieve the selected language file based on the selected language option in JMenuItem (.lang files would be in the project lang folder):

class Language {
private Properties language;
public void loadLanguage(int lang) throws FileNotFoundException,
        IOException {
    if (lang == 0)
        language = getLanguageProperties("lang/english.lang");
    else if (lang == 1)
        language = getLanguageProperties("lang/portuguese.lang");
}

protected Properties getLanguageProperties(String languageFile)
        throws FileNotFoundException, IOException {
    File file = new File(languageFile);
    Properties props = new Properties();
    props.load(new FileInputStream(file));
    return props;
}
}

2- Retrieve the text of a component through your key:

class LanguageController {
    public static String getProperty(String key) {
       return language.getProperty(key);
    }
}

3- Load text from application swing components:

JButton componente = new JButton();
componente.setText(
            LanguageController.getProperty(
                    "BOTAO_KEY")

4- The language file would contain the content with the key and value ("portuguese.lang"):

BOTAO_KEY=Texto do meu button

If you're curious, take a look at the project where I did this internationalization treatment: link . There is a language menu that allows the user to change the language at runtime. I hope I have helped ^^

    
02.09.2016 / 13:26