public class Locales extends Object
| Constructor and Description |
|---|
Locales() |
| Modifier and Type | Method and Description |
|---|---|
static Locale |
getByFallback(Collection<Locale> values,
Locale locale)
Returns any occurrence of the specified Locale or any its fallback
in the value collection, or null if not found.
|
static Locale |
getCurrent()
Returns the current locale; never null.
|
static Locale |
getLocale(Locale locale)
Converts a Locale to one of them being used before.
|
static Locale |
getLocale(String localeString)
Converts a string that consists of language, country and variant
to a locale.
|
static Locale |
getLocale(String localeString,
char separator)
Converts a string that consists of language, country and variant
to a locale.
|
static Locale |
getThreadLocal()
Returns the locale defined by
setThreadLocal(java.util.Locale). |
static int |
indexOfUnderline(String s)
Returns the index of '_' preceding the country part.
|
static int |
indexOfUnderline(String s,
int j)
Returns the index of '_' preceding the country part, starting from j.
|
static Locale |
setThreadLocal(Locale locale)
Sets the locale for the current thread only.
|
static boolean |
testCurrent(String lang,
String country)
Returns whether the current locale (
getCurrent()) belongs
to the specified language and/or country. |
public static final Locale getCurrent()
Default: If setThreadLocal(java.util.Locale) was called with non-null,
the value is returned. Otherwise, Locale.getDefault() is returned,
public static final boolean testCurrent(String lang, String country)
getCurrent()) belongs
to the specified language and/or country.lang - the language code, e.g., en and zh. Ignored if null.country - the country code, e.g., US. Ignored if null.
If empty, it means no country code at all.public static final Locale setThreadLocal(Locale locale)
Each thread could have an independent locale, called the thread locale.
When Invoking this method under a thread that serves requests, remember to clean up the setting upon completing each request.
Locale old = Locales.setThreadLocal(newValue);
try {
...
} finally {
Locales.setThreadLocal(old);
}locale - the thread locale; null to denote no thread localepublic static final Locale getThreadLocal()
setThreadLocal(java.util.Locale).getCurrent()public static final Locale getLocale(String localeString, char separator)
The separator between language, country and variant is customizable, and whitespaces are ignored, e.g., "zh_TW" and "zh, TW".
Thus, locale.equals(Locales.getLocale(locale.toString(), '_')).
localeString - the locale in string; null is OKseparator - the separator; ((char)0) means to decide automatically
(either ',' or '_')public static final Locale getLocale(String localeString)
A shortcut: getLocale(localeString, (char)0).
public static final Locale getLocale(Locale locale)
This method first look for any locale
public static Locale getByFallback(Collection<Locale> values, Locale locale)
public static int indexOfUnderline(String s, int j)
public static int indexOfUnderline(String s)
Copyright © 2018. All rights reserved.