Can I localize the JOptionPane Yes/No/Cancel option?

Nikola Luburic picture Nikola Luburic · Jan 4, 2013 · Viewed 8.6k times · Source

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.

Answer

jfajunior picture jfajunior · Aug 12, 2013

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!