Class LocaleInfo

java.lang.Object
com.iizix.LocaleInfo
All Implemented Interfaces:
Cloneable

public class LocaleInfo extends Object implements Cloneable
The LocaleInfo class contains information of the system-global or client session specific Locale, Currency, NumberFormat, date and time format.
Author:
Christopher Mindus
  • Field Details

    • ENGLISH_LOCALEINFO

      public static final LocaleInfo ENGLISH_LOCALEINFO
      The Locale.ENGLISH based locale info.
    • DEFAULT_LOCALEINFO

      public static final LocaleInfo DEFAULT_LOCALEINFO
      The system default locale info.
  • Method Details

    • getLocaleInfo

      public static LocaleInfo getLocaleInfo(String languageCode)
      Creates or gets a cached copy of a locale for a language code.
      Parameters:
      languageCode - The IETF BCP 47 language tag, e.g. "en-US". The tag is canonicalized, so "en-us" and "en-US" share one cached instance. A tag that is not well-formed, e.g. "en_US" with an underscore, yields the root locale and is logged as a warning.
      Returns:
      The LocaleInfo for the parameters.
      Throws:
      NullPointerException - If languageCode is null.
    • getLocaleInfo

      public static LocaleInfo getLocaleInfo(Locale locale)
      Creates or gets a cached copy of a locale for a language.
      Parameters:
      locale - The locale, non-null.
      Returns:
      A cloned LocaleInfo from a cached instance for the locale in question.
      Throws:
      NullPointerException - If locale is null.
    • getLocaleInfo

      public static LocaleInfo getLocaleInfo(String country, String language, String variant)
      Creates or gets a cached copy of a locale for a language.
      Parameters:
      country - The country, an ISO 3166 two-letter code, null for the country of the default locale. Anything else, e.g. a three-letter code, is treated as no country.
      language - The language, null for none.
      variant - The variant, null for none.
      Returns:
      The LocaleInfo for the parameters. If all parameters are null, or none of them names anything, a CLONE of the current LocaleInfo is returned - never the shared instance itself, which initializeTimeZone(int,String) refuses to be called on.
    • setCurrent

      public static void setCurrent(LocaleInfo localeInfo)
      Sets the current LocaleInfo, i.e. for the Designer. This method throws IllegalStateException if executing elsewhere that in the Designer.
      Parameters:
      localeInfo - The default locale information.
      Throws:
      IllegalStateException - When called and the Designer is not the calling process.
    • getNonNull

      public static LocaleInfo getNonNull(LocaleInfo localeInfo, boolean logDefault)
      Returns the LocaleInfo from the parameter if non-null, otherwise takes the current user's LocaleInfo or the system's.
      Parameters:
      localeInfo - The locale information, or null.
      logDefault - Flag indicating a warning message is logged if the default is used due to none other LocaleInfo found.
      Returns:
      The localeInfo parameter is non-null, otherwise the user's or system locale information.
    • getCurrent

      public static LocaleInfo getCurrent(boolean logDefault)
      Gets the current LocalInfo for the calling thread. This value is extracted from the Thread context depending on where it is executing (i.e. Server, Designer, etc). If none is found, the default LocaleInfo is returned.
      Parameters:
      logDefault - Flag indicating a warning message is logged if the default is used due to none other LocaleInfo found.
      Returns:
      A LocaleInfo instance.
    • clone

      public LocaleInfo clone()
      Clones the instance.
      Overrides:
      clone in class Object
      Returns:
      A new instance without time-zone information and with an empty formatter cache.
    • getLocale

      public Locale getLocale()
      Gets the locale.
      Returns:
      The locale.
    • getCollator

      public Collator getCollator()
      Gets the collator used for sorting strings for this locale.
      Returns:
      The collator.
    • isDefaultEnglish

      public boolean isDefaultEnglish()
      Returns if the locale is using Default English.
      Returns:
      true if language is English.
    • getLanguageCode

      public String getLanguageCode()
      Gets the language code to use.
      Returns:
      The language code, or null for default (not defined by the client).
    • getCurrency

      public Currency getCurrency()
      Gets the currency instance.
      Returns:
      The Currency instance.
    • createNumberFormat

      public NumberFormat createNumberFormat()
      Gets the NumberFormat.
      Returns:
      The NumberFormat for the locale.
    • getDecimalFormatSymbols

      public DecimalFormatSymbols getDecimalFormatSymbols()
      Gets the DecimalFormatSymbols object for the locale.
      Returns:
      The decimal format symbols the current locale.
    • createDecimalFormat

      public DecimalFormat createDecimalFormat()
      Gets a default DecimalFormat instance that can be used by cloning to set patterns, etc. The format returned is a cloned cached copy.
      Returns:
      The DecimalFormat instance initialized with the current locale's DecimalFormatSymbols.
    • createIntegerFormat

      public NumberFormat createIntegerFormat()
      Gets an Integer NumberFormat in the specified locale. The format returned is a cloned cached copy.
      Returns:
      The Integer NumberFormat instance.
    • createPercentFormat

      public NumberFormat createPercentFormat()
      Gets a Percent NumberFormat in the specified locale. The format returned is a cloned cached copy.
      Returns:
      The Percent NumberFormat instance.
    • createCurrencyFormat

      public NumberFormat createCurrencyFormat()
      Gets a Currency NumberFormat in the specified locale with the currency symbols loaded. The format returned is a cloned cached copy.
      Returns:
      The Currency NumberFormat instance.
    • getDateTimeFormatter

      public DateTimeFormatter getDateTimeFormatter(FormatStyle style)
      Gets the date and time formatter in this locale of the specified type.
      Parameters:
      style - The format style.
      Returns:
      A cached formatter.
      See Also:
    • getDateTimeFormatter

      public DateTimeFormatter getDateTimeFormatter(String pattern)
      Gets the date and time formatter in this locale of the specified pattern.
      Parameters:
      pattern - The pattern.
      Returns:
      A formatter.
      See Also:
    • getPeriodFormatter

      public org.joda.time.format.PeriodFormatter getPeriodFormatter()
      Gets the period formatter in this locale.
      Returns:
      The Joda PeriodFormatter instance for the locale.
    • initializeTimeZone

      public int initializeTimeZone(int offset, String name)
      Initializes the time-zone information. This method is not intended to be called, it is done by the framework.

      The time-zone offset is EAST-POSITIVE, in minutes: a client at UTC+02:00 sends 120, matching ZoneOffset. The browser primitive is the other way round - Date.prototype.getTimezoneOffset() is positive WEST - so client.js negates it before sending, in buildEnv. Do not "simplify" either side: the two conventions disagreeing across the wire was IIZI-JS-ZONEOFFSET-SIGN-INVERTED, which stored the wrong sign for every session from the first release until 2026-08-26.

      When no usable name arrives, the zone is built from the offset as ZoneId.ofOffset("GMT",zo) - id GMT+02:00, and GMT at zero offset. That spelling is RFC-adjacent and is what already-persisted values carry, so it is kept DELIBERATELY. Using the ZoneOffset itself (ids +02:00, and Z at zero) was considered and REJECTED: a value built by that code would not equals() one parsed from the old persisted string form for the same instant, and the compat break buys nothing the platform needs. See ruling R-B in RULINGS-BATCH2. Do not "modernise" this to the offset or to a "UTC" prefix.

      Parameters:
      offset - In minutes, -1080 to 1080 inclusive, which is the 18 hours either way that ZoneOffset permits. Anything outside is refused with -1 and a warning.
      name - The time-zone name in the form "{area}/{city}", e.g. "Europe/Berlin", null or empty for none.
      Returns:
      -1 for invalid offset, 0=success, 1 for unknown zone name but success. Any problems with offset or name are logged as warnings, including an offset that contradicts a name that did resolve.
      Throws:
      IllegalStateException - If called on DEFAULT_LOCALEINFO or ENGLISH_LOCALEINFO. Those two are shared by the whole JVM and carry no session; a zone stored on either would be answered to every caller that has none of its own. Clone first - every door of this class hands out clones.
    • getTimeZone

      public TimeZone getTimeZone()
      Gets the time-zone.
      Returns:
      The time-zone, if not initialized, the default system time-zone is returned and remembered by this instance.
    • getZoneId

      public ZoneId getZoneId()
      Gets the time-zone ID.
      Returns:
      The time-zone ID, if not initialized, the default system time-zone ID is returned and remembered by this instance.
    • getZoneOffset

      public ZoneOffset getZoneOffset()
      Gets the time-zone offset.

      When the offset was not initialized, it is the offset getZoneId() has at this instant, derived from the zone rules and not remembered: a locale that was never given a zone therefore follows the daylight-saving changes of the system time-zone instead of pinning the offset of the day it was first read.

      Returns:
      The time-zone offset, if not initialized, the current offset of the default system time-zone is returned.
    • formatTemperature

      public String formatTemperature(double temperature, int decimals, TemperatureUnit unit)
      Converts a temperature to a String.
      Parameters:
      temperature - The temperature value.
      decimals - The number of decimals, zero or more; a negative count is taken as zero.
      unit - The temperature unit.
      Returns:
      The String formatted as e.g. "1234,45 °F" for Fahrenheit, always carrying the locale's separators and the unit suffix, at zero decimals too.
    • formatTemperature

      public String formatTemperature(float temperature, int decimals, TemperatureUnit unit)
      Converts a temperature to a String.
      Parameters:
      temperature - The temperature value.
      decimals - The number of decimals, zero or more; a negative count is taken as zero.
      unit - The temperature unit.
      Returns:
      The String formatted as e.g. "1234,45 °F" for Fahrenheit, always carrying the locale's separators and the unit suffix, at zero decimals too.