3

I was wondering if anyone knows how to get formatted or styled codes from the Locale class. What i would like to see is "en_US" for English for US. For example, if i detect a language from Firefox it comes back as "en-us,en;q=0.5". Later when I want to output it (using getLanguage() or toString()) it comes back as "en-us,en;q=0.5" instead of "en_US". I tried various functions, but they all seem to return the string that was used to generate the Locale in the first place.

These values go into various UI elements and config files, so it would be nicer to have them looking the same regardless of who or how it was generated and also comparison is not easy when they are converted to strings (have to be).

Cheers and Thanks!

NOTE: I am asking this because we have legacy code that does alot (and i really mean most) of things when data is in string/xml format, so formatting is crucial.

SpaceBear
  • 832
  • 3
  • 8
  • 22

2 Answers2

2

Locale#toString() returns what you want.

Here's the thing: the languages you detect from Firefox use the format specified in RFC 2616, which is the HTTP specification. This is a different format from anything you'll see in a Java Locale because that format is very much HTTP-specific.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • Nope, returns "en-us,en;q=0.5" :( – SpaceBear Mar 26 '12 at 17:52
  • Then you're not working with a `java.util.Locale` instance. What is the FQCN of this class? – Matt Ball Mar 26 '12 at 17:52
  • java.util.Locale.Locale :(. toString() returns whatever string was used to initialize the Locale class from what i can tell. – SpaceBear Mar 26 '12 at 17:55
  • Where does the `Locale` instance come from? If you're doing something like `new Locale("en-us,en;q=0.5")` that's simply not a valid language code, [which is what that constructor expects](http://docs.oracle.com/javase/7/docs/api/java/util/Locale.html#Locale%28java.lang.String%29). – Matt Ball Mar 26 '12 at 17:58
  • That's exactly what I am doing :(. I just looked up Accepted-Language in the http header and used that string to initialize the class. What would be the proper way to do this? – SpaceBear Mar 26 '12 at 18:00
  • Well, like I said, that's not a valid language code! If you're using servlets: http://stackoverflow.com/questions/6824157/parse-accept-language-header-in-java – Matt Ball Mar 26 '12 at 18:01
0

The problem is the meaning on what you are trying to print.

en-us,en;q=0.5

Means that the preference quality of the english language is 50% and it can be identified as en-us or en.

In the case you get in an http request

Accept-Language: da, en-gb;q=0.8, en;q=0.7

it means: "I prefer Danish, but will accept British English and other types of English"

so first you will have to decode your http request according to the specification and then get the Locales

Oscar Castiblanco
  • 1,626
  • 14
  • 31
  • Well, there is function on HttpServletRequest called getLocale(). It does it all for you! :) I was making things too complicated. I do that ;). – SpaceBear Mar 26 '12 at 18:12