Copyright© 2008-2022 Sitevision AB, all rights reserved.
@Requireable(value="LocaleUtil") public interface LocaleUtil
Note! Sitevision supports a few Locales that uses 3 char ISO-639 language codes. These are not automatically handled by Java. Sitevision also supports a few 2 char language codes that isn't natively supported by Java in most regions of the world. This interface handles all Locales supported/installed by Java and all "custom" Locales that Sitevision has added. Here are some examples for such Locales:
new Locale("se", "SE")
new Locale("sma", "SE")
new Locale("smj", "SE")
new Locale("ckb", "IR")
new Locale("fa", "IR")
new Locale("ku", "TR")
Information about Supported Locales in JDK 8.
An instance of the Sitevision class implementing this interface can be obtained via Utils.getLocaleUtil()
.
See Utils
for how to obtain an instance of the Utils
interface.
PortletContextUtil.getCurrentLocale()
,
NodeResolverUtil.getLocaleResolver()
Modifier and Type | Method and Description |
---|---|
List<Locale> |
getAvailableLocales()
Gets an immutable list of all available Locales (installed Java Locales and custom ones added by Sitevision).
|
String |
getLanguageTag(Locale aLocale)
Returns the IETF BCP 47 language tag value for a Locale.
|
Locale |
getLocaleByString(String aLocaleString)
Returns the Locale for a language tag or a String that contains language or language + country ("en-US" or "en" or "en_US").
|
String |
getLocalizedLanguageName(Locale aLocale,
Locale aDisplayLocale)
Gets the localized language name for a specified Locale.
|
String |
getLocalizedLanguageNameByString(String aLocaleString,
Locale aDisplayLocale)
Gets the localized language name for a Locale that is resolved via a specified locale string.
|
String |
getNativeLanguageName(Locale aLocale)
Gets the native language name for a specified Locale.
|
String |
getNativeLanguageNameByString(String aLocaleString)
Gets the native language name for a Locale that is resolved via a specified locale string.
|
String |
getNormalizedLanguage(Locale aLocale)
Returns the normalized language for a specified Locale.
|
boolean |
isRTL(Locale aLocale)
Checks if a locale has writing direction Right-To-Left (RTL).
|
Locale |
resolveCountryLocale(Locale aLocale)
Gets a Locale with language and country for a (typically language-only) Locale.
|
List<Locale> getAvailableLocales()
Note! The returned List is immutable/unmodifiable, i.e. you must create a copy if you want to modify it (e.g. sort).
Below is an example of how to display some info about all locales via server-side Javascript:
(function() {
var localeUtil = require('LocaleUtil'),
locales = localeUtil.getAvailableLocales(),
count = locales.size(),
currentLocale = require('PortletContextUtil').getCurrentLocale(),
locale, i;
out.println('<table>');
out.println('<tr>');
out.println(' <th>Language</th>');
out.println(' <th>Country</th>');
out.println(' <th>Language Tag</th>');
out.println(' <th>Native language name</th>');
out.println(' <th>Localized language name</th>');
out.println(' <th>Directionality</th>');
out.println('</tr>');
for (i = 0; i < count; i++) {
locale = locales.get(i);
out.println('<tr>');
out.println(' <td>' + localeUtil.getNormalizedLanguage(locale) + '</td>');
out.println(' <td>' + locale.getCountry() + '</td>');
out.println(' <td>' + localeUtil.getLanguageTag(locale) + '</td>');
out.println(' <td>' + localeUtil.getNativeLanguageName(locale) + '</td>');
out.println(' <td>' + localeUtil.getLocalizedLanguageName(locale, currentLocale) + '</td>');
out.println(' <td>' + (localeUtil.isRTL(locale) ? 'RTL' : 'LTR') + '</td>');
out.println('</tr>');
}
out.println('</table>');
}());
String getNativeLanguageName(Locale aLocale)
Some examples:
aLocale
- the localeaLocale
, or null
if aLocale
is null
String getLocalizedLanguageName(Locale aLocale, Locale aDisplayLocale)
Some examples:
aLocale
and an English aDisplayLocale
will return SwedishaLocale
and a Swedish aDisplayLocale
will return EngelskaaLocale
and a Swedish aDisplayLocale
will return TyskaaLocale
and an English aDisplayLocale
will return GermanaLocale
- the locale to get the language name foraDisplayLocale
- the locale that should be used when displaying the language name of aLocale
aLocale
, or null
if aLocale
is null
String getNativeLanguageNameByString(String aLocaleString)
This method can be seen as a convenience method that combines the getLocaleByString(String)
method and the
getNativeLanguageName(java.util.Locale)
method to resolve a Locale
and get the native language name.
But this is not strictly true. This method will handle all all well-formed locale strings that starts with a valid
language code used by a supported locale (even though the "full" locale string might actually refer to a non-supported locale).
Some examples of this forgiving/lenient behaviour:
"rmy-RO"
matches a supported locale (Romani in Romania) and will return
the native name for the "rmy" language.
"rmy_RO"
matches a supported locale (Romani in Romania) and will return
the native name for the "rmy" language.
"rmy"
does NOT match the supported locale "rmy-RO"/"rmy_RO"
but will still return
the native name for the "rmy" language.
"rmy-SE"
does NOT match the supported locale "rmy-RO"
but will still return
the native name for the "rmy" language.
"rmy_SE"
does NOT match the supported locale "rmy_RO"
but will still return
the native name for the "rmy" language.
aLocaleString
- a language tag ("en-US") or string that contains language only ("en") or language and country ("en_US"),
typically what you will get as result of Locale.toString()
aLocale
that matches aLocaleString
,
or null
if aLocaleString
is null
, whitespace only or doesn't match a supported LocaleString getLocalizedLanguageNameByString(String aLocaleString, Locale aDisplayLocale)
This is a convenience method that combines the getLocaleByString(String)
method and the
getLocalizedLanguageName(java.util.Locale, java.util.Locale)
method to resolve a Locale
and get the localized language name.
aLocaleString
- a string that contains language ("en") or language and country ("en_US"),
typically what you will get as result of Locale.toString()
aDisplayLocale
- the locale that should be used when displaying the language name of the Locale
resolved by aLocaleString
aLocale
that matches aLocaleString
,
or null
if aLocaleString
is null
, whitespace only or doesn't match a supported LocaleLocale getLocaleByString(String aLocaleString)
A toString()
on a english american locale returns the String "en_US"
, but to create a locale
from that string you can NOT just create it by doing: new Locale("en_US")
. You must split language (en)
and country (US) since the proper way of creating the Locale is: new Locale("en", "US")
.
Some examples of accepted locale strings:
Note! This method only handles supported Locale's, i.e. Locales available via getAvailableLocales()
.
aLocaleString
- a language tag or the toString() value of a Locale (i.e. a String with a lowercased language - or a lowercased language
and uppercased country, separated with underscore).aLocaleString
,
or null
if aLocaleString
is null
, whitespace only or doesn't match a supported LocaleString getLanguageTag(Locale aLocale)
Language tags are used to indicate the language of texts or other items in HTML and XML documents. The language tag syntax is defined by the IETF BCP 47 and exemplified by W3C internationalization in Language tags in HTML and XML.
This method supports both the Java default locales and the custom-defined ones in Sitevision (such as Northern Sami). It produces a well-formatted BCP 47 value for a specified Locale but the language and region are not verified against the IANA language subtag registry.
Some output examples:
aLocale
- a localeaLocale
formatted according to BCP 47, or null
if aLocale
is null
String getNormalizedLanguage(Locale aLocale)
Java uses old ISO language codes for for language codes that have changed. For instance: the legacy ISO language code for Yiddish is "ji", but that was changed to "yi" 1989. This method compensates for Javas legacy considerations and returns the new ISO language codes instead.
aLocale
- a localeboolean isRTL(Locale aLocale)
Most languages have a clear writing direction, typically Left-To-Right (LTR). Though, some languages has mixed writing directions (e.g. text is RTL but numbers are LTR) and some languages is written with different symbols (e.g. Latin, Arabic) in different regions. This method will return true for such complex/mixed languages if the official writing direction is RTL or if RTL is used by the majority of the native language writers.
This method supports the Java default locales and the custom-defined ones in Sitevision (such as Northern Sami).
It also supports locales that aren't officially supported by Sitevision (i.e. not present in getAvailableLocales()
).
The direction of the first char in the locales native name (as of getNativeLanguageName(Locale)
)
is used to determine if the locale is LTR or RTL for such (unofficial) locales.
aLocale
- a localeLocale resolveCountryLocale(Locale aLocale)
Gets a Locale with language and country for a (typically language-only) Locale.
Locale/language | Replacement |
---|---|
Swedish (sv) | Sweden (sv-SE) |
Norwegian (no) | Norway (no-NO) |
Danish (da) | Denmark (da-DK) |
Finnish (fi) | Finland (fi-FI) |
Icelandic (is) | Iceland (is-IS) |
Belarusian (be) | Belarus (be-BY) |
Bulgarian (bg) | Bulgaria (bg-BG) |
Catalan (ca) | Spain (ca-ES) |
Czech (cs) | Czech Republic (cs-CZ) |
Estonian (et) | Estonia (et-EE) |
Irish (ga) | Ireland (ga-IE) |
Hebrew (he) and (iw) | Israel (he-IL) |
Hindi (hi) | India (hi-IN) |
Croatian (hr) | Croatia (hr-HR) |
Indonesian (id) and (in) | Indonesia (id-ID) |
Italian (it) | Italy (it-IT) |
Japanese (ja) | Japan (ja-JP) |
Lithuanian (lt) | Lithuania (lt-LT) |
Latvian (lv) | Latvia (lv-LV) |
Malay (ms) | Malaysia (ms-MY) |
Maltese (mt) | Malta (mt-MT) |
Polish (pl) | Poland (pl-PL) |
Romanian (ro) | Romania (ro-RO) |
Slovak (sk) | Slovakia (sk-SK) |
Slovenian (sl) | Slovenia (sl-SI) |
Albanian (sq) | Albania (sq-AL) |
Thai (th) | Thailand (th-TH) |
Turkish (tr) | Turkey (tr-TR) |
Ukranian (uk) | Ukraine (uk-UA) |
Vietnamese (vi) | Vietnam (vi-VN) |
Important language-only note! There are many language-only locales that can not be resolved
(i.e. the language is officially spoken in multiple countries).
This method will return null
for such locales. For example:
aLocale
- a Locale, typically a language-only LocaleSitevision - Portal and Content Management Made Easy
Sitevision is an advanced Java enterprise portal product and a portlet container (JSR 286) that implements Java Content Repository (JSR 283).
Copyright© 2008-2022 Sitevision AB, all rights reserved.