5

I need to convert Locale to String. How can I do that?

I have Locale loc and String loc1 and need to convert the value from locale to String.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jack Jr
  • 131
  • 1
  • 1
  • 5
  • Did you take a look at http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Locale.html? – nwinkler Mar 02 '12 at 15:41
  • thanx.. just a newbie.. trying to figure sth out... – Jack Jr Mar 02 '12 at 15:47
  • @Jack: In future you might want to (Google|Bing) search for answers first and ask **detailed** question if you are unable to find the answer or feel confused. I understand that Locale class could be very confusing, especially since Java 7 but it really doesn't mean that you should not work on the problem by yourself instead of looking for fast solutions. – Paweł Dyda Mar 02 '12 at 20:10
  • 2
    Since locale conversion is not trivial, I think this question is not so stupid. Especially when you need convert both ways: locale => string and string => locale http://stackoverflow.com/questions/2522248/how-to-get-locale-from-its-string-representation-in-java – Matthias M Nov 06 '14 at 12:12

3 Answers3

22

That really depends on the Java version you use. When you say convert to String, I assume you might want to:

  1. Convert it to Locale identifier, so that you can easily recreate Locale in the future.
  2. Pass it as a String representation to some part of the system.
  3. Get (possibly native) name of language, country or the Locale object.

Others pointed you to a documentation, but I think it is better to walk through several possibilities.

Ad 1. If you need to convert to a Locale identifier, the best method as of Java 7 (that doesn't really exist in previous releases) is toLanguageTag().

That's because you can easily store Locale identifier in a string (including all the options, like script, Unicode extensions, etc.) and recreate Locale object using forLanguageTag(String) without losing information. Simple toString() does not hold this property.
Following code snippet illustrates my point:

Locale locale = Locale.getDefault(Locale.Category.DISPLAY);
String localeId = locale.toLanguageTag();
Locale recreatedLocale = Locale.forLanguageTag(localeId);

Prior to Java 7, you really needed to use toString() and manually handle countries and variants (for Locale consisted of up to three identifiers separated by and underscore, i.e. no_NO_NY.)

Ad 2. If you need to pass Locale to other part of the system (i.e. web service), toString() may and may be not what you are looking for. That's because of what I wrote above - if you need dash as a separator and don't care so much for variants, I would go with getLanguage() and getCountry() methods rather than plain toString():

Locale uiLocale = Locale.getDefault(Locale.Category.DISPLAY);
String localeId = MessageFormat.format("{0}-{1}", 
                                       uiLocale.getLanguage(),
                                       uiLocale.getCountry());

This method should work well with Java versions since version 1.4.

Ad 3. If you want to present language, country or Locale display name, so that user can choose the valid Locale (i.e. in his/her profile), what you probably looking for are getDisplayCountry(Locale), getDisplayLanguage(Locale) and getDisplayName(Locale). You can use them to display names in a target user language (provided that JVM has appropriate translations, which is true for many languages):

Locale uiLocale = Locale.getDefault(Locale.Category.DISPLAY);
String languageName = uiLocale.getDisplayLanguage(uiLocale);
String countryName = uiLocale.getDisplayLanguage(uiLocale);
// usually something like langage (country), i.e. polski (Polska)
String localeName = uiLocale.getDisplayName(uiLocale);
Paweł Dyda
  • 18,366
  • 7
  • 57
  • 79
3

Depending what you want to do with it you could use the toString method of the Locale class.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Puce
  • 37,247
  • 13
  • 80
  • 152
0

It’s very simple. Just use the following code. Consider you are using locale to convert from an ISO current calendar date to the Hijri calendar.

Chronology iso = ISOChronology.getInstanceUTC();
Chronology hijri = IslamicChronology.getInstanceUTC();
LocalDate todayIso = new LocalDate(2016, 1, 20, iso);
LocalDate todayHijri = new LocalDate(todayIso.toDateTimeAtStartOfDay(), hijri);
String currenthijridate = "" + todayHijri;

/* 'currenthijridate' will appear as 1437-04-08 which
   is a string. Now it's simply String = "" + locale
 */
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131