Can I localize the JOptionPane Yes/No/Cancel option?
Asked Answered
W

3

7

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.

Wards answered 4/1, 2013 at 13:58 Comment(2)
try showOptionDialog check this link as wellPodgy
should happen by default - what's your Locale?Ting
V
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!

Vibrato answered 12/8, 2013 at 14:25 Comment(4)
hmm ... nothing special needed, should work automatically - except maybe for exoctic Locales that are not supported by the jdkTing
This works perfect and is the simplest and cleanest way. Just one line, why isn't this accepted or the highest voted?Teahan
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!Dada
Locale.setDefault(locale); and JOptionPane.setDefaultLocale(locale); should be used to "fully" switch the locale how it seemsTangier
L
4

This is enough:

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

etc

Leftist answered 2/11, 2014 at 22:27 Comment(1)
Simplest solution.Combination
A
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");
Atharvaveda answered 31/3, 2016 at 8:59 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.