Locale.getDefault()

hi
i am developing an application which can be viewed in any country.
Does Locale l=Locale.getDefault(); helps to get the Country and language of the person who is using the application.
i have separate properties file for each country.
Locale l=Locale.getDefault();
String ResourceBundleFileName="Message";
ResourceBundle messagesPOC = ResourceBundle.getBundle(ResourceBundleFileName,l);
//this function is written by me , to get all the values in the properties file
String []ValueArray=DefaultLocale.GetLocale(messagesPOC);I have properties files like Message_en_US.properties , Message_fr_FR.properties,Message_en_GB.properties
if a person in France accesses the application , he should get values from Message_fr_FR.properties
if a person in UK accesses the application , he should ge values from Message_en_GB.properties.
in the same way candian from Message_en_CA.properties , U.S. from Message_en_US.properties etc..
Thanks in Advance
null
Edited by: SathyaK on Jan 16, 2008 1:30 PM

SathyaK wrote:
Does Locale l=Locale.getDefault(); helps to get the Country and language of the person who is using the application.Yes, that is what is is for. This is what it returns here.. "en_AU".
Note that to provide 'English' for me, it would be necessary to have either a file ending in simply "en", or one specific to "en_AU".

Similar Messages

  • JOptionPane doesn't talke into account Locale.getDefault()

    Hello,
    I'm working with J2SDK and RE 1.3.1 for Linux. I have noticed that, when using JOptionPane, the text labels for the buttons placed automatically by JOptionPanel, when invoking showXXX, doesn't take into account the default Locale for that instance of the JVM.
    If I have the environment variables for linux:
    export LC_ALL=en_GB
    export LANG=en_GB
    And in my java application I do the following:
    Locale.setDefault(new Locale("en", "GB"));
    The buttons of my JOptionPanes are still rendered in spanish, and not in English...
    Maybe is this a bug? Or, more probably, am I doing anything wrong?
    Thanks in advance and best regards,
    Miguel Angel

    I have the same problems when I display Chinese Character

  • Locale.getDefault() for other language OS

    I have a Swedish operating system and would like to get Swedish as the default locale. However, looking into the Locale API there seems to be no constant for Swedish. It seems to be limited to 10 or 11 languages in total. How can I detect OSs that is based on other languages than those 10 or 11 mentioned in Locale?

    Yes, spell check in OS X is totally separate from the choice of input source.  It can also be set in system prefs/language & text/text/spelling, but that is even less convenient.  I don't know of any easier way than what you have already discovered.
    However there is at least one app, Nisus Writer, where the app preferences can be set so as to link the keyboard, spellcheck, and font for any given language, with the whole set choosable via a single keyboard shortcut.

  • Maintaining default locale in multi-lingual application

    Hello,
    I have a multi-lingual application where the language can be changed at runtime.
    To make the following code work properly, the default locale has to be set each
    time the language changes. Why?
    Since I need to check sometimes the platforms original locale (I do this with
    "Locale.getDefault()"), I am looking for a way not to change the default locale,
    but still to change the resourceBundle.
    In the API I read under "ResourceBundle, Cache Management" that the bundles are cached. Is this the reason why redefining the resourceBundle has no effect? And if yes, how can it be avoided?
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    public class Y extends JFrame {
      boolean toggle;
      Locale currentLocale;
      JButton b;
      ResourceBundle languageBundle;
      String country, userLanguage;
      public Y() {
        setSize(300,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container cp= getContentPane();
        b= new JButton();
        b.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
         toggle= !toggle;
         if (toggle)
           country= "DE";
         else
           country= "GB";
         setUserLanguage(country);
        cp.add(b, BorderLayout.SOUTH);
        setUserLanguage("GB");
        setVisible(true);
      public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
         new Y();
      void setUserLanguage(String country) {
        if (country.equals("DE"))
          userLanguage= "de";
        else
          userLanguage= "en";
        currentLocale = new Locale(userLanguage, country);
    //    System.out.println(currentLocale); // The locale changes ...
    //    Locale.setDefault(currentLocale); // Remove comment slashes and it works.
    //    languageBundle.clearCache(); // No effect.
        languageBundle = ResourceBundle.getBundle("MyBundle",currentLocale);
        System.out.println(languageBundle); // ... but the resourceBundle does not change.
        b.setText(languageBundle.getString("ButtonText"));
    The resource bundle files:
    MyBundle.properties
    ButtonText= Just a button
    MyBundle_de_DE.properties
    ButtonText= Nur ein KnopfEdited by: Joerg22 on 18.08.2008 13:26

    What's your default locale? If your default locale is de_DE, that's the expected behavior. The reason for it is the fallback mechanism searches default locale's bundle before falls back to the base bundle, i.e., in case of searching en_GB bundle, the search order is:
    en_GB
    en
    de_DE
    de
    (base)
    So, it will choose MyBundles_de_DE.
    If you do not want this default locale fallback, you can specify ResourceBundle.Control instance, which is returned from ResourceBundle.Control.getNoFallbackControl() method, in your getBundle() call. Or if you do not use JDK6, you could copy the base bundle to MyBundles_en, which is ugly but should work.
    Naoto
    Edited by: naoto on Aug 18, 2008 1:05 PM

  • Java.util.Locale not thread-safe !

    In multithreading programming, we know that double-checking idiom is broken. But lots of code, even in sun java core libraries, are written using this idiom, like the class "java.util.Locale".
    I have submitted this bug report just now,
    but I wanted to have your opinion about this.
    Don't you think a complete review of the source code of the core libraries is necessary ?
    java.util.Locale seems not to be thread safe, as I look at the source code.
    The static method getDefault() is not synchronized.
    The code is as follows:
    public static Locale getDefault() {
    // do not synchronize this method - see 4071298
    // it's OK if more than one default locale happens to be created
    if (defaultLocale == null) {
    // ... do something ...
    defaultLocale = new Locale(language, country, variant);
    return defaultLocale;
    This method seems to have been synchronized in the past, but the bug report 4071298 removed the "synchronized" modifier.
    The problem is that for multiprocessor machines, each processor having its own cache, the data in these caches are never synchronized with the main memory.
    The lack of a memory barrier, that is provided normally by the "synchronized" modifier, can make a thread read an incompletely initialized Locale instance referenced by the static private variable "defaultlocale".
    This problem is well explained in http://www.javaworld.com/javaworld/jw-02-2001/jw-0209-double.html and other documents about multithreading.
    I think this method must just be synchronized again.

    Shankar, I understand that this is something books and articles about multithreading don't talk much about, because for marketing reasons, multithreading is supposed to be very simple.
    It absolutely not the case.
    Multithreading IS a most difficult topic.
    First, you must be aware that each processor has its own high-speed cache memory, much faster than the main memory.
    This cache is made of a mixture of registers and L1/L2/L3 caches.
    Suppose we have a program with a shared variable "public static int a = 0;".
    On a multiprocessor system, suppose that a thread TA running on processor P1 assign a value to this variable "a=33;".
    The write is done to the cache of P1, but not in the main memory.
    Now, a second thread TB running on processor P2 reads this variable with "System.out.prinln(a);".
    The value of "a" is retrieved from main memory, and is 0 !
    The value 33 is in the cache of P1, not in main memory where its value is still 0, because the cache of P1 has not been flushed.
    When you are using BufferedOutputStream, you use the "flush()" method to flush the buffer, and the "synch()" method to commit data to disk.
    With memory, it is the same thing.
    The java "synchronized" keyword is not only a streetlight to regulate traffic, it is also a "memory barrier".
    The opening brace "{" of a synchronized block writes the data of the processor cache into the main memory.
    Then, the cache is emptied, so that stale values of other data don't remain here.
    Inside the "synchronized" block, the thread must thus retrieve fresh values from main memory.
    At the closing brace "}", data in the processor cache is written to main memory.
    The word "synchronized" has the same meaning as the "sync()" method of FileDescriptor class, which writes data physically to disk.
    You see, it is really a cache communication problem, and the synchronized blocks allows us to devise a kind of data transfer protocol between main memory and the multiple processor local caches.
    The hardware does not do this memory reconciliation for you. You must do it yourself using "synchronized" block.
    Besides, inside a synchronized block, the processor ( or compiler ) feels free to write data in any order it feels most appropriate.
    It can thus reorder assignments and instruction.
    It is like the elevator algorithm used when you store data into a hard disk.
    Writes are reordered so that they can be retrieved more efficiently by one sweep or the magnetic head.
    This reordering, as well as the arbitrary moment the processor decides to reconciliate parts of its cache to main memory ( if you don't use synchronized ) are the source of the problem.
    A thread TB on processor P2 can retrieve a non-null pointer, and retrieve this object from main memory, where it is not yet initialized.
    It has been initialized in the cache of P1 by TA, though, but TB doen't see it.
    To summarize, use "synchronized" every time you access to shared variables.
    There is no other way to be safe.
    You get the problem, now ?
    ( Note that this problem has strictly nothing to do with the atomicity issue, but most people tend to mix the two topics...
    Besides, as each access to a shared variable must be done inside a synchronized block, the issue of atomicity is not important at all.
    Why would you care about atomicity if you can get a stale value ?
    The only case where atomicity is important is when multiple threads access a single shared variable not in synchronized block. In this case, the variable must be declared volatile, which in theory synchronizes main and cache memory, and make even long and double atomic, but as it is broken in lots of implementation, ... )

  • Locale settings in Debian linux

    I have J2SDK 1.4.1 with NetBeans on Debian 3.0 (woody). Althoug system is set to cs_CZ (Czech) locale, Java behaves as in English environment (Locale.getDefault()...). The main problem concerns displaying and reading czech strings - neither czech diacritics are showed, nor can be typed. There are similar problems both in textual console and in Swing application.
    Is Java localization supported for Debian? Is there a documentation, how to set it up?
    Thank you!
    Regards
    David Zejda

    It seems that I have solved the problem with the character set. It's not really something that I understand but it works: I have changed the startup script that is used in /etc/init.d/rc5 to start the server. I changed the shell for the script. It was not working when I used #/bin/bash or #/bin/sh but it works when I use #/bin/csh.

  • Locale test program exception

    Hi,
    I wrote a simple program to help me learn about using locales and currency in java. The program worked file for the default locale, but gives an exception when I pass it a US locale. I expect this is something very simple that I've done wrong but it escapes me for now. I'm hoping someone here can help.
    Here is the code and output:
    import java.util.*;
    import java.text.*;
    public class LocaleTest {
    private static void showLocaleInfo(Locale theLocale) {
    System.out.println("Country: "+ theLocale.getCountry()+" "+theLocale.getDisplayCountry());
    Double currencyValue = new Double(9876543.61);
    String currencyOut;
    NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(theLocale);
    currencyOut = currencyFormatter.format(currencyValue);
    System.out.println(currencyOut + " " +      theLocale.toString());
    Currency currencyInstance = Currency.getInstance(theLocale);
    int intDP = currencyInstance.getDefaultFractionDigits();
    System.out.println(intDP);
    public static void main(String[] args) {
    Locale usLocale = new Locale("US","en");
    Locale defLocale = Locale.getDefault();
    showLocaleInfo(defLocale);
    showLocaleInfo(usLocale);
    Country: JP 日本
    ¥9,876,544 ja_JP
    0
    Country: EN EN
    ?9,876,544 us_EN
    java.lang.IllegalArgumentException
    at java.util.Currency.getInstance(Currency.java:257)
    at LocaleTest.showLocaleInfo(LocaleTest.java:26)
    at LocaleTest.main(LocaleTest.java:40)
    Exception in thread "main"
    The problem would appear to be in the way the US locale is initialized... maybe. Help please!
    Iain

    Hi,
    I've read it, and have been recommending that document to everyone anyway. It's an interesting read and essential information for people on the internationalization trail like myself.
    One thing I wasn't sure about after reading your article, is which you consider preferable, directly referencing a font name (like the arial unicode one) in your programs, or using logical font names. The alternatives are well presented and explained, but I was left wondering, if you were pinned down and had to make one recommendation, what would it be?
    As far as I can tell, the best thing would be to store font names in resource bundles (the name can be logical or physical) and never refer to a font name directly in your application code.
    I don't know if there is any potential problem with this.
    regards
    iain

  • JRE 8 and Locale.setDefault(), java 8 update 5, Windows 7

    Hello,
    I have simple question that i can't resolve.
    I am working on JavaFX application in which I am using DatePicker. This part is not important.
    My date picker is formating date based on my Windows 7 OS Regional and Language settings(Formats).
    This part is now relevant:
    In order to try to make my app Regional independant, and show date format always as US, I only once used this line of code:
    Locale.setDefault(Locale.ENGLISH);
    Here is problem:
    Now when I remove this line of code "Locale.setDefault(Locale.ENGLISH)" my application is no longer able to get Regional format from my Windows 7 OS Regional and Language settings(Formats). Locale informations are permanently lost in JVM. Things are even worse, because my JDBC connection is not working now, and it reports:
    java.sql.SQLException: Locale not recognized
      at oracle.jdbc.driver.T4CTTIoauthenticate.setSessionFields(T4CTTIoauthenticate.java:1006)...
    How can I revert to old JVM behaviour, and get Locale information from my Windows 7 OS Regional and Language settings(Formats) automatically? Any help?
    The application now is only working when there is Locale.setDefault(Locale.ENGLISH); line in code.

    I am constantly using JRE8. Now
    Locale.getDefault(Locale.Category.DISPLAY) returns en_US which is OK
    and
    Locale.getDefault(Locale.Category.FORMAT)? returns bs_BA_#Latin witch is something I set wrongly with my Locale.setDefault.
    I think that this bs_BA_#Latin. How can I repair this and make JRE8 read Locale settings from regional settings?
    Many thx

  • No Japanese locale on a machine with Japanese OS

    I am running a small test application on a Windows box with a Japanese version of XP running on it. The default locale correctly displays as Japanese (Locale.getDefault()). However, when I say Locale.getAvailableLocales() and display the results in a combo box, Japanese is not one of the available locales.
    In addition, when I try to format a date, with something like:
    Calendar c.setTime(new Date(System.currentTimeMillis()));
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.getDefault());
    String str += df.format(c.getTime());
    the date is still in English.
    How is it possible to be running on a Japanese OS and not have Japanese be one of the available locales in Java?
    Thanks very much.

    From Sun's locale support page (http://java.sun.com/j2se/1.5.0/docs/guide/intl/locale.doc.html):
    "Sun's J2SE Runtime Environment 5.0 for Windows may be installed as a complete international version or as a European languages version. The JRE installer by default installs a European languages version if it recognizes that the host operating system only supports European languages. If the installer recognizes that any other language is needed, or if the user requests support for non-European languages in a customized installation, a complete international version is installed. "
    and
    "The European languages version of the J2SE Runtime Environment 5.0 supports all locales for the following languages: Albanian, Belorussian, Bulgarian, Catalan, Croatian, Czech, Danish, Dutch, English, Estonian, Finnish, French, German, Greek, Hungarian, Icelandic, Italian, Latvian, Lithuanian, Macedonian, Norwegian, Polish, Portuguese, Romanian, Russian, Slovak, Slovenian, Spanish, Swedish, Turkish, Ukrainian."
    You might have the European languages version if you didn't use a default method of installing it on that Japanese system (which should have given you the full international version).

  • Dummy question on date format and locale

    Hi everybody,
    Sorry for asking such a stupid question. I've always thought that the format of the generated date
    System.out.println("Default Locale: " + Locale.getDefault());
    System.out.println("Default Locale Country: " + Locale.getDefault().getCountry());
    System.out.println("Default Locale Language: " + Locale.getDefault().getLanguage());
    SimpleDateFormat simpledateformat = new SimpleDateFormat("ddMMMyy");
    String s = simpledateformat.format(calendar.getTime());
    System.out.println("Generated date: " + s);On one machine I have the following output:
    en
    en
    01FEB08On a second machine I have
    en
    en
    01FEV08I don't understant why that difference. I thought only the locale and language could change the way dates are formatted.
    Can s/o help me and explain me what elements are used to format dates or where to search?
    Thx for your help
    Marc

    @Martin
    Thx for the tip.
    That's what I am already doing but I'd like to understand what creates the difference between my two instances.
    Edited by: elmarc06 on Jan 29, 2009 8:11 AM

  • ResourceBundle and Locale ?

    Hi All,
    Anybody please tell me what is the use of ResourceBundle and Locale ?
    Can I use it in my util/Servlet class ?
    What is the advantage of using it ?
    Please reply soon
    Thanks
    Harish Pathak

    ResourceBundle is a utility class provided by the java.util package which internally uses ClassLoader and loads files which .properties extension.
    Incase you wish to make your application Region or Language specific. Then you can use the Locale concept of ResourceBundle and have multiple property files for each region.
    You can set the Locale for the ResourceBundle like en_US where region is US and language is en (english) and it will load the appropriate property file.
    --- For your information
    Gets a resource bundle using the specified base name, locale, and class loader.
    Conceptually, getBundle uses the following strategy for locating and instantiating resource bundles:
    getBundle uses the base name, the specified locale, and the default locale (obtained from Locale.getDefault) to generate a sequence of candidate bundle names. If the specified locale's language, country, and variant are all empty strings, then the base name is the only candidate bundle name. Otherwise, the following sequence is generated from the attribute values of the specified locale (language1, country1, and variant1) and of the default locale (language2, country2, and variant2):
    baseName + "_" + language1 + "_" + country1 + "_" + variant1
    baseName + "_" + language1 + "_" + country1
    baseName + "_" + language1
    baseName + "_" + language2 + "_" + country2 + "_" + variant2
    baseName + "_" + language2 + "_" + country2
    baseName + "_" + language2
    baseName
    Candidate bundle names where the final component is an empty string are omitted. For example, if country1 is an empty string, the second candidate bundle name is omitted.
    getBundle then iterates over the candidate bundle names to find the first one for which it can instantiate an actual resource bundle. For each candidate bundle name, it attempts to create a resource bundle:
    First, it attempts to load a class using the candidate bundle name. If such a class can be found and loaded using the specified class loader, is assignment compatible with ResourceBundle, is accessible from ResourceBundle, and can be instantiated, getBundle creates a new instance of this class and uses it as the result resource bundle.
    Otherwise, getBundle attempts to locate a property resource file. It generates a path name from the candidate bundle name by replacing all "." characters with "/" and appending the string ".properties". It attempts to find a "resource" with this name using ClassLoader.getResource. (Note that a "resource" in the sense of getResource has nothing to do with the contents of a resource bundle, it is just a container of data, such as a file.) If it finds a "resource", it attempts to create a new PropertyResourceBundle instance from its contents. If successful, this instance becomes the result resource bundle.
    If no result resource bundle has been found, a MissingResourceException is thrown.

  • The JUNavigationBar don't works whit the locale settings (9.0.2)

    I have a problem with JUNavigationBar, it ignores the locale settings. The application module configuration is set with "es" & "ES" locale properties. I check the locale when the app runs with:
    Locale.getDefault();
    app.getApplicationModule().getSession().getLocale(); app.getApplicationModule().getSession().getLocaleContext().getLocale();
    and they are right ("es", "ES").
    How can I get that JUNavigationBar uses the UIMessageBundle_es_ES which is deployed with the jar file?
    Thanks!

    I Performed the following local settings.
    1. Before first line at the main method :
    Locale.setDefault(new Locale("it", "IT"));
    2. Locale properties in the Application module configuration
    JUApplication app = JUMetaObjectManager.createApplicationObject("Project1.DeptEmpServices", null, new JUEnvInfoProvider());
    app.setLocale(new Locale("it","IT"));
    JNavigationBar , it ignores again the locale settings.
    JNavigationBar doesn't use the UIMessageBundle_it.
    I checked the locale settings when the app runs with:
    app.getApplicationModule().getSession().getLocale() --> it_IT
    app.getApplicationModule().getSession().getLocaleContext().getLocale() --> it_IT

  • JMenu and internationalisation (Locale ?)

    Hello,
    I have some menus that I wish to change based on the selected language
    of the user. I have some text that is in IN18 Latin that should render
    in my JMenu. The problem is that some accented letters of the alphabet are
    not rendering properly. For example "Régions" renders in the menus as
    "Régions".
    My application is a web applet, so the end user has no access to the
    start up of java at all, except for their system locale settings.
    Should I be encoding my translations of menus as HTML(ish) Unicode strings?
    Do I have the wrong Locale setup?
    I did try the following:
    Locale locale = Locale.getDefault();
    Locale.setDefault(Locale.FRANCE);This did nothing to resolve the problem.
    Is there a tutorial that shows the proper way of using international character sets
    transparently without having to start the java application in a special way from the
    command line?
    Thanks for any pointers.
    Peter
    Edited by: pgw on Nov 3, 2008 1:06 PM

    Well, I'm reading data from the database via:
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));Which I just changed to:
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF8"));Now all is well.
    I had tried to convert the actual string, but
    String.getBytes(original, "UTF-8") appeared to be converting the ESC character to UTF ... (??)
    Anyway it didn't work.
    Specifying the format of the InputStreamReader appears to be the way to go.
    Thanks for the leads.

  • How to real the locale of the Server

    Hi,
    I need to read the locale of the server in java program. how to read the locale of the server.
    Kindly help me.

    If your code is running on the server, then:
    Locale default = Locale.getDefault();If it isn't, then you'll have to tell us about the protocol you have put in place whereby clients can request that information from the server.
    You didn't do that? Then your clients can't get the default locale from the server.
    Or you could tell us why you need to do that. Generally the client doesn't care about the server's locale. If you think it does, maybe that's a proposed solution to some other problem.

  • Set Locale with a backng bean

    I have this issue: i'm trying to set the default Locale for the web page with a backing bean wich is boud to a combobox on the page for selecting the language. So in the bean on the value change of the combobox i do this:
    Locale locale=Locale.getDefault();
    locale.setLocale(language);
    When i tried it the first time everythink worked fine, the problems comes when to users access at the same time at the web application: when i user sets the language, also the locale of the other user is automatically changed. The backing bean which is managing the language change is defned as a session bean. Do someone know why?

    The default locale is set JVM-wide and is not specific to a user's session. JSF will use the Locale specified by ExternalContext.getRequestLocale() which is determined by the HttpServletRequest object, which uses the Accept-Language header from the request.

Maybe you are looking for