Java Tutorial/I18N/Locales
Содержание
- 1 Calculate the postfixes along the search path from the base bundle to the bundle specified by baseName and locale.
- 2 Calculate the postfix to append to a filename to load the correct single filename for that Locale.
- 3 Change the default locale is to call Locale.setDefault():
- 4 Constant Locale Usage
- 5 Converts a String to a Locale
- 6 format date for a Locale
- 7 Get a list of country names
- 8 Get Display Country for default locale
- 9 Get Display Name for default locale
- 10 Get Display Variant for default locale
- 11 Get ISO3 Language for default locale
- 12 Get localized name suitable for display to the user
- 13 Get the 2-letter country code; may be equal to ""
- 14 Japanese ImperialCalendar
- 15 Listing All Available Locales
- 16 Load Resource Bundle based on Locale Name
- 17 Locales
- 18 Locale Sensitive Services SPI
- 19 Map using Locale objects as keys
- 20 New locales in Java 6
- 21 Returns the parent locale of a given locale.
- 22 Set a default Locale
- 23 Set language and country code on the command line
- 24 Set only language code on the command line
- 25 Set the default locale to custom locale
- 26 Set the default locale to pre-defined locale
- 27 Setting the Default Locale on the command line
- 28 Use Locale constant
Calculate the postfixes along the search path from the base bundle to the bundle specified by baseName and locale.
/*
* $Id: LocaleUtil.java 667964 2008-06-15 15:00:54Z apetrelli $
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/**
* Utilities for locale manipulation.
*
* @version $Rev: 667964 $ $Date: 2008-06-15 17:00:54 +0200 (Sun, 15 Jun 2008) $
* @since 2.1.0
*/
public class Main {
/**
* The "null" Locale, i.e. a Locale that points to no real locale.
*
* @since 2.1.0
*/
public static final Locale NULL_LOCALE = new Locale("");
/**
* Calculate the postfixes along the search path from the base bundle to the
* bundle specified by baseName and locale. Method copied from
* java.util.ResourceBundle
*
* @param locale The locale.
* @return a list of postfixes to add to filenames.
* @since 2.1.0
*/
public static List<String> calculatePostfixes(Locale locale) {
final List<String> result = new ArrayList<String>();
// The default configuration file must be loaded to allow correct
// definition inheritance.
result.add("");
if (locale == null) {
return result;
}
final String language = locale.getLanguage();
final int languageLength = language.length();
final String country = locale.getCountry();
final int countryLength = country.length();
final String variant = locale.getVariant();
final int variantLength = variant.length();
if (languageLength + countryLength + variantLength == 0) {
// The locale is "", "", "".
return result;
}
final StringBuffer temp = new StringBuffer();
temp.append("_");
temp.append(language);
if (languageLength > 0) {
result.add(temp.toString());
}
if (countryLength + variantLength == 0) {
return result;
}
temp.append("_");
temp.append(country);
if (countryLength > 0) {
result.add(temp.toString());
}
if (variantLength == 0) {
return result;
} else {
temp.append("_");
temp.append(variant);
result.add(temp.toString());
return result;
}
}
}
Calculate the postfix to append to a filename to load the correct single filename for that Locale.
/*
* $Id: LocaleUtil.java 667964 2008-06-15 15:00:54Z apetrelli $
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/**
* Utilities for locale manipulation.
*
* @version $Rev: 667964 $ $Date: 2008-06-15 17:00:54 +0200 (Sun, 15 Jun 2008) $
* @since 2.1.0
*/
public class Main {
/**
* The "null" Locale, i.e. a Locale that points to no real locale.
*
* @since 2.1.0
*/
public static final Locale NULL_LOCALE = new Locale("");
/**
* Calculate the postfix to append to a filename to load the correct single
* filename for that Locale.
*
* @param locale The locale.
* @return The postfix to append to the filename.
* @since 2.1.0
*/
public static String calculatePostfix(Locale locale) {
if (locale == null) {
return "";
}
StringBuilder builder = new StringBuilder();
String language = locale.getLanguage();
String country = locale.getCountry();
String variant = locale.getVariant();
if (!"".equals(language)) {
builder.append("_");
builder.append(language);
if (!"".equals(country)) {
builder.append("_");
builder.append(country);
if (!"".equals(variant)) {
builder.append("_");
builder.append(variant);
}
}
}
return builder.toString();
}
}
Change the default locale is to call Locale.setDefault():
import java.util.Locale;
public class Main {
public static void main(String[] argv) throws Exception {
// Get default locale
Locale locale = Locale.getDefault();
}
}
Constant Locale Usage
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
public class ConstantLocaleUsage {
public static void main(String[] argv) {
NumberFormat numberFormat = NumberFormat.getInstance();
numberFormat.setParseIntegerOnly(false);
double usersNumber;
if (argv.length == 1)
try {
usersNumber = numberFormat.parse(argv[0]).doubleValue();
} catch (ParseException e) {
usersNumber = 197912.29;
}
else
usersNumber = 1976.0826;
numberFormat = NumberFormat.getNumberInstance(Locale.US);
System.out.println("User"s number (US): "
+ numberFormat.format(usersNumber));
numberFormat = NumberFormat.getNumberInstance(Locale.GERMANY);
System.out.println("User"s number (GERMANY): "
+ numberFormat.format(usersNumber));
numberFormat = NumberFormat.getNumberInstance();
System.out.println("User"s number (DEFAULT LOCALE): "
+ numberFormat.format(usersNumber));
}
}
Converts a String to a Locale
// Copyright 2008 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import java.util.Locale;
/**
* Contains code borrowed from .
*/
public class LocaleUtils
{
/**
* Converts a String to a Locale. <p/> This method takes the string format of a locale and creates the
* locale object from it. <p/>
* <pre>
* LocaleUtils.toLocale("en") = new Locale("en", "")
* LocaleUtils.toLocale("en_GB") = new Locale("en", "GB")
* LocaleUtils.toLocale("en_GB_xxx") = new Locale("en", "GB", "xxx") (#)
* </pre>
* <p/> (#) The behaviour of the JDK variant constructor changed between JDK1.3 and JDK1.4. In JDK1.3, the
* constructor upper cases the variant, in JDK1.4, it doesn"t. Thus, the result from getVariant() may vary depending
* on your JDK. <p/> This method validates the input strictly. The language code must be lowercase. The
* country code must be uppercase. The separator must be an underscore. The length must be correct.
*
* @param input the locale String to convert, null returns null
* @return a Locale, null if null input
* @throws IllegalArgumentException if the string is an invalid format
*/
public static Locale toLocale(String input)
{
if (input == null)
return null;
int len = input.length();
if (len != 2 && len != 5 && len < 7)
fail(input);
char ch0 = input.charAt(0);
char ch1 = input.charAt(1);
if (ch0 < "a" || ch0 > "z" || ch1 < "a" || ch1 > "z")
fail(input);
if (len == 2)
return new Locale(input, "");
if (input.charAt(2) != "_")
fail(input);
char ch3 = input.charAt(3);
if (ch3 == "_")
return new Locale(input.substring(0, 2), "", input.substring(4));
char ch4 = input.charAt(4);
if (ch3 < "A" || ch3 > "Z" || ch4 < "A" || ch4 > "Z")
fail(input);
if (len == 5)
return new Locale(input.substring(0, 2), input.substring(3, 5));
if (input.charAt(5) != "_")
fail(input);
return new Locale(input.substring(0, 2), input.substring(3, 5), input.substring(6));
}
private static void fail(String input)
{
throw new IllegalArgumentException(String.format("Unable to convert "%s" to a Locale instance.", input));
}
}
format date for a Locale
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class FormatDateLocale {
public static void main(String[] args) {
Locale[] locales = new Locale[] { Locale.JAPAN, Locale.CHINA, Locale.KOREA, Locale.TAIWAN,
Locale.ITALY, Locale.FRANCE, Locale.GERMAN };
Date today = new Date();
for (Locale locale : locales) {
System.out.println("Date format in "
+ locale.getDisplayName()
+ " = "
+ SimpleDateFormat.getDateInstance(SimpleDateFormat.LONG, locale).format(today)
.toUpperCase());
}
}
}
Get a list of country names
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.ruparator;
import java.util.List;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
List<Country> countries = new ArrayList<Country>();
Locale[] locales = Locale.getAvailableLocales();
for (Locale locale : locales) {
String iso = locale.getISO3Country();
String code = locale.getCountry();
String name = locale.getDisplayCountry();
if (!"".equals(iso) && !"".equals(code) && !"".equals(name)) {
countries.add(new Country(iso, code, name));
}
}
Collections.sort(countries, new CountryComparator());
for (Country country : countries) {
System.out.println(country);
}
}
}
class CountryComparator implements Comparator<Country> {
private Comparator comparator;
CountryComparator() {
comparator = Collator.getInstance();
}
public int compare(Country o1, Country o2) {
return comparator.rupare(o1.name, o2.name);
}
}
class Country {
private String iso;
private String code;
public String name;
Country(String iso, String code, String name) {
this.iso = iso;
this.code = code;
this.name = name;
}
public String toString() {
return iso + " - " + code + " - " + name.toUpperCase();
}
}
Get Display Country for default locale
import java.util.Locale;
public class DisplayCountryOutput {
public static void main(String [] argv) {
Locale defaultLocale = Locale.getDefault();
System.out.println(defaultLocale.getISO3Country());
System.out.println(defaultLocale.getDisplayCountry());
System.out.println(defaultLocale.getDisplayCountry(Locale.GERMAN));
}
}
Get Display Name for default locale
import java.util.Locale;
public class DisplayNameOutput {
public static void main(String [] argv) {
Locale defaultLocale = Locale.getDefault();
System.out.println(defaultLocale.getDisplayName());
System.out.println(defaultLocale.getDisplayName(Locale.ITALIAN));
System.out.println(defaultLocale.getDisplayName(Locale.US));
}
}
Get Display Variant for default locale
import java.util.Locale;
public class DisplayVariantOutput {
public static void main(String [] argv) {
Locale defaultLocale = Locale.getDefault();
System.out.println(defaultLocale.getDisplayVariant());
System.out.println(defaultLocale.getDisplayVariant(Locale.US));
System.out.println((new Locale("en","US","WIN_TX_Austin"))
.getDisplayVariant());
}
}
Get ISO3 Language for default locale
import java.util.Locale;
public class DisplayLanguageOutput {
public static void main(String [] argv) {
Locale defaultLocale = Locale.getDefault();
System.out.println(defaultLocale.getISO3Language());
System.out.println(defaultLocale.getDisplayLanguage());
System.out.println(defaultLocale.getDisplayLanguage(Locale.CANADA_FRENCH));
}
}
Get localized name suitable for display to the user
import java.util.Locale;
public class Main {
public static void main(String[] argv) throws Exception {
Locale[] locales = Locale.getAvailableLocales();
for (int i = 0; i < locales.length; i++) {
String locName = locales[i].getDisplayName();
}
}
}
Get the 2-letter country code; may be equal to ""
import java.util.Locale;
public class Main {
public static void main(String[] argv) throws Exception {
Locale[] locales = Locale.getAvailableLocales();
for (int i = 0; i < locales.length; i++) {
String country = locales[i].getCountry();
}
}
}
Japanese ImperialCalendar
import java.util.Calendar;
import java.util.Locale;
import java.util.Map;
public class JapaneseCalendar {
public static void main(String args[]) {
Locale locale = new Locale("ja", "JP", "JP");
Calendar now = Calendar.getInstance(locale);
Map<String, Integer> names = now.getDisplayNames(Calendar.ERA, Calendar.LONG, locale);
System.out.printf("%s%n", names);
System.out.printf("It is year %tY of the current era%n", now);
System.out.printf("The calendar class is: %s%n", now.getClass().getName());
}
}
Listing All Available Locales
import java.util.Locale;
public class Main {
public static void main(String[] argv) throws Exception {
Locale[] locales = Locale.getAvailableLocales();
for (int i = 0; i < locales.length; i++) {
// Get the 2-letter language code
String language = locales[i].getLanguage();
}
}
}
Load Resource Bundle based on Locale Name
import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;
class ResourceBundleApp {
public static void main(String args[]) {
if (args.length != 1) {
System.out.println("Usage: java ResourceBundleApp country_code");
System.exit(0);
}
Locale mexico = new Locale("es", "MX");
Locale spain = new Locale("es", "ES");
Locale locales[] = { mexico, spain, Locale.US, Locale.CANADA, Locale.UK };
Locale newLocale = null;
for (int i = 0; i < locales.length; ++i) {
if (args[0].equals(locales[i].getCountry())) {
newLocale = locales[i];
break;
}
}
if (newLocale == null) {
System.out.println("Country not found.");
System.exit(0);
}
ResourceBundle resources = ResourceBundle.getBundle("TextBundle", newLocale);
Enumeration e = resources.getKeys();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
System.out.println(resources.getString(key));
}
}
}
Locales
The java.util.Locale class represents a locale. There are three main components of a Locale object:
- language,
- country, and
- variant.
The variant argument is a vendor- or browser-specific code. For example, you use WIN for Windows, MAC for Macintosh, and POSIX for POSIX. If there are two variants, separate them with an underscore, and put the most important one first.
To construct a Locale object, use one of the Locale class"s constructors.
public Locale(java.lang.String language)
public Locale(java.lang.String language, java.lang.String country)
public Locale(java.lang.String language, java.lang.String country, java.lang.String variant)
Locale Sensitive Services SPI
SPI stands for service provider interface.
You can write your own implementations for most locale sensitive classes in the java.text and java.util packages for locales that are not (yet) supported by the JRE.
with the new Java 6 feature, you can provide locale-sensitive implementations for the following entities.
- Language and country names for the java.util.Locale class
- Time zone names for the java.util.TimeZone class
- Symbols for the java.util.Currency class
java.text.BreakIterator
java.text.Collator
java.text.DateFormat
java.text.NumberFormat
java.text.DateFormatSymbols
java.text.DecimalFormatSymbol
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.spi.DateFormatProvider;
import java.util.Locale;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class AntarcticaLocaleDemo {
public static void main(String [] args) {
Date now = new Date();
DateFormat defaultFormat = DateFormat.getDateTimeInstance();
String defaultString = defaultFormat.format (now);
System.out.println ("Default : " + defaultString);
DateFormat antarcticaFormat =
DateFormat.getDateTimeInstance (
DateFormat.FULL, DateFormat.FULL,
new Locale ("en", "AQ"));
String antarcticaString = antarcticaFormat.format (now);
System.out.println ("Antarctica: " + antarcticaString);
}
}
class DateFormatProviderImpl extends DateFormatProvider {
private Locale antarctica = new Locale ("en", "AQ");
public Locale[] getAvailableLocales() {
return new Locale [] {antarctica};
}
public DateFormat getTimeInstance(int style, Locale locale) {
if (locale.equals(antarctica)) {
return new SimpleDateFormat("HH.mm.ss");
}
return null;
}
public DateFormat getDateTimeInstance(int dateStyle, Locale locale) {
if (locale.equals(antarctica)) {
return new SimpleDateFormat("yyyy~MM~dd HH.mm.ss");
}
return null;
}
public DateFormat getDateTimeInstance(int dateStyle,
int timeStyle, Locale locale) {
if (locale.equals(antarctica)) {
return new SimpleDateFormat("yyyy~MM~dd HH.mm.ss");
}
return null;
}
@Override
public DateFormat getDateInstance(int style, Locale locale) {
if (locale.equals(antarctica)) {
return new SimpleDateFormat("yyyy~MM~dd HH.mm.ss");
}
return null;
}
}
//Reference:
//Java 6 New Features: A Tutorial
//by Budi Kurniawan
//Brainy Software Corp. 2006
//Chapter 4 - Networking
//# ISBN-10: 0975212885
//# ISBN-13: 978-0975212882
Map using Locale objects as keys
/*
* Copyright 2004 Outerthought bvba and Schaubroeck nv
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
The Apache Software License, Version 1.1
Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by the Apache Software Foundation (http://www.apache.org/)."
Alternately, this acknowledgment may appear in the software itself, if
and wherever such third-party acknowledgments normally appear.
4. The names "Apache Cocoon" and "Apache Software Foundation" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
apache@apache.org.
5. Products derived from this software may not be called "Apache", nor may
"Apache" appear in their name, without prior written permission of the
Apache Software Foundation.
THIS SOFTWARE IS PROVIDED ``AS IS"" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software consists of voluntary contributions made by many individuals
on behalf of the Apache Software Foundation and was originally created by
Stefano Mazzocchi <stefano@apache.org>. For more information on the Apache
Software Foundation, please see <http://www.apache.org/>.
*/
import java.util.Locale;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap;
/**
* Map using Locale objects as keys.
*
* <b>This class is based on code from Apache Cocoon.</b>
*
* This map should be filled once using calls to {@link #put}, before any calls
* are made to {@link #get}.
*
*/
public class LocaleMap {
/** Contains the original values the have been put in the map. */
protected ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<String, Object>();
/** Contains "resolved" locales for quick lookup. */
protected ConcurrentHashMap<String, Object> searchMap = new ConcurrentHashMap<String, Object>();
private static final String NO_RESULT = "(no result: you should never see this)";
/**
* Gets an object based on the given locale. An automatic fallback mechanism is used:
* if nothing is found for language-COUNTRY-variant, then language-COUNTRY is searched,
* the language, and finally "" (empty string). If nothing is found null is returned.
*/
public Object get(Locale locale) {
if (map.size() == 0)
return null;
String full = getFullKey(locale);
if (!searchMap.containsKey(full)) {
if (map.containsKey(full)) {
Object object = map.get(full);
searchMap.put(full, object);
return object;
}
String altKey = locale.getLanguage() + "-" + locale.getCountry();
Object object = map.get(altKey);
if (object != null) {
searchMap.put(full, object);
return object;
}
altKey = locale.getLanguage();
object = map.get(altKey);
if (object != null) {
searchMap.put(full, object);
return object;
}
object = map.get("");
if (object != null) {
searchMap.put(full, object);
return object;
}
searchMap.put(full, NO_RESULT);
}
Object result = searchMap.get(full);
return result == NO_RESULT ? null : result;
}
public Object getExact(Locale locale) {
return map.get(getString(locale));
}
public void clear() {
map.clear();
searchMap.clear();
}
public void remove(Locale locale) {
put(locale, null);
}
public Locale[] getLocales() {
String[] localeNames = map.keySet().toArray(new String[0]);
Locale[] locales = new Locale[localeNames.length];
for (int i = 0; i < locales.length; i++)
locales[i] = parseLocale(localeNames[i]);
return locales;
}
public Set entrySet() {
return map.entrySet();
}
private String getFullKey(Locale locale) {
return locale.getLanguage() + "-" + locale.getCountry() + "-" + locale.getVariant();
}
public void put(Locale locale, Object object) {
if (object == null)
map.remove(getString(locale));
else
map.put(getString(locale), object);
searchMap.clear();
}
public boolean isEmpty() {
return map.isEmpty();
}
public boolean equals(Object obj) {
if (!(obj instanceof LocaleMap))
return false;
LocaleMap other = (LocaleMap)obj;
// map can do equals
return map.equals(other.map);
}
public static Locale parseLocale(String localeString) {
StringTokenizer localeParser = new StringTokenizer(localeString, "-_");
String lang = null, country = null, variant = null;
if (localeParser.hasMoreTokens())
lang = localeParser.nextToken();
if (localeParser.hasMoreTokens())
country = localeParser.nextToken();
if (localeParser.hasMoreTokens())
variant = localeParser.nextToken();
if (lang != null && country != null && variant != null)
return new Locale(lang, country, variant);
else if (lang != null && country != null)
return new Locale(lang, country);
else if (lang != null)
return new Locale(lang);
else
return new Locale("");
}
public static String getString(Locale locale) {
boolean hasLanguage = !locale.getLanguage().equals("");
boolean hasCountry = !locale.getCountry().equals("");
boolean hasVariant = !locale.getVariant().equals("");
if (hasLanguage && hasCountry && hasVariant)
return locale.getLanguage() + "-" + locale.getCountry() + "-" + locale.getVariant();
else if (hasLanguage && hasCountry)
return locale.getLanguage() + "-" + locale.getCountry();
else if (hasLanguage)
return locale.getLanguage();
else
return "";
}
}
New locales in Java 6
LanguageCountryLocale IdentifierChinese (Simplified)Singaporezh_SGEnglishMaltaen_MTEnglishPhilippinesen_PHEnglishSingaporeen_SGGreekCyprusel_CYIndonesianIndonesiain_IDJapanese (Japanese calendar)Japanja_JP_JPMalayMalaysiams_MYMalteseMaltamt_MTSpanishUSes_US
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Locale;
import javax.swing.JOptionPane;
public class JapaneseCalendar {
public static void main(String[] args) {
Locale japanese = new Locale("ja", "JP", "JP");
Calendar cal = Calendar.getInstance(japanese);
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, japanese);
String str = df.format(cal.getTime());
JOptionPane.showMessageDialog(null, str);
}
}
Returns the parent locale of a given locale.
/*
* $Id: LocaleUtil.java 667964 2008-06-15 15:00:54Z apetrelli $
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/**
* Utilities for locale manipulation.
*
* @version $Rev: 667964 $ $Date: 2008-06-15 17:00:54 +0200 (Sun, 15 Jun 2008) $
* @since 2.1.0
*/
public class Main {
/**
* The "null" Locale, i.e. a Locale that points to no real locale.
*
* @since 2.1.0
*/
public static final Locale NULL_LOCALE = new Locale("");
/**
*
* Returns the "parent" locale of a given locale.
*
*
* If the original locale is only language-based, the {@link #NULL_LOCALE}
* object is returned.
*
*
* If the original locale is {@link #NULL_LOCALE}, then <code>null</code>
* is returned.
*
*
* @param locale The original locale.
* @return The parent locale.
*/
public static Locale getParentLocale(Locale locale) {
Locale retValue = null;
String language = locale.getLanguage();
String country = locale.getCountry();
String variant = locale.getVariant();
if (!"".equals(variant)) {
retValue = new Locale(language, country);
} else if (!"".equals(country)) {
retValue = new Locale(language);
} else if (!"".equals(language)) {
retValue = NULL_LOCALE;
}
return retValue;
}
}
Set a default Locale
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
NumberFormat formatter = new DecimalFormat();
Locale locale = Locale.getDefault();
System.out.println("Locale = " + locale);
System.out.println(formatter.format(123.123));
locale = Locale.ITALY;
Locale.setDefault(locale);
formatter = new DecimalFormat();
System.out.println("Locale = " + locale);
System.out.println(formatter.format(123.123));
}
}
Set language and country code on the command line
java -Duser.language=fr -Duser.region=CA MyApp
Set only language code on the command line
java -Duser.language=fr -Duser.region= MyApp
Set the default locale to custom locale
import java.util.Locale;
public class Main {
public static void main(String[] argv) throws Exception {
Locale locale = new Locale("fr", "CA");
Locale.setDefault(locale);
}
}
Set the default locale to pre-defined locale
import java.util.Locale;
public class Main {
public static void main(String[] argv) throws Exception {
Locale.setDefault(Locale.FRENCH);
}
}
Setting the Default Locale on the command line
java -Duser.language=2-char-language-code -Duser.region=2-char-country-code MyApp
Use Locale constant
import java.util.Locale;
class LocaleApp {
public static void main(String args[]) {
Locale currentLocale = Locale.getDefault();
Locale locales[] = { Locale.TAIWAN, Locale.KOREA, Locale.ITALY, Locale.CANADA,
Locale.CANADA_FRENCH };
System.out.println("CURRENT LOCALE:");
describeLocale(currentLocale);
System.out.println("OTHER LOCALES:");
for (int i = 0; i < locales.length; ++i)
describeLocale(locales[i]);
}
static void describeLocale(Locale l) {
System.out.println("Country: " + l.getDisplayCountry());
System.out.println("Language: " + l.getDisplayLanguage());
System.out.println();
}
}