Results 1 to 7 of 7
- 08-10-2009, 07:49 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 5
- Rep Power
- 0
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:
Any pointers to what's wrong would be much appreciated as this is beginning to drive me up the wall. What have I missed?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()); } }
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.
- 08-10-2009, 09:30 PM #2
Member
- Join Date
- Aug 2009
- Location
- ...
- Posts
- 12
- Rep Power
- 0
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:
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: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"); } }
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".
- 08-10-2009, 09:38 PM #3
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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.
- 08-10-2009, 11:57 PM #4
Member
- Join Date
- Jul 2009
- Posts
- 5
- Rep Power
- 0
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?
- 08-11-2009, 01:40 PM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
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 01:49 PM.
- 08-11-2009, 01:48 PM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
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"
- 08-12-2009, 11:22 PM #7
Member
- Join Date
- Jul 2009
- Posts
- 5
- Rep Power
- 0
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
-
Java, Military Format using "/" and "%" Operator!!
By sk8rsam77 in forum New To JavaReplies: 11Last Post: 02-26-2010, 03:03 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
<core:forEach var="" begin="+<%=j%>+">???
By freddieMaize in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 09-27-2008, 01:20 AM -
"Jumble" or "Scramble" Program
By Shadow22202 in forum Java AppletsReplies: 8Last Post: 04-30-2008, 03:42 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks