Sagewire Logo

How to set locale for application at runtime?

7 Message(s) by 3 Author(s) originally posted in java help


From: Sabine Dinis Blochberger Date:   Monday, October 08, 2007
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 int ernationalize
it. I have a properties ResourceBundle with a default language , pt_PT
and en_EN parts, created using NetBeans.

I can not 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
does not change the string s. 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 does not 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've to use the GUI designer to tell the
components to get the string from the ResourceBundle. That screen isn't
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


From: Andrew Thompson Date:   Monday, October 08, 2007
wrote in message:
How to set locale for application at runtime?
..
..have tried Locale.setDefault(Locale newlocale), but it
>does not 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


From: Sabine Dinis Blochberger Date:   Tuesday, October 09, 2007
wrote in message:

wrote in message:
>How to set locale for application at runtime?
...
>..have tried Locale.setDefault(Locale newlocale), but it
>does not 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


From: RedGrittyBrick Date:   Tuesday, October 09, 2007
wrote in message:
wrote in message:
wrote in message:

How to set locale for application at runtime?

...

..have tried Locale.setDefault(Locale newlocale), but it
>does not 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


This seemed interesting but I'm easily confused, so I did it this way

------------------------------ 8< --------------------------
package uk.co.infotop.ukbond.test;

import JAVA.text.NumberFormat;
import JAVA.util.Arrays;
import JAVA.util.Comparator;
import JAVA.util.Locale;

class LocaleTest {

public static void main(String[] args) {
Locale[] allLocale = Locale.getAvailableLocales();
Arrays.sort(allLocale, new Comparator<Locale>
() {
public int compare(Locale o1, Locale o2) {
return o1.toString().compareTo(o2.toString());
}
});

printLocaleInfo();

System.out.println("\nSetting default locale to 1st available");
Locale.setDefault( allLocale[0] );
printLocaleInfo();

System.out.println("\nSetting default locale to last available");
Locale.setDefault( allLocale[allLocale.length-1] );
printLocaleInfo();
}

static void printLocaleInfo() {
System.out.println("Default Locale is " + Locale.getDefault());
System.out.println("Number format " +
NumberFormat.getInstance().format(12345.67));
}
}
------------------------------ 8< --------------------------

Default Locale is en_GB
Number format 12,345.67

Setting default locale to 1st available
Default Locale is be
Number format 12 345,67

Setting default locale to last available
Default Locale is uk_UA
Number format 12.345,67


From: Andrew Thompson Date:   Tuesday, October 09, 2007
wrote in message:
>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


From: Sabine Dinis Blochberger Date:   Wednesday, October 10, 2007
wrote in message:

wrote in message:
>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 does not. It always
gets the default ResourceBundle strings. Even when I add the appLocale
parameter to the getBundle(), it does not change what I see (trying this
with the dialogs only).

It also does not 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


From: Sabine Dinis Blochberger Date:   Monday, October 15, 2007
wrote in message:

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



Next Message: JAVA.lang.UnsatisfiedLinkError


Blogs related to How to set locale for application at runtime?

Java SE 6 First Impressions: A Desktop Winner
Action) that you can integrate into your applications. Internationalization—Java SE 6 supports "plugability" for some locale-specific features, such as date formatting, Unicode text normalization, and resource bundles. ...

A common scenario mambo is that someone hitting the sleep button ...
Xorg is ignoring my screen resolutions I’m trying to get it to show 1280 x 720 but it refuses to. run “sudo dpkg-reconfigure xserver-xorg”. what is that update –alternatives command to choose your java runtime. update-java-alternatives ...

12.2.1 The Locale Class The java.util.Locale class is (Geocities ...
A particular instance of the Locale represents a unique language and region. When a class in the Java library modifies its functionality during runtime based on a Locale object, it’s said to be locale-sensitive. ...

Struts Interview Questions
All the strings/labels in the application using Struts will get it labels from this file only depending on locale. This is an i18n implementation of struts3. This validation.xml configuration file defines which validation routines that ...

Book II Chapter 2 Commanding the Shell Discovering (My web site)
I get the following output: k3b - A sophisticated KDE cd burning application k3b-i18n - Internationalized (i18n) files for k3b k3blibs - The KDE cd burning application library - runtime files k3blibs-dev - The KDE cd burning application ...

Java Faqs
If an application has six modules, each module behaves like a subscriber to a named topic on the server. Question What is the difference between the Mailing and Messaging? (JMS) Answer Java Mailing is the set of APIs that primarily ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional