Results 1 to 7 of 7
  1. #1
    DD70 is offline Member
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Default getDisplayLanguage returns "en" not "English"

    I'm having problems displaying details of locales. The following example should, I've been told, display "Language: English" and "Country: England" but it displays "Language: en" and "Country: GB". The default locale is Locale.UK (ie: "en_GB") and the output is the same if I change it to Locale.US or Locale.FRANCE

    The code I'm using is as follows:

    Java Code:
    import java.util.*;
    
    public class TestLocale
    {
    	static public void main(String[] args)
    	{
    		Locale myLocale = new Locale("en", "GB");
    		System.out.println("Language: " + myLocale.getDisplayLanguage());
    		System.out.println("Country: " + myLocale.getDisplayCountry());
    	}
    }
    Any pointers to what's wrong would be much appreciated as this is beginning to drive me up the wall. What have I missed?

    I've copied and pasted other examples from various sources into a text editor, all of them show the language and country in the 2-letter form.

    Cheers.

  2. #2
    coffee is offline Member
    Join Date
    Aug 2009
    Location
    ...
    Posts
    12
    Rep Power
    0

    Smile

    The parameters for a Locale are the 2-char codes for the language and country, which you set in line 7 as "en" and "GB". All getDisplayLanguage() does is return the input you gave for that locale when you created it, the same goes for getDisplayCountry(). If you want it to display the full name of whatever input you gave it, you can do a check beforehand, like:
    Java Code:
    Locale myLocale = new Locale("en","GB");
    String lan = myLocale.getDisplayLanguage();
    String con = my Locale.getDisplayCountry();
    if (lan.equals("en")) {
    	if (con.equals("GB")) {
    		System.out.println("Language: English");
    		System.out.println("Country: England");
    	}
    }
    Also, I was playing around with the locale constructor, and I found that you can just set new Locale(); to ("English","England"); but it will return:
    english
    ENGLAND
    .. and it would probably lead to problems later into coding, so I suggest the first method, but instead of the if-statements, use a switch/case statement and set the default return to "Invalid Language" or "Invalid Country".

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    you can also set up a class that does all translations for you so you can do Translator.getEnglish(String) or something similar or even a HashMap<String, String>. could save you a bunch of if's and comparisons.

  4. #4
    DD70 is offline Member
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Default

    Thanks for the replies.

    The problem is that in all the specs and examples I've read it says that getLanguage returns whatever the language has been set at (eg: "en" or "fr") and getDisplayLanguage should return the full name of the language (eg: "English" or "French").

    In Sun's Java class reference it says that getDisplayLanguage will try to return the full language name in the current or specified locale, if it can't it will then try to return it in English, if it can't do that then it will return the 2-letter language code.

    So, why can't the language "fr" be displayed as "French"? Is something wrong with my installation?

  5. #5
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    That code with Java 1.6.0_12 gives me:

    lan: "English"
    con: "United Kingdom"

    Have you tried the using pre-defined UK locale:
    Java Code:
    Locale myLocale = Locale.UK;
    Last edited by dlorde; 08-11-2009 at 02:49 PM.

  6. #6
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by coffee View Post
    The parameters for a Locale are the 2-char codes for the language and country, which you set in line 7 as "en" and "GB". All getDisplayLanguage() does is return the input you gave for that locale when you created it, the same goes for getDisplayCountry().
    I don't know which version of Java you're using, but in Java 6, those methods are not as you describe:

    getDisplayLanguage()
    Returns a name for the locale's language that is appropriate for display to the user. If possible, the name returned will be localized for the default locale. For example, if the locale is fr_FR and the default locale is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and the default locale is fr_FR, getDisplayLanguage() will return "anglais".

    getDisplayCountry()
    Returns a name for the locale's country that is appropriate for display to the user. If possible, the name returned will be localized for the default locale. For example, if the locale is fr_FR and the default locale is en_US, getDisplayCountry() will return "France"; if the locale is en_US and the default locale is fr_FR, getDisplayLanguage() will return "Etats-Unis"

  7. #7
    DD70 is offline Member
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Default

    Thanks to everyone for your help.

    Turns out my Java installation was a bit screwed up. Re-installing it fixed the problem and now getDisplayLanguage shows "English" rather than "en".

    I knew something was really wrong when a simple Scanner example would compile but threw NoClassDefFoundError when run.

    All sorted, thanks again.

Similar Threads

  1. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 04:03 AM
  2. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  3. Replies: 1
    Last Post: 10-20-2008, 08:35 AM
  4. <core:forEach var="" begin="+<%=j%>+">???
    By freddieMaize in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2008, 02:20 AM
  5. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 04:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •