Frage

I'm trying to make a program localized in Java.

package javaapplication8;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

public class LanguageController {

    private final Map supportedLanguages;
    private final ResourceBundle translation;

    public LanguageController(String language){
        supportedLanguages = new HashMap();
        supportedLanguages.put("English",Locale.ENGLISH);
        supportedLanguages.put("Italiano",Locale.ITALIAN); 
//here I get error
        translation = ResourceBundle.getBundle("language", supportedLanguages.get(language)); 
    }

    public String getWord(String keyword)
    {
        return translation.getString(keyword);
    }
}

Than in a class I try to print a word in two different languages, italian and english. I have two proprieties file

  • Language.proprieties
  • Language_it.proprieties

screen

In the class:

LanguageController langController_it = new LanguageController("Italiano");
System.out.println(langController_it.getWord("Option"));
LanguageController langController_en = new LanguageController("English");
System.out.println(langController_en.getWord("Option"));

EDIT: First problem solution java.util.MissingResourceException: Can't find bundle for base name 'property_file name', locale en_US

I still have error in that line supportedLanguages.get(language)

War es hilfreich?

Lösung

There are several problems with your application (and with your question for that matter).
First of all, you do not use parametrized collection:

private final Map supportedLanguages;

This map will always return Object, but the getBundle() method has different signature:

public static ResourceBundle getBundle(String baseName, Locale locale);

I am sure that's exactly what Netbeans is complaining about. The ugly way to fix this would be to cast the parameter to Locale:

translation = ResourceBundle.getBundle("language", (Locale) supportedLanguages.get(language));

The better way would be to use type parameters in Map declaration:

private final Map<String, Locale> supportedLanguages = new HashMap<>();

Another possible issue with your application is where you keep properties files with translations. Unfortunately, Java is extremely sensitive where it comes to file location and you have to provide the fully qualified path to a properties file. It changes a bit with Java 8 and ResourceBundle's SPI providers, but that's a different story.

Last, but not least, it seems that you are trying to implement the common anti-pattern, that is language switcher. If you are implementing desktop application, please don't do this mistake! It is just enough to get user interface default locale:

Locale locale = Locale.getDefault(LocaleCategory.DISPLAY);

Believe it or not, but the ResourceBundle class will try to fall-back to the most appropriate language for the user. If I already have set the UI language in my Operating System preferences, why are you bothering to make a choice again?
Honestly, language switcher make sense for static web sites sometimes, but not for web applications, and definitely not for desktop applications.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top