Class LocaleHelper


  • public class LocaleHelper
    extends Object
    • Constructor Detail

      • LocaleHelper

        public LocaleHelper()
    • 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, then null 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 by Locale.getDefault(). In the rare cases that this may be null, it is initialized to Locale.US.

        In the Pentaho server, the default locale is initialized by the org.pentaho.platform.web.http.context.SolutionContextListener. When the web.xml server parameters locale-language and locale-country are both specified and match an available locale, as given by Locale.getAvailableLocales(), it sets the default locale to that.

        Parameters:
        newLocale - The new default locale, possibly null.
      • 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 of parseLocale(String) and setThreadLocaleOverride(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, or null or an empty string, if none.
      • setLocaleOverride

        @Deprecated
        public static void setLocaleOverride​(Locale newLocale)
        Deprecated.
        Sets the locale override of the current thread.
        Parameters:
        newLocale - The new override locale. Can be null.
      • getLocaleOverride

        @Deprecated
        public static Locale getLocaleOverride()
        Deprecated.
        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 the org.pentaho.platform.web.http.filters.HttpSessionPentahoSessionIntegrationFilter filter.

        Parameters:
        newLocale - The new thread locale override. Can be null.
      • 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.
        Sets the base locale of the current thread.
        Parameters:
        newLocale - The new base locale. Can be null.
      • 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 the org.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 be null.
      • 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:

        1. The current thread's locale override, getThreadLocaleOverride()
        2. The current thread's base locale, getThreadLocaleBase()
        3. The default locale, getDefaultLocale()

        The effective locale is never null.

        Returns:
        The effective locale.
      • 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()
      • getClosestLocale

        public static String getClosestLocale​(String locale,
                                              String[] locales)