0

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)

Community
  • 1
  • 1
Mitro
  • 1,230
  • 8
  • 32
  • 61
  • what your question ? your program looks like Localization not I18n – anish Mar 25 '14 at 09:52
  • @anish excuse me, which is the difference? – Mitro Mar 25 '14 at 09:56
  • 1
    you are adding language support for your product using Locale concept, refer to the WIki, http://en.wikipedia.org/wiki/Language_localization, for I18n you have to understand the UTF support in your product – anish Mar 25 '14 at 13:07

1 Answers1

1

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.

Paweł Dyda
  • 18,366
  • 7
  • 57
  • 79
  • Very clear, but I don't understand the last part. What do you mean for anti-pattern? I'm doing a program with several languages, just for fun and learn, if I want a multilanguage program I just have to create proprieties file and than the OS will chose the correct one? – Mitro Mar 26 '14 at 14:08
  • @AlessioMTX: Correct, all you have to do is to create per-language properties files in your case language.properties, language_it.properties and read a default Locale from OS. This way users with Italian version of OS will see Italian translations and they won't have to select anything. – Paweł Dyda Mar 26 '14 at 20:27