Class GlobalizationPreferences
- java.lang.Object
-
- com.ibm.icu.util.GlobalizationPreferences
-
- All Implemented Interfaces:
Freezable<GlobalizationPreferences>
,java.lang.Cloneable
public class GlobalizationPreferences extends java.lang.Object implements Freezable<GlobalizationPreferences>
This convenience class provides a mechanism for bundling together different globalization preferences. It includes:- A list of locales/languages in preference order
- A territory
- A currency
- A timezone
- A calendar
- A collator (for language-sensitive sorting, searching, and matching).
- Explicit overrides for date/time formats, etc.
To reset any explicit field so that it will get heuristic values, pass in null. For example, myPreferences.setLocale(null);
All of the heuristics can be customized by subclasses, by overriding getTerritory(), guessCollator(), etc.
The class also supplies display names for languages, scripts, territories, currencies, timezones, etc. These are computed according to the locale/language preference list. Thus, if the preference is Breton; French; English, then the display name for a language will be returned in Breton if available, otherwise in French if available, otherwise in English.
The codes used to reference territory, currency, etc. are as defined elsewhere in ICU, and are taken from CLDR (which reflects RFC 3066bis usage, ISO 4217, and the TZ Timezone database identifiers).
This is at a prototype stage, and has not incorporated all the design changes that we would like yet; further feedback is welcome.
Note:- to get the display name for the first day of the week, use the calendar + display names.
- to get the work days, ask the calendar (when that is available).
- to get papersize / measurement system/bidi-orientation, ask the locale (when that is available there)
- to get the field order in a date, and whether a time is 24hour or not, ask the DateFormat (when that is available there)
- it will support HOST locale when it becomes available (it is a special locale that will ask the services to use the host platform's values).
-
-
Field Summary
Fields Modifier and Type Field Description private static java.util.HashMap<ULocale,java.util.BitSet>
available_locales
static int
BI_CHARACTER
Break iterator typeprivate static int
BI_LIMIT
static int
BI_LINE
Break iterator typestatic int
BI_SENTENCE
Break iterator typestatic int
BI_TITLE
Break iterator typestatic int
BI_WORD
Break iterator typeprivate BreakIterator[]
breakIterators
private Calendar
calendar
private Collator
collator
private Currency
currency
private DateFormat[][]
dateFormats
static int
DF_FULL
Date Format typeprivate static int
DF_LIMIT
static int
DF_LONG
Date Format typestatic int
DF_MEDIUM
Date Format typestatic int
DF_NONE
Date Format typestatic int
DF_SHORT
Date Format typeprivate boolean
frozen
static int
ID_CURRENCY
For selecting a choice of display namesstatic int
ID_CURRENCY_SYMBOL
For selecting a choice of display namesstatic int
ID_KEYWORD
For selecting a choice of display namesstatic int
ID_KEYWORD_VALUE
For selecting a choice of display namesstatic int
ID_LANGUAGE
For selecting a choice of display namesstatic int
ID_LOCALE
For selecting a choice of display namesstatic int
ID_SCRIPT
For selecting a choice of display namesstatic int
ID_TERRITORY
For selecting a choice of display namesstatic int
ID_TIMEZONE
For selecting a choice of display namesstatic int
ID_VARIANT
For selecting a choice of display namesprivate java.util.List<ULocale>
implicitLocales
private static java.lang.String[][]
language_territory_hack
private static java.util.Map<java.lang.String,java.lang.String>
language_territory_hack_map
WARNING: All of this data is temporary, until we start importing from CLDR!!!private java.util.List<ULocale>
locales
static int
NF_CURRENCY
Number Format typestatic int
NF_INTEGER
Number Format typeprivate static int
NF_LIMIT
static int
NF_NUMBER
Number Format typestatic int
NF_PERCENT
Number Format typestatic int
NF_SCIENTIFIC
Number Format typeprivate NumberFormat[]
numberFormats
private java.lang.String
territory
(package private) static java.lang.String[][]
territory_tzid_hack
(package private) static java.util.Map<java.lang.String,java.lang.String>
territory_tzid_hack_map
private TimeZone
timezone
private static int
TYPE_BREAKITERATOR
private static int
TYPE_CALENDAR
private static int
TYPE_COLLATOR
private static int
TYPE_DATEFORMAT
private static int
TYPE_GENERIC
private static int
TYPE_LIMIT
private static int
TYPE_NUMBERFORMAT
-
Constructor Summary
Constructors Constructor Description GlobalizationPreferences()
Default constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description GlobalizationPreferences
cloneAsThawed()
Provides for the clone operation.GlobalizationPreferences
freeze()
Freezes the object.private ULocale
getAvailableLocale(int type)
BreakIterator
getBreakIterator(int type)
Get a copy of the break iterator for the specified type according to the settings.Calendar
getCalendar()
Get a copy of the calendar according to the settings.Collator
getCollator()
Get a copy of the collator according to the settings.Currency
getCurrency()
Get a copy of the currency computed according to the settings.DateFormat
getDateFormat(int dateStyle, int timeStyle)
Gets a date format according to the current settings.java.lang.String
getDisplayName(java.lang.String id, int type)
Get the display name for an ID: language, script, territory, currency, timezone...ULocale
getLocale(int index)
Convenience function for getting the locales in priority orderjava.util.List<ULocale>
getLocales()
Get a copy of the language/locale priority listNumberFormat
getNumberFormat(int style)
Gets a number format according to the current settings.java.util.ResourceBundle
getResourceBundle(java.lang.String baseName)
Convenience function to get a ResourceBundle instance using the specified base name based on the language/locale priority list stored in this object.java.util.ResourceBundle
getResourceBundle(java.lang.String baseName, java.lang.ClassLoader loader)
Convenience function to get a ResourceBundle instance using the specified base name and class loader based on the language/locale priority list stored in this object.java.lang.String
getTerritory()
Gets the territory setting.TimeZone
getTimeZone()
Get the timezone.protected BreakIterator
guessBreakIterator(int type)
This function can be overridden by subclasses to use different heuristics.protected Calendar
guessCalendar()
This function can be overridden by subclasses to use different heuristics.protected Collator
guessCollator()
This function can be overridden by subclasses to use different heuristics.protected Currency
guessCurrency()
This function can be overridden by subclasses to use different heuristicsprotected DateFormat
guessDateFormat(int dateStyle, int timeStyle)
This function can be overridden by subclasses to use different heuristics.protected java.util.List<ULocale>
guessLocales()
This function can be overridden by subclasses to use different heuristics It MUST return a 'safe' value, one whose modification will not affect this object.protected NumberFormat
guessNumberFormat(int style)
This function can be overridden by subclasses to use different heuristics.protected java.lang.String
guessTerritory()
This function can be overridden by subclasses to use different heuristics.protected TimeZone
guessTimeZone()
This function can be overridden by subclasses to use different heuristics.private boolean
isAvailableLocale(ULocale loc, int type)
boolean
isFrozen()
Determines whether the object has been frozen or not.protected java.util.List<ULocale>
processLocales(java.util.List<ULocale> inputLocales)
Process a language/locale priority list specified viasetLocales
.GlobalizationPreferences
reset()
Restore the object to the initial state.GlobalizationPreferences
setBreakIterator(int type, BreakIterator iterator)
Explicitly set the break iterator for this object.GlobalizationPreferences
setCalendar(Calendar calendar)
Sets the calendar.GlobalizationPreferences
setCollator(Collator collator)
Explicitly set the collator for this object.GlobalizationPreferences
setCurrency(Currency currency)
Sets the currency code.GlobalizationPreferences
setDateFormat(int dateStyle, int timeStyle, DateFormat format)
Set an explicit date format.GlobalizationPreferences
setLocale(ULocale uLocale)
Convenience routine for setting the language/locale priority list from a single locale/language.GlobalizationPreferences
setLocales(ULocale[] uLocales)
Convenience routine for setting the language/locale priority list from an array.GlobalizationPreferences
setLocales(java.lang.String acceptLanguageString)
Convenience routine for setting the locale priority list from an Accept-Language string.GlobalizationPreferences
setLocales(java.util.List<ULocale> inputLocales)
Sets the language/locale priority list.GlobalizationPreferences
setNumberFormat(int style, NumberFormat format)
Sets a number format explicitly.GlobalizationPreferences
setTerritory(java.lang.String territory)
Sets the territory, which is a valid territory according to for RFC 3066 (or successor).GlobalizationPreferences
setTimeZone(TimeZone timezone)
Sets the timezone ID.
-
-
-
Field Detail
-
NF_NUMBER
public static final int NF_NUMBER
Number Format type- See Also:
- Constant Field Values
-
NF_CURRENCY
public static final int NF_CURRENCY
Number Format type- See Also:
- Constant Field Values
-
NF_PERCENT
public static final int NF_PERCENT
Number Format type- See Also:
- Constant Field Values
-
NF_SCIENTIFIC
public static final int NF_SCIENTIFIC
Number Format type- See Also:
- Constant Field Values
-
NF_INTEGER
public static final int NF_INTEGER
Number Format type- See Also:
- Constant Field Values
-
NF_LIMIT
private static final int NF_LIMIT
- See Also:
- Constant Field Values
-
DF_FULL
public static final int DF_FULL
Date Format type- See Also:
- Constant Field Values
-
DF_LONG
public static final int DF_LONG
Date Format type- See Also:
- Constant Field Values
-
DF_MEDIUM
public static final int DF_MEDIUM
Date Format type- See Also:
- Constant Field Values
-
DF_SHORT
public static final int DF_SHORT
Date Format type- See Also:
- Constant Field Values
-
DF_NONE
public static final int DF_NONE
Date Format type- See Also:
- Constant Field Values
-
DF_LIMIT
private static final int DF_LIMIT
- See Also:
- Constant Field Values
-
ID_LOCALE
public static final int ID_LOCALE
For selecting a choice of display names- See Also:
- Constant Field Values
-
ID_LANGUAGE
public static final int ID_LANGUAGE
For selecting a choice of display names- See Also:
- Constant Field Values
-
ID_SCRIPT
public static final int ID_SCRIPT
For selecting a choice of display names- See Also:
- Constant Field Values
-
ID_TERRITORY
public static final int ID_TERRITORY
For selecting a choice of display names- See Also:
- Constant Field Values
-
ID_VARIANT
public static final int ID_VARIANT
For selecting a choice of display names- See Also:
- Constant Field Values
-
ID_KEYWORD
public static final int ID_KEYWORD
For selecting a choice of display names- See Also:
- Constant Field Values
-
ID_KEYWORD_VALUE
public static final int ID_KEYWORD_VALUE
For selecting a choice of display names- See Also:
- Constant Field Values
-
ID_CURRENCY
public static final int ID_CURRENCY
For selecting a choice of display names- See Also:
- Constant Field Values
-
ID_CURRENCY_SYMBOL
public static final int ID_CURRENCY_SYMBOL
For selecting a choice of display names- See Also:
- Constant Field Values
-
ID_TIMEZONE
public static final int ID_TIMEZONE
For selecting a choice of display names- See Also:
- Constant Field Values
-
BI_CHARACTER
public static final int BI_CHARACTER
Break iterator type- See Also:
- Constant Field Values
-
BI_WORD
public static final int BI_WORD
Break iterator type- See Also:
- Constant Field Values
-
BI_LINE
public static final int BI_LINE
Break iterator type- See Also:
- Constant Field Values
-
BI_SENTENCE
public static final int BI_SENTENCE
Break iterator type- See Also:
- Constant Field Values
-
BI_TITLE
public static final int BI_TITLE
Break iterator type- See Also:
- Constant Field Values
-
BI_LIMIT
private static final int BI_LIMIT
- See Also:
- Constant Field Values
-
locales
private java.util.List<ULocale> locales
-
territory
private java.lang.String territory
-
currency
private Currency currency
-
timezone
private TimeZone timezone
-
calendar
private Calendar calendar
-
collator
private Collator collator
-
breakIterators
private BreakIterator[] breakIterators
-
dateFormats
private DateFormat[][] dateFormats
-
numberFormats
private NumberFormat[] numberFormats
-
implicitLocales
private java.util.List<ULocale> implicitLocales
-
available_locales
private static final java.util.HashMap<ULocale,java.util.BitSet> available_locales
-
TYPE_GENERIC
private static final int TYPE_GENERIC
- See Also:
- Constant Field Values
-
TYPE_CALENDAR
private static final int TYPE_CALENDAR
- See Also:
- Constant Field Values
-
TYPE_DATEFORMAT
private static final int TYPE_DATEFORMAT
- See Also:
- Constant Field Values
-
TYPE_NUMBERFORMAT
private static final int TYPE_NUMBERFORMAT
- See Also:
- Constant Field Values
-
TYPE_COLLATOR
private static final int TYPE_COLLATOR
- See Also:
- Constant Field Values
-
TYPE_BREAKITERATOR
private static final int TYPE_BREAKITERATOR
- See Also:
- Constant Field Values
-
TYPE_LIMIT
private static final int TYPE_LIMIT
- See Also:
- Constant Field Values
-
language_territory_hack_map
private static final java.util.Map<java.lang.String,java.lang.String> language_territory_hack_map
WARNING: All of this data is temporary, until we start importing from CLDR!!!
-
language_territory_hack
private static final java.lang.String[][] language_territory_hack
-
territory_tzid_hack_map
static final java.util.Map<java.lang.String,java.lang.String> territory_tzid_hack_map
-
territory_tzid_hack
static final java.lang.String[][] territory_tzid_hack
-
frozen
private volatile boolean frozen
-
-
Method Detail
-
setLocales
public GlobalizationPreferences setLocales(java.util.List<ULocale> inputLocales)
Sets the language/locale priority list. If other information is not (yet) available, this is used to to produce a default value for the appropriate territory, currency, timezone, etc. The user should be given the opportunity to correct those defaults in case they are incorrect.- Parameters:
inputLocales
- list of locales in priority order, eg {"be", "fr"} for Breton first, then French if that fails.- Returns:
- this, for chaining
-
getLocales
public java.util.List<ULocale> getLocales()
Get a copy of the language/locale priority list- Returns:
- a copy of the language/locale priority list.
-
getLocale
public ULocale getLocale(int index)
Convenience function for getting the locales in priority order- Parameters:
index
- The index (0..n) of the desired item.- Returns:
- desired item. null if index is out of range
-
setLocales
public GlobalizationPreferences setLocales(ULocale[] uLocales)
Convenience routine for setting the language/locale priority list from an array.- Parameters:
uLocales
- list of locales in an array- Returns:
- this, for chaining
- See Also:
setLocales(List locales)
-
setLocale
public GlobalizationPreferences setLocale(ULocale uLocale)
Convenience routine for setting the language/locale priority list from a single locale/language.- Parameters:
uLocale
- single locale- Returns:
- this, for chaining
- See Also:
setLocales(List locales)
-
setLocales
public GlobalizationPreferences setLocales(java.lang.String acceptLanguageString)
Convenience routine for setting the locale priority list from an Accept-Language string.- Parameters:
acceptLanguageString
- Accept-Language list, as defined by Section 14.4 of the RFC 2616 (HTTP 1.1)- Returns:
- this, for chaining
- See Also:
setLocales(List locales)
-
getResourceBundle
public java.util.ResourceBundle getResourceBundle(java.lang.String baseName)
Convenience function to get a ResourceBundle instance using the specified base name based on the language/locale priority list stored in this object.- Parameters:
baseName
- the base name of the resource bundle, a fully qualified class name- Returns:
- a resource bundle for the given base name and locale based on the language/locale priority list stored in this object
-
getResourceBundle
public java.util.ResourceBundle getResourceBundle(java.lang.String baseName, java.lang.ClassLoader loader)
Convenience function to get a ResourceBundle instance using the specified base name and class loader based on the language/locale priority list stored in this object.- Parameters:
baseName
- the base name of the resource bundle, a fully qualified class nameloader
- the class object from which to load the resource bundle- Returns:
- a resource bundle for the given base name and locale based on the language/locale priority list stored in this object
-
setTerritory
public GlobalizationPreferences setTerritory(java.lang.String territory)
Sets the territory, which is a valid territory according to for RFC 3066 (or successor). If not otherwise set, default currency and timezone values will be set from this. The user should be given the opportunity to correct those defaults in case they are incorrect.- Parameters:
territory
- code- Returns:
- this, for chaining
-
getTerritory
public java.lang.String getTerritory()
Gets the territory setting. If it wasn't explicitly set, it is computed from the general locale setting.- Returns:
- territory code, explicit or implicit.
-
setCurrency
public GlobalizationPreferences setCurrency(Currency currency)
Sets the currency code. If this has not been set, uses default for territory.- Parameters:
currency
- Valid ISO 4217 currency code.- Returns:
- this, for chaining
-
getCurrency
public Currency getCurrency()
Get a copy of the currency computed according to the settings.- Returns:
- currency code, explicit or implicit.
-
setCalendar
public GlobalizationPreferences setCalendar(Calendar calendar)
Sets the calendar. If this has not been set, uses default for territory.- Parameters:
calendar
- arbitrary calendar- Returns:
- this, for chaining
-
getCalendar
public Calendar getCalendar()
Get a copy of the calendar according to the settings.- Returns:
- calendar explicit or implicit.
-
setTimeZone
public GlobalizationPreferences setTimeZone(TimeZone timezone)
Sets the timezone ID. If this has not been set, uses default for territory.- Parameters:
timezone
- a valid TZID (see UTS#35).- Returns:
- this, for chaining
-
getTimeZone
public TimeZone getTimeZone()
Get the timezone. It was either explicitly set, or is heuristically computed from other settings.- Returns:
- timezone, either implicitly or explicitly set
-
getCollator
public Collator getCollator()
Get a copy of the collator according to the settings.- Returns:
- collator explicit or implicit.
-
setCollator
public GlobalizationPreferences setCollator(Collator collator)
Explicitly set the collator for this object.- Parameters:
collator
- The collator object to be passed.- Returns:
- this, for chaining
-
getBreakIterator
public BreakIterator getBreakIterator(int type)
Get a copy of the break iterator for the specified type according to the settings.- Parameters:
type
- break type - BI_CHARACTER or BI_WORD, BI_LINE, BI_SENTENCE, BI_TITLE- Returns:
- break iterator explicit or implicit
-
setBreakIterator
public GlobalizationPreferences setBreakIterator(int type, BreakIterator iterator)
Explicitly set the break iterator for this object.- Parameters:
type
- break type - BI_CHARACTER or BI_WORD, BI_LINE, BI_SENTENCE, BI_TITLEiterator
- a break iterator- Returns:
- this, for chaining
-
getDisplayName
public java.lang.String getDisplayName(java.lang.String id, int type)
Get the display name for an ID: language, script, territory, currency, timezone... Uses the language priority list to do so.- Parameters:
id
- language code, script code, ...type
- specifies the type of the ID: ID_LANGUAGE, etc.- Returns:
- the display name
-
setDateFormat
public GlobalizationPreferences setDateFormat(int dateStyle, int timeStyle, DateFormat format)
Set an explicit date format. Overrides the locale priority list for a particular combination of dateStyle and timeStyle. DF_NONE should be used if for the style, where only the date or time format individually is being set.- Parameters:
dateStyle
- DF_FULL, DF_LONG, DF_MEDIUM, DF_SHORT or DF_NONEtimeStyle
- DF_FULL, DF_LONG, DF_MEDIUM, DF_SHORT or DF_NONEformat
- The date format- Returns:
- this, for chaining
-
getDateFormat
public DateFormat getDateFormat(int dateStyle, int timeStyle)
Gets a date format according to the current settings. If there is an explicit (non-null) date/time format set, a copy of that is returned. Otherwise, the language priority list is used. DF_NONE should be used for the style, where only the date or time format individually is being gotten.- Parameters:
dateStyle
- DF_FULL, DF_LONG, DF_MEDIUM, DF_SHORT or DF_NONEtimeStyle
- DF_FULL, DF_LONG, DF_MEDIUM, DF_SHORT or DF_NONE- Returns:
- a DateFormat, according to the above description
-
getNumberFormat
public NumberFormat getNumberFormat(int style)
Gets a number format according to the current settings. If there is an explicit (non-null) number format set, a copy of that is returned. Otherwise, the language priority list is used.- Parameters:
style
- NF_NUMBER, NF_CURRENCY, NF_PERCENT, NF_SCIENTIFIC, NF_INTEGER
-
setNumberFormat
public GlobalizationPreferences setNumberFormat(int style, NumberFormat format)
Sets a number format explicitly. Overrides the general locale settings.- Parameters:
style
- NF_NUMBER, NF_CURRENCY, NF_PERCENT, NF_SCIENTIFIC, NF_INTEGERformat
- The number format- Returns:
- this, for chaining
-
reset
public GlobalizationPreferences reset()
Restore the object to the initial state.- Returns:
- this, for chaining
-
processLocales
protected java.util.List<ULocale> processLocales(java.util.List<ULocale> inputLocales)
Process a language/locale priority list specified viasetLocales
. The input locale list may be expanded or re-ordered to represent the prioritized language/locale order actually used by this object by the algorithm explained below.
Step 1: Move later occurrence of more specific locale before earlier occurrence of less specific locale.
Before: en, fr_FR, en_US, en_GB
After: en_US, en_GB, en, fr_FR
Step 2: Append a fallback locale to each locale.
Before: en_US, en_GB, en, fr_FR
After: en_US, en, en_GB, en, en, fr_FR, fr
Step 3: Remove earlier occurrence of duplicated locale entries.
Before: en_US, en, en_GB, en, en, fr_FR, fr
After: en_US, en_GB, en, fr_FR, fr
The final locale list is used to produce a default value for the appropriate territory, currency, timezone, etc. The list also represents the lookup order used ingetResourceBundle
for this object. A subclass may override this method to customize the algorithm used for populating the locale list.- Parameters:
inputLocales
- The list of input locales
-
guessDateFormat
protected DateFormat guessDateFormat(int dateStyle, int timeStyle)
This function can be overridden by subclasses to use different heuristics. It MUST return a 'safe' value, one whose modification will not affect this object.- Parameters:
dateStyle
-timeStyle
-
-
guessNumberFormat
protected NumberFormat guessNumberFormat(int style)
This function can be overridden by subclasses to use different heuristics. It MUST return a 'safe' value, one whose modification will not affect this object.- Parameters:
style
-
-
guessTerritory
protected java.lang.String guessTerritory()
This function can be overridden by subclasses to use different heuristics.
-
guessCurrency
protected Currency guessCurrency()
This function can be overridden by subclasses to use different heuristics
-
guessLocales
protected java.util.List<ULocale> guessLocales()
This function can be overridden by subclasses to use different heuristics It MUST return a 'safe' value, one whose modification will not affect this object.
-
guessCollator
protected Collator guessCollator()
This function can be overridden by subclasses to use different heuristics. It MUST return a 'safe' value, one whose modification will not affect this object.
-
guessBreakIterator
protected BreakIterator guessBreakIterator(int type)
This function can be overridden by subclasses to use different heuristics. It MUST return a 'safe' value, one whose modification will not affect this object.- Parameters:
type
-
-
guessTimeZone
protected TimeZone guessTimeZone()
This function can be overridden by subclasses to use different heuristics. It MUST return a 'safe' value, one whose modification will not affect this object.
-
guessCalendar
protected Calendar guessCalendar()
This function can be overridden by subclasses to use different heuristics. It MUST return a 'safe' value, one whose modification will not affect this object.
-
getAvailableLocale
private ULocale getAvailableLocale(int type)
-
isAvailableLocale
private boolean isAvailableLocale(ULocale loc, int type)
-
isFrozen
public boolean isFrozen()
Description copied from interface:Freezable
Determines whether the object has been frozen or not.- Specified by:
isFrozen
in interfaceFreezable<GlobalizationPreferences>
-
freeze
public GlobalizationPreferences freeze()
Description copied from interface:Freezable
Freezes the object.- Specified by:
freeze
in interfaceFreezable<GlobalizationPreferences>
- Returns:
- the object itself.
-
cloneAsThawed
public GlobalizationPreferences cloneAsThawed()
Description copied from interface:Freezable
Provides for the clone operation. Any clone is initially unfrozen.- Specified by:
cloneAsThawed
in interfaceFreezable<GlobalizationPreferences>
-
-