Class LocaleHelper
- java.lang.Object
-
- org.pentaho.platform.util.messages.LocaleHelper
-
public class LocaleHelper extends Object
-
-
Field Summary
Fields Modifier and Type Field Description static int
FORMAT_FULL
static int
FORMAT_IGNORE
static int
FORMAT_LONG
static int
FORMAT_MEDIUM
static int
FORMAT_SHORT
static String
LEFT_TO_RIGHT
static String
USER_LOCALE_PARAM
static String
USER_LOCALE_SETTING
static String
UTF_8
-
Constructor Summary
Constructors Constructor Description LocaleHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static String
convertEncodedStringToSystemDefaultEncoding(String fromEncoding, String encodedStr)
This method converts strings from a known encoding into a string encoded by the system default encoding.static String
convertISOStringToSystemDefaultEncoding(String isoString)
This method is called to convert strings from ISO-8859-1 (post/get parameters for example) into the default system locale.static String
convertStringEncoding(String sourceString, String sourceEncoding, String targetEncoding)
This method converts strings between various encodings.static String
getClosestLocale(String locale, String[] locales)
static NumberFormat
getCurrencyFormat()
static DateFormat
getDateFormat(int dateFormat, int timeFormat)
static Locale
getDefaultLocale()
Gets the default locale.static DateFormat
getFullDateFormat(boolean date, boolean time)
static Locale
getLocale()
Gets the effective locale of the current thread.static Locale
getLocaleOverride()
Deprecated.UsegetThreadLocaleOverride()
instead.static DateFormat
getLongDateFormat(boolean date, boolean time)
static DateFormat
getMediumDateFormat(boolean date, boolean time)
static NumberFormat
getNumberFormat()
static DateFormat
getShortDateFormat(boolean date, boolean time)
static String
getSystemEncoding()
static String
getTextDirection()
static Locale
getThreadLocaleBase()
Gets the base locale of the current thread.static Locale
getThreadLocaleOverride()
Gets the locale override of the current thread.static boolean
isAscii(String aString)
static boolean
isLatin1(String aString)
static String
isoToUtf8(String isoString)
This method converts an ISO-8859-1 encoded string to a UTF-8 encoded string.static boolean
isWithinCharset(String aString, String charsetTarget)
static void
parseAndSetLocaleOverride(String localeOverride)
Deprecated.Use a combination ofparseLocale(String)
andsetThreadLocaleOverride(Locale)
instead.static Locale
parseLocale(String locale)
Parses a locale string, taking into account if it is composed of several parts, separated by_
characters.static void
setDefaultLocale(Locale newLocale)
Sets the default locale.static void
setLocale(Locale newLocale)
Deprecated.UsesetThreadLocaleBase(Locale)
instead.static void
setLocaleOverride(Locale newLocale)
Deprecated.UsesetThreadLocaleOverride(Locale)
instead.static void
setSessionLocaleOverride(Locale locale)
Sets the locale override of the current session, if any.static void
setSystemEncoding(String encoding)
static void
setTextDirection(String textDirection)
static void
setThreadLocaleBase(Locale newLocale)
Sets the base locale of the current thread.static void
setThreadLocaleOverride(Locale newLocale)
Sets the locale override of the current thread.static String
utf8ToIso(String utf8String)
This method converts a UTF8-encoded string to ISO-8859-1
-
-
-
Field Detail
-
FORMAT_SHORT
public static final int FORMAT_SHORT
- See Also:
- Constant Field Values
-
FORMAT_MEDIUM
public static final int FORMAT_MEDIUM
- See Also:
- Constant Field Values
-
FORMAT_LONG
public static final int FORMAT_LONG
- See Also:
- Constant Field Values
-
FORMAT_FULL
public static final int FORMAT_FULL
- See Also:
- Constant Field Values
-
FORMAT_IGNORE
public static final int FORMAT_IGNORE
- See Also:
- Constant Field Values
-
UTF_8
public static final String UTF_8
- See Also:
- Constant Field Values
-
LEFT_TO_RIGHT
public static final String LEFT_TO_RIGHT
- See Also:
- Constant Field Values
-
USER_LOCALE_PARAM
public static final String USER_LOCALE_PARAM
- See Also:
- Constant Field Values
-
USER_LOCALE_SETTING
public static final String USER_LOCALE_SETTING
- See Also:
- Constant Field Values
-
-
Method Detail
-
parseLocale
public static Locale parseLocale(String locale)
Parses a locale string, taking into account if it is composed of several parts, separated by_
characters.If the given locale string is
null
or empty, thennull
is returned.If the locale string has at least two parts, language and country, it creates and returns a locale using the first two parts as first and second arguments of the locale constructor
Locale(String, String)
, and ignoring the remaining parts.Otherwise, it creates and returns a locale in which the whole string is passed to
Locale(String)
.See BISERVER-9863 for more information.
- Parameters:
locale
- The locale string.- Returns:
- The new locale or
null
.
-
setDefaultLocale
public static void setDefaultLocale(Locale newLocale)
Sets the default locale.The default locale applies to any session or thread.
When
null
, the locale assumes the Java VM's current default locale, as given byLocale.getDefault()
. In the rare cases that this may benull
, it is initialized toLocale.US
.In the Pentaho server, the default locale is initialized by the
org.pentaho.platform.web.http.context.SolutionContextListener
. When theweb.xml
server parameterslocale-language
andlocale-country
are both specified and match an available locale, as given byLocale.getAvailableLocales()
, it sets the default locale to that.- Parameters:
newLocale
- The new default locale, possiblynull
.
-
getDefaultLocale
public static Locale getDefaultLocale()
Gets the default locale.The default locale applies to any session or thread.
The default locale is always defined; it's never
null
.- Returns:
- The default locale.
-
parseAndSetLocaleOverride
@Deprecated public static void parseAndSetLocaleOverride(String localeOverride)
Deprecated.Use a combination ofparseLocale(String)
andsetThreadLocaleOverride(Locale)
instead.BISERVER-9863 Check if override locale string contains language and country. If so, instantiate Locale with two parameters for language and country, instead of just language.- Parameters:
localeOverride
- The new locale override, ornull
or an empty string, if none.
-
setLocaleOverride
@Deprecated public static void setLocaleOverride(Locale newLocale)
Deprecated.UsesetThreadLocaleOverride(Locale)
instead.Sets the locale override of the current thread.- Parameters:
newLocale
- The new override locale. Can benull
.
-
getLocaleOverride
@Deprecated public static Locale getLocaleOverride()
Deprecated.UsegetThreadLocaleOverride()
instead.Gets the locale override of the current thread.- Returns:
- The locale override, if set;
null
, otherwise.
-
setThreadLocaleOverride
public static void setThreadLocaleOverride(Locale newLocale)
Sets the locale override of the current thread.A thread's locale override is the most specific locale, as returned by
getLocale()
.The Pentaho server sets the thread locale override, of threads handling HTTP requests, to the locale override of the Pentaho session of the corresponding request, as given by
IPentahoSession.getAttributeLocaleOverride()
. This is done by theorg.pentaho.platform.web.http.filters.HttpSessionPentahoSessionIntegrationFilter
filter.- Parameters:
newLocale
- The new thread locale override. Can benull
.
-
getThreadLocaleOverride
public static Locale getThreadLocaleOverride()
Gets the locale override of the current thread.- Returns:
- The locale override, if set;
null
, otherwise.
-
setLocale
@Deprecated public static void setLocale(Locale newLocale)
Deprecated.UsesetThreadLocaleBase(Locale)
instead.Sets the base locale of the current thread.- Parameters:
newLocale
- The new base locale. Can benull
.
-
setThreadLocaleBase
public static void setThreadLocaleBase(Locale newLocale)
Sets the base locale of the current thread.The Pentaho server sets the base locale of threads handling HTTP requests to the corresponding request's locale, as given by
javax.servlet.http.HttpServletRequest#getLocale()
. This is done by theorg.pentaho.platform.web.http.filters.HttpSessionPentahoSessionIntegrationFilter
filter.In this manner, according to the rules of
getLocale()
, and unless a web user has explicitly set their Pentaho session's locale override (or the thread's override locale is set by some other means), the effective locale will be the web browser's preferred language.- Parameters:
newLocale
- The new base locale. Can benull
.
-
getThreadLocaleBase
public static Locale getThreadLocaleBase()
Gets the base locale of the current thread.- Returns:
- The base locale, if any;
null
, otherwise.
-
getLocale
public static Locale getLocale()
Gets the effective locale of the current thread.The effective locale is the value of the first non-
null
, among the following:-
The current thread's locale override,
getThreadLocaleOverride()
-
The current thread's base locale,
getThreadLocaleBase()
-
The default locale,
getDefaultLocale()
The effective locale is never
null
.- Returns:
- The effective locale.
-
The current thread's locale override,
-
setSessionLocaleOverride
public static void setSessionLocaleOverride(Locale locale)
Sets the locale override of the current session, if any.- See Also:
PentahoSessionHolder.getSession()
,IPentahoSession.setAttributeLocaleOverride(String)
-
setSystemEncoding
public static void setSystemEncoding(String encoding)
-
setTextDirection
public static void setTextDirection(String textDirection)
-
getSystemEncoding
public static String getSystemEncoding()
-
getTextDirection
public static String getTextDirection()
-
convertISOStringToSystemDefaultEncoding
public static String convertISOStringToSystemDefaultEncoding(String isoString)
This method is called to convert strings from ISO-8859-1 (post/get parameters for example) into the default system locale.- Parameters:
isoString
-- Returns:
- Re-encoded string
-
convertEncodedStringToSystemDefaultEncoding
public static String convertEncodedStringToSystemDefaultEncoding(String fromEncoding, String encodedStr)
This method converts strings from a known encoding into a string encoded by the system default encoding.- Parameters:
fromEncoding
-encodedStr
-- Returns:
- Re-encoded string
-
isoToUtf8
public static String isoToUtf8(String isoString)
This method converts an ISO-8859-1 encoded string to a UTF-8 encoded string.- Parameters:
isoString
-- Returns:
- Re-encoded string
-
utf8ToIso
public static String utf8ToIso(String utf8String)
This method converts a UTF8-encoded string to ISO-8859-1- Parameters:
utf8String
-- Returns:
- Re-encoded string
-
convertStringEncoding
public static String convertStringEncoding(String sourceString, String sourceEncoding, String targetEncoding)
This method converts strings between various encodings.- Parameters:
sourceString
-sourceEncoding
-targetEncoding
-- Returns:
- Re-encoded string.
-
isAscii
public static boolean isAscii(String aString)
- Parameters:
aString
-- Returns:
- true if the provided string is completely within the US-ASCII character set.
-
isLatin1
public static boolean isLatin1(String aString)
- Parameters:
aString
-- Returns:
- true if the provided string is completely within the Latin-1 character set (ISO-8859-1).
-
isWithinCharset
public static boolean isWithinCharset(String aString, String charsetTarget)
- Parameters:
aString
-charsetTarget
-- Returns:
- true if the provided string is completely within the target character set.
-
getDateFormat
public static DateFormat getDateFormat(int dateFormat, int timeFormat)
-
getShortDateFormat
public static DateFormat getShortDateFormat(boolean date, boolean time)
-
getMediumDateFormat
public static DateFormat getMediumDateFormat(boolean date, boolean time)
-
getLongDateFormat
public static DateFormat getLongDateFormat(boolean date, boolean time)
-
getFullDateFormat
public static DateFormat getFullDateFormat(boolean date, boolean time)
-
getNumberFormat
public static NumberFormat getNumberFormat()
-
getCurrencyFormat
public static NumberFormat getCurrencyFormat()
-
-