6

I have a question regarding the JOptionPane.showConfirmDialog. The buttons I get are Yes, No and Cancel, but I was wondering if it was possible to localize these three buttons to my language? I realize I can just create a confirmation dialogue of my own with my JButtons but I was wondering if this was possible as well.

Thanks in advance.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
Nikola Luburic
  • 133
  • 1
  • 3
  • 10

3 Answers3

13

The simple way is to set the JOptionPane with the desired locale, like this:

Locale locale = new Locale("pt","BR");
JOptionPane.setDefaultLocale(locale);

If you have a singleton class that treats all the messages from your ResourceBundle, then you just need to set it once in the locale initialization. For example:

public void setLocale(Locale locale) {
    if (_bundle == null || !locale.equals(_locale)) {
        _locale = locale;
        _bundle = ResourceBundle.getBundle("resources.i18n.MessagesBundle", locale);

        JOptionPane.setDefaultLocale(_locale);

        _logger.info("Got new resource bundle for language-country: " + _locale.getLanguage() + "-" +
                locale.getCountry());
    }
}

Cheers!

jfajunior
  • 1,211
  • 1
  • 15
  • 19
  • 1
    hmm ... nothing special needed, should work automatically - except maybe for exoctic Locales that are not supported by the jdk – kleopatra Aug 12 '13 at 14:44
  • 2
    This works perfect and is the simplest and cleanest way. Just one line, why isn't this accepted or the highest voted? – Pascal Schneider Mar 02 '17 at 07:58
  • 1
    I switch from OS to OS with the same project, and two computers has different default locale. It seems Java detects the system locale and shows `yes`/`no` in your language, but if the font used doesn't support the other locale characters, it's annoying. So force the locale to be one, as this answer suggests, works. Thx! – WesternGun Mar 03 '17 at 00:28
  • `Locale.setDefault(locale);` and `JOptionPane.setDefaultLocale(locale);` should be used to "fully" switch the locale how it seems – BullyWiiPlaza Jul 08 '17 at 18:36
3

This is enough:

UIManager.put("OptionPane.yesButtonText", "Ya");

etc

Pascal Schneider
  • 405
  • 1
  • 5
  • 19
BZM
  • 31
  • 1
2

All the localized messages for standard dialogs for most used languages, German included, are provided by Swing PLAF resource bundles, and you can find them in the JDK sources. See e.g. for messages in English:

String values for languages other than English are provided by adjacent bundle files.

And in case of a rarely used language you can add one more bundle to any of these families just by creating one more file for desired language and placing it anywhere on your classpath. Bundles in .java and .properties format work equally well, though .java format may be slightly more Unicode-friendly...

It may be good to keep in mind though that direct adding of content to com.sun package may violate the Java license. So to be on the safe side, it may be wise to move your extra resources to a package of your own and register it with UIManager like this:

UIManager.getDefaults().addResourceBundle("mypackage.swing.plaf.basic.resources.basic");
Sergey Ushakov
  • 2,425
  • 1
  • 24
  • 15