How to set locale for application at runtime?
Using Netbeans 5.5.1, JDK 1.6.0_03.
Developing a XMPP client desktop application, trying to internationalize
it. I have a properties ResourceBundle with a default language, pt_PT
and en_EN parts, created using NetBeans.
I can't figure out how to set the locale to use at startup of the
application - so far it always uses the default locale.
I get the locale from a properties file (the user gets to choose the
locale), and have tried Locale.setDefault(Locale newlocale), but it
doesn't change the strings. Debugging shows me the newLocale parameter
is correct for the case.
I know that java.util.ResourceBundle.getBundle() can have the locale as
the second parameter, *but* NetBeans' GUI editor doesn't seem to let me
add that parameter.
(Somewhat related, but just FYI - the internationalization wizard will
pick up the JFrames' strings, but fails to correctly tell itself to use
the ResourceBundle. Next time you change the GUI, it reverts to
hardcoded strings. Then you have to use the GUI designer to tell the
components to get the string from the ResourceBundle. That screen is not
cooperating when trying to add the locale.)
I think I have exhausted Google on this, it's where I got the
Locale.setDefault() from, but it seems it is less than recommended, at
least in applets.
--
Sabine Dinis Blochberger
Op3racional
www.op3racional.eu
|
|
0
|
|
|
|
Reply
|
Sabine
|
10/8/2007 11:05:21 AM |
|
Sabine Dinis Blochberger wrote:
>How to set locale for application at runtime?
...
>..have tried Locale.setDefault(Locale newlocale), but it
>doesn't change the strings. ...
What ouput does this example produce on that PC?
<sscce>
import java.util.Locale;
/** Typical (if you are an aussie) output
en_AU
es_HN
ja_JP
*/
class LocaleTest {
public static void main(String[] args) {
System.out.println(Locale.getDefault());
Locale[] allLocale = Locale.getAvailableLocales();
Locale.setDefault( allLocale[allLocale.length-1] );
System.out.println(Locale.getDefault());
Locale.setDefault( allLocale[0] );
System.out.println(Locale.getDefault());
}
}
</sscce>
--
Andrew Thompson
http://www.athompson.info/andrew/
Message posted via http://www.javakb.com
|
|
0
|
|
|
|
Reply
|
Andrew
|
10/8/2007 2:58:14 PM
|
|
Andrew Thompson wrote:
> Sabine Dinis Blochberger wrote:
> >How to set locale for application at runtime?
> ...
> >..have tried Locale.setDefault(Locale newlocale), but it
> >doesn't change the strings. ...
>
> What ouput does this example produce on that PC?
>
> <sscce>
snipped
>
Output is
C:\Projekte\localetest>java LocaleTest
pt_PT
es_HN
es_PE
--
Sabine Dinis Blochberger
Op3racional
www.op3racional.eu
|
|
0
|
|
|
|
Reply
|
Sabine
|
10/9/2007 8:29:40 AM
|
|
Sabine Dinis Blochberger wrote:
>> >How to set locale for application at runtime?
...
>Output is
>
>C:\Projekte\localetest>java LocaleTest
>pt_PT
>es_HN
>es_PE
OK.. did that example/output progress your
immediate goal, any?
(BTW - RGB's e.g. was better, in the respect of being
'well *designed*' code. It had some nice tweaks, too.)
--
Andrew Thompson
http://www.athompson.info/andrew/
Message posted via http://www.javakb.com
|
|
0
|
|
|
|
Reply
|
Andrew
|
10/9/2007 11:28:20 AM
|
|
Andrew Thompson wrote:
> Sabine Dinis Blochberger wrote:
> >> >How to set locale for application at runtime?
> ...
> >Output is
> >
> >C:\Projekte\localetest>java LocaleTest
> >pt_PT
> >es_HN
> >es_PE
>
> OK.. did that example/output progress your
> immediate goal, any?
>
> (BTW - RGB's e.g. was better, in the respect of being
> 'well *designed*' code. It had some nice tweaks, too.)
>
No light bulbs went on. Except if that means those are the only locales
I could use, even if I had the appropriate ResourceBundle files?
All my hardcoded strings have been replaced with something like
java.util.ResourceBundle.getBundle(
"eu/op3racional/op3MI/resources/displayStrings")
.getString("ds_error_serverdisconnect")
I get the language code from my settings
appLocale = new Locale(appSettings.languageApp().localeCode());
Locale.setDefault(appLocale);
Then I tried a dialog to confirm if this "works":
JOptionPane.showMessageDialog(new JFrame(),
"Locale: "+Locale.getDefault().getDisplayName(),
"Debug", JOptionPane.INFORMATION_MESSAGE);
If I put the dialog before the Locale.setDefault() line (using
appLocale.getDisplayName()), I get
Locale: ingl�s
putting it after, it says
Locale: English
To me that means it should work as expected, but it doesn't. It always
gets the default ResourceBundle strings. Even when I add the appLocale
parameter to the getBundle(), it doesn't change what I see (trying this
with the dialogs only).
It also doesn't make a difference running it from Netbeans IDE or from
the Windows explorer.
There must be something I missed. Is it because I only use the language
code and no country?
--
Sabine Dinis Blochberger
Op3racional
www.op3racional.eu
|
|
0
|
|
|
|
Reply
|
Sabine
|
10/10/2007 9:42:54 AM
|
|
Sabine Dinis Blochberger wrote:
>
> There must be something I missed. Is it because I only use the language
> code and no country?
>
Seems the missing country code was indeed significant. When I added "GB"
to my "en" language, it started working as expected!
Cheers everyone :)
--
Sabine Dinis Blochberger
Op3racional
www.op3racional.eu
|
|
0
|
|
|
|
Reply
|
Sabine
|
10/15/2007 9:20:15 AM
|
|
|
5 Replies
428 Views
(page loaded in 0.102 seconds)
|