Set Locale inside the program

A

adherent

Hi@all,

I wrote a program which contains a ProgressMonitor showing the progress
of the job. Since my computer's default locale is Germany, the titel
and button of the ProgressMonitor are in German("Fortschritt" and
"Abbrechen").

If i change my locale in the control panel to US, the titel and button
are in English. My question is how can i set the properties inside my
program?

I have tried:
System.setProperty("user.language", "en");
System.setProperty("user.country", "US");
System.setProperty("user.region", "US");

and Locale.setDefault(new Locale("en", "US")); also

both dont work......

Any hint will be appreciated!

db
 
R

Rhino

Hi@all,

I wrote a program which contains a ProgressMonitor showing the progress
of the job. Since my computer's default locale is Germany, the titel
and button of the ProgressMonitor are in German("Fortschritt" and
"Abbrechen").

If i change my locale in the control panel to US, the titel and button
are in English. My question is how can i set the properties inside my
program?

I have tried:
System.setProperty("user.language", "en");
System.setProperty("user.country", "US");
System.setProperty("user.region", "US");

and Locale.setDefault(new Locale("en", "US")); also

both dont work......

Any hint will be appreciated!

Try:

setLocale(Locale.getDefault());

This version of the setLocale() method is in the Component class and should
set the Locale to whatever it is set to in the Windows control panel.

Or, if you want to set the locale specifically, try:

setLocale(new Locale("en", "US");
 
D

db

Hi Rhino,

I have tried your advice, still doesnt work......
I have tried:
setLocale(Locale.getDefault());
and setLocale(new Locale("en", "US"));
and setLocale(Locale.US); also
none of them works......

I printed out the system properties, it looks like these:

-- listing properties --
java.runtime.name=Java(TM) 2 Runtime Environment, Stand...
sun.boot.library.path=D:\Programme\java1.5\jre\bin
java.vm.version=1.5.0_04-b05
java.vm.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
path.separator=;
java.vm.name=Java HotSpot(TM) Client VM
file.encoding.pkg=sun.io
user.country=US
<--------------------------------------------------------------here
sun.os.patch.level=Service Pack 2
java.vm.specification.name=Java Virtual Machine Specification
java.runtime.version=1.5.0_04-b05
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs=D:\Programme\java1.5\jre\lib\endorsed
os.arch=x86
java.io.tmpdir=c:\temp\
line.separator=

java.vm.specification.vendor=Sun Microsystems Inc.
user.variant=
os.name=Windows XP
sun.jnu.encoding=Cp1252
java.library.path=D:\Programme\java1.5\bin;.;C:\WINDOWS...
java.specification.name=Java Platform API Specification
java.class.version=49.0
sun.management.compiler=HotSpot Client Compiler
os.version=5.1
user.timezone=Europe/Berlin
java.awt.printerjob=sun.awt.windows.WPrinterJob
file.encoding=Cp1252
java.specification.version=1.5
java.vm.specification.version=1.0
sun.arch.data.model=32
java.home=D:\Programme\java1.5\jre
java.specification.vendor=Sun Microsystems Inc.
user.language=en
<-----------------------------------------------------------here
awt.toolkit=sun.awt.windows.WToolkit
java.vm.info=mixed mode, sharing
java.version=1.5.0_04
java.ext.dirs=D:\Programme\java1.5\jre\lib\ext
sun.boot.class.path=D:\Programme\java1.5\jre\lib\rt.jar;D...
java.vendor=Sun Microsystems Inc.
file.separator=\
java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport...
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeLittle
sun.desktop=windows
user.region=US
<--------------------------------------------------------------------------------here
sun.cpu.isalist=pentium_pro+mmx pentium_pro pentium+m...

As u can see, all properties concerning language have been changed. But
the titel of progressMonitor keeps german................

Do u have more suggestions?

thanks

db
 
R

Rhino

db said:
Hi Rhino,

I have tried your advice, still doesnt work......
I have tried:
setLocale(Locale.getDefault());
and setLocale(new Locale("en", "US"));
and setLocale(Locale.US); also
none of them works......

I printed out the system properties, it looks like these:

-- listing properties --
java.runtime.name=Java(TM) 2 Runtime Environment, Stand...
sun.boot.library.path=D:\Programme\java1.5\jre\bin
java.vm.version=1.5.0_04-b05
java.vm.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
path.separator=;
java.vm.name=Java HotSpot(TM) Client VM
file.encoding.pkg=sun.io
user.country=US
<--------------------------------------------------------------here
sun.os.patch.level=Service Pack 2
java.vm.specification.name=Java Virtual Machine Specification
java.runtime.version=1.5.0_04-b05
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs=D:\Programme\java1.5\jre\lib\endorsed
os.arch=x86
java.io.tmpdir=c:\temp\
line.separator=

java.vm.specification.vendor=Sun Microsystems Inc.
user.variant=
os.name=Windows XP
sun.jnu.encoding=Cp1252
java.library.path=D:\Programme\java1.5\bin;.;C:\WINDOWS...
java.specification.name=Java Platform API Specification
java.class.version=49.0
sun.management.compiler=HotSpot Client Compiler
os.version=5.1
user.timezone=Europe/Berlin
java.awt.printerjob=sun.awt.windows.WPrinterJob
file.encoding=Cp1252
java.specification.version=1.5
java.vm.specification.version=1.0
sun.arch.data.model=32
java.home=D:\Programme\java1.5\jre
java.specification.vendor=Sun Microsystems Inc.
user.language=en
<-----------------------------------------------------------here
awt.toolkit=sun.awt.windows.WToolkit
java.vm.info=mixed mode, sharing
java.version=1.5.0_04
java.ext.dirs=D:\Programme\java1.5\jre\lib\ext
sun.boot.class.path=D:\Programme\java1.5\jre\lib\rt.jar;D...
java.vendor=Sun Microsystems Inc.
file.separator=\
java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport...
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeLittle
sun.desktop=windows
user.region=US
<--------------------------------------------------------------------------------here
sun.cpu.isalist=pentium_pro+mmx pentium_pro pentium+m...

As u can see, all properties concerning language have been changed. But
the titel of progressMonitor keeps german................

Do u have more suggestions?

It sounds to me as if the code which displays the progressMonitor is the
problem then. Is it possible that it is not even looking at the locale and
blindly going after the German title in every case?

Perhaps if you post the relevant snippets of code where you set the Locale
and where you create/display the progressMonitor we will be able to see the
cause of the problem.
 
K

karlheinz klingbeil

db schrub am Montag, 8. Mai 2006 15:35 folgendes:
Hi Rhino,

I have tried your advice, still doesnt work......
I have tried:
setLocale(Locale.getDefault());
and setLocale(new Locale("en", "US"));
and setLocale(Locale.US); also
none of them works......

They all work, but they will set set the locale only
for the running app, not the System Locale.
 
J

John O'Conner

db said:
Hi Rhino,

I have tried your advice, still doesnt work......
I have tried:
setLocale(Locale.getDefault());
and setLocale(new Locale("en", "US"));
and setLocale(Locale.US); also
none of them works......

Are you doing this at the very beginning of your application...before
any GUI code gets created?

Just out of curiousity, does this work for you:

java -Duser.region=US -Duser.language=en YourApp

Let me know,
John O'Conner
(e-mail address removed)
http://www.joconner.com/javai18n/
 
D

db

I set the system properties in the very beginning of the constructor of
the main frame
and display the system properties just before the progress monitor is
initialized.
 
D

db

That's my intention. I just want the running appliction use the defined
properties, and no other applications should be influenced
 
D

db

with using java -Duser.region=US -Duser.language=en YourApp

There are many exceptions coming out, ClassNotFoundExeptions...etc
 
R

Rhino

db said:
I set the system properties in the very beginning of the constructor of
the main frame
and display the system properties just before the progress monitor is
initialized.
A one sentence description of what your code does is not particularly
helpful and doesn't address the suggestion I made anyway.

I don't know you and have no idea how much experience you have with
i18n/l10n (internationalization/localization) in particular or Java in
general so forgive me for saying this if you are actually very experienced:
is it possible that you are setting the properties and locale to US English
as you've described but then fail to get the appropriate resource bundle and
text due to an error after you set the locale but before you display the
progress monitor? That would be entirely possible if you were not very
familiar with i18n/l10n.

For example, if you set your locale to US English but the title of the
progress monitor was hard-coded as:

progressMonitor.setNote("Anfangen");

then you are inevitably going to get German text, not English.

The other case where you may get a language that you don't want is if you
don't name the file containing your resource bundle correctly. That
situation is considerably harder to detect than the one where you hard-code
the text and ignore the locale.

I had similar problems to yours a couple of years back and spent a few hours
experimenting; I wrote myself some documentation to describe what I'd found.
This is it:

------------------------------------------------------------------------------------------------------------------
Locales

When your program loads a message ResourceBundle, the bundle loaded depends
on a combination of three factors: what ResourceBundle you requested, what
locale is the default for your system, and what ResourceBundles are defined
to your program. Strangely enough, number, currency, and percent formatters
will format their values in the format of the appropriate locale.

For example, suppose a program named PROG01 has a Message Base name of
PROG01_Msg. The default locale for the Java Virtual Machine running on a
given machine is 'en_CA'. The program has defined the following
ResourceBundles:

'de_DE', i.e. PROG01_Msg_de_DE.properties

'en_CA', i.e. PROG01_Msg_en_CA.properties

'en', i.e. PROG01_Msg_en.properties

'fr_CA', i.e. PROG01_Msg_fr_CA.properties

<default bundle>, i.e. PROG01_Msg.properties

The following table documents what message ResourceBundle will be loaded:

Locale chosen by user
ResourceBundle loaded
Reason

de_DE
de_DE
bundle 'de_DE' exists

de_AT
en_CA
bundle 'de_AT' does not exist and no 'de' bundle exists

de_CH
en_CA
bundle 'de_CH' does not exist and no 'de' bundle exists

fr_CH
en_CA
bundle 'fr_CH' does not exist and no 'fr' bundle exists

fr_FR
en_CA
bundle 'fr_FR' does not exist and no 'fr' bundle exists

fr_CA
fr_CA
bundle 'fr_CA' exists

en_CA
en_CA
bundle 'en_CA' exists

en_US
en
bundle 'en_US' does not exist but 'en' bundle exists

en_UK
en
bundle 'en_UK' does not exist but 'en' bundle exists

ja_JP
en_CA
bundle 'ja_JP' does not exist and no 'ja' bundle exists


Note: Number formatters will give numbers in the locale chosen by the user,
NOT the locale indicated by the above table. For example, if the user chose
the locale fr_CH in the above scenario, numbers will still be formatted in
fr_CH style (with "S. Fr" in front of currency amounts) even though all
messages will come from the en_CA message bundle.

If we modify the above scenario so that the only ResourceBundles defined to
the program are 'de_DE', 'en', 'fr_CA', and the default bundle (i.e. we have
eliminated the en_CA bundle, which corresponds to our default locale) and
then let the users select the same locales, here is what will happen:

Locale chosen by user
ResourceBundle loaded
Reason

de_DE
de_DE
bundle 'de_DE' exists

de_AT
en
bundle 'de_AT' does not exist and no 'de' bundle exists

de_CH
en
bundle 'de_CH' does not exist and no 'de' bundle exists

fr_CH
en
bundle 'fr_CH' does not exist and no 'fr' bundle exists

fr_FR
en
bundle 'fr_FR' does not exist and no 'fr' bundle exists

fr_CA
fr_CA
bundle 'fr_CA' exists

en_CA
en
bundle 'en_CA' does not exist but 'en' bundle exists

en_US
en
bundle 'en_US' does not exist but 'en' bundle exists

en_UK
en
bundle 'en_UK' does not exist but 'en' bundle exists

ja_JP
en
bundle 'ja_JP' does not exist and no 'ja' bundle exists


If we modify the above scenarios so that the only ResourceBundles defined to
the program are 'de_DE', 'fr_CA', and the default bundle (i.e. we have no
English bundles at all) and then let the users select the same locales, here
is what will happen:

Locale chosen by user
ResourceBundle loaded
Reason

de_DE
de_DE
bundle 'de_DE' exists

de_AT
default
bundle 'de_AT' does not exist and no 'de' bundle exists

de_CH
default
bundle 'de_CH' does not exist and no 'de' bundle exists

fr_CH
default
bundle 'fr_CH' does not exist and no 'fr' bundle exists

fr_FR
default
bundle 'fr_FR' does not exist and no 'fr' bundle exists

fr_CA
fr_CA
bundle 'fr_CA' exists

en_CA
default
bundle 'en_CA' does not exist and no 'en' bundle exists

en_US
default
bundle 'en_US' does not exist and no 'en' bundle exists

en_UK
default
bundle 'en_UK' does not exist and no 'en' bundle exists

ja_JP
default
bundle 'ja_JP' does not exist and no 'ja' or 'en' bundle exists


If we modify the above scenarios once again so that the only ResourceBundles
defined to the program are 'de_DE' and 'fr_CA' (i.e. the default bundle no
longer exists), this is the result:

Locale chosen by user
ResourceBundle loaded
Reason

de_DE
de_DE
bundle 'de_DE' exists

de_AT
exception
bundles 'de_AT', 'de', 'en', and default do not exist

de_CH
exception
bundles 'de_CH', 'de', 'en', and default do not exist

fr_CH
exception
bundles 'fr_CH', 'fr', 'en', and default do not exist

fr_FR
exception
bundles 'fr_FR', 'fr', 'en', and default do not exist

fr_CA
fr_CA
bundle 'fr_CA' exists

en_CA
exception
bundles 'en_CA', 'en', and default do not exist

en_US
exception
bundles 'en_US', 'en', and default do not exist

en_UK
exception
bundles 'en_UK', 'en', and default do not exist

ja_JP
exception
bundles 'ja_JP', 'ja', 'en, and default do not exist


This gives rise to an observation: if you need to ensure that the message
text is at least in the correct language, if not necessarily the correct
dialect, you must make sure that you supply a language-only bundle, e.g. if
you want to make sure that you give French-speakers from France and
Switzerland messages in French, you must create an "fr" bundle, even if you
don't create "fr_FR" or "fr_CH' bundles. Of course, French-speakers from
France and Switzerland may be dismayed to see that their messages are not in
precisely the dialect of French they wanted but they will be happier than if
they get English messages.

------------------------------------------------------------------------------------------------------------------

Now, that is probably "too much information" but I wanted to share with you
all the documentation I wrote on the subject in the hope that it would be
helpful.

In brief, you need to understand what ResourceBundle gets used if the locale
specified in your program does not exist. If you've specifed "en_US" in the
program and are still getting German, one of the following has probably
happened:

- you don't have a bundle whose name ends with _en_US, e.g.
PROG01_Msg_en_US.properties (NOTE: the hyphen in front of the "en_US" is
very important! If your base name is PROG01_Msg and your resource bundle is
called PROG01_Msgen_US.properties rather than PROG01_Msg_en_US.properties,
it will not be found and some other bundle, perhaps your German one, will be
used.)
- you do have a bundle whose name ends with _en_US, e.g.
PROG01_Msg_en_US.properties but the text in that file is in German. (Perhaps
you just forgot to translate the text in that file!)
 
D

db

Thanks Rhino for that many words!
although the problem is still there.............

actually it has nothing to do with the resource bundle, i didnt write
the titel text of progressMonitor in it. It is generated by system.
What i do is only using setProgess method of ProgressMonitor to show
the progress, nothing else.

thanks again
 
D

db

i have finally figured it out, it is so simple.........

UIManager.put("OptionPane.cancelButtonText","Cancel");
UIManager.put("ProgressMonitor.progressText","Progress");

thats all T_T
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top