Class GlobalizationPreferences
- All Implemented Interfaces:
Freezable<GlobalizationPreferences>
,Cloneable
- 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
FieldsModifier and TypeFieldDescriptionstatic final int
Break iterator typestatic final int
Break iterator typestatic final int
Break iterator typestatic final int
Break iterator typestatic final int
Break iterator typestatic final int
Date Format typestatic final int
Date Format typestatic final int
Date Format typestatic final int
Date Format typestatic final int
Date Format typestatic final int
For selecting a choice of display namesstatic final int
For selecting a choice of display namesstatic final int
For selecting a choice of display namesstatic final int
For selecting a choice of display namesstatic final int
For selecting a choice of display namesstatic final int
For selecting a choice of display namesstatic final int
For selecting a choice of display namesstatic final int
For selecting a choice of display namesstatic final int
For selecting a choice of display namesstatic final int
For selecting a choice of display namesstatic final int
Number Format typestatic final int
Number Format typestatic final int
Number Format typestatic final int
Number Format typestatic final int
Number Format type -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionProvides for the clone operation.freeze()
Freezes the object.getBreakIterator
(int type) Get a copy of the break iterator for the specified type according to the settings.Get a copy of the calendar according to the settings.Get a copy of the collator according to the settings.Get a copy of the currency computed according to the settings.getDateFormat
(int dateStyle, int timeStyle) Gets a date format according to the current settings.getDisplayName
(String id, int type) Get the display name for an ID: language, script, territory, currency, timezone...getLocale
(int index) Convenience function for getting the locales in priority orderGet a copy of the language/locale priority listgetNumberFormat
(int style) Gets a number format according to the current settings.getResourceBundle
(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.getResourceBundle
(String baseName, 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.Gets the territory setting.Get the timezone.protected BreakIterator
guessBreakIterator
(int type) This function can be overridden by subclasses to use different heuristics.protected Calendar
This function can be overridden by subclasses to use different heuristics.protected Collator
This function can be overridden by subclasses to use different heuristics.protected Currency
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.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 String
This function can be overridden by subclasses to use different heuristics.protected TimeZone
This function can be overridden by subclasses to use different heuristics.boolean
isFrozen()
Determines whether the object has been frozen or not.processLocales
(List<ULocale> inputLocales) Process a language/locale priority list specified viasetLocales
.reset()
Restore the object to the initial state.setBreakIterator
(int type, BreakIterator iterator) Explicitly set the break iterator for this object.setCalendar
(Calendar calendar) Sets the calendar.setCollator
(Collator collator) Explicitly set the collator for this object.setCurrency
(Currency currency) Sets the currency code.setDateFormat
(int dateStyle, int timeStyle, DateFormat format) Set an explicit date format.Convenience routine for setting the language/locale priority list from a single locale/language.setLocales
(ULocale[] uLocales) Convenience routine for setting the language/locale priority list from an array.setLocales
(String acceptLanguageString) Convenience routine for setting the locale priority list from an Accept-Language string.setLocales
(List<ULocale> inputLocales) Sets the language/locale priority list.setNumberFormat
(int style, NumberFormat format) Sets a number format explicitly.setTerritory
(String territory) Sets the territory, which is a valid territory according to for RFC 3066 (or successor).setTimeZone
(TimeZone timezone) Sets the timezone ID.
-
Field Details
-
NF_NUMBER
public static final int NF_NUMBERNumber Format type- See Also:
-
NF_CURRENCY
public static final int NF_CURRENCYNumber Format type- See Also:
-
NF_PERCENT
public static final int NF_PERCENTNumber Format type- See Also:
-
NF_SCIENTIFIC
public static final int NF_SCIENTIFICNumber Format type- See Also:
-
NF_INTEGER
public static final int NF_INTEGERNumber Format type- See Also:
-
DF_FULL
public static final int DF_FULLDate Format type- See Also:
-
DF_LONG
public static final int DF_LONGDate Format type- See Also:
-
DF_MEDIUM
public static final int DF_MEDIUMDate Format type- See Also:
-
DF_SHORT
public static final int DF_SHORTDate Format type- See Also:
-
DF_NONE
public static final int DF_NONEDate Format type- See Also:
-
ID_LOCALE
public static final int ID_LOCALEFor selecting a choice of display names- See Also:
-
ID_LANGUAGE
public static final int ID_LANGUAGEFor selecting a choice of display names- See Also:
-
ID_SCRIPT
public static final int ID_SCRIPTFor selecting a choice of display names- See Also:
-
ID_TERRITORY
public static final int ID_TERRITORYFor selecting a choice of display names- See Also:
-
ID_VARIANT
public static final int ID_VARIANTFor selecting a choice of display names- See Also:
-
ID_KEYWORD
public static final int ID_KEYWORDFor selecting a choice of display names- See Also:
-
ID_KEYWORD_VALUE
public static final int ID_KEYWORD_VALUEFor selecting a choice of display names- See Also:
-
ID_CURRENCY
public static final int ID_CURRENCYFor selecting a choice of display names- See Also:
-
ID_CURRENCY_SYMBOL
public static final int ID_CURRENCY_SYMBOLFor selecting a choice of display names- See Also:
-
ID_TIMEZONE
public static final int ID_TIMEZONEFor selecting a choice of display names- See Also:
-
BI_CHARACTER
public static final int BI_CHARACTERBreak iterator type- See Also:
-
BI_WORD
public static final int BI_WORDBreak iterator type- See Also:
-
BI_LINE
public static final int BI_LINEBreak iterator type- See Also:
-
BI_SENTENCE
public static final int BI_SENTENCEBreak iterator type- See Also:
-
BI_TITLE
public static final int BI_TITLEBreak iterator type- See Also:
-
-
Constructor Details
-
GlobalizationPreferences
public GlobalizationPreferences()Default constructor
-
-
Method Details
-
setLocales
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
Get a copy of the language/locale priority list- Returns:
- a copy of the language/locale priority list.
-
getLocale
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
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:
-
setLocale
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
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:
-
getResourceBundle
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
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
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
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
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
Get a copy of the currency computed according to the settings.- Returns:
- currency code, explicit or implicit.
-
setCalendar
Sets the calendar. If this has not been set, uses default for territory.- Parameters:
calendar
- arbitrary calendar- Returns:
- this, for chaining
-
getCalendar
Get a copy of the calendar according to the settings.- Returns:
- calendar explicit or implicit.
-
setTimeZone
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
Get the timezone. It was either explicitly set, or is heuristically computed from other settings.- Returns:
- timezone, either implicitly or explicitly set
-
getCollator
Get a copy of the collator according to the settings.- Returns:
- collator explicit or implicit.
-
setCollator
Explicitly set the collator for this object.- Parameters:
collator
- The collator object to be passed.- Returns:
- this, for chaining
-
getBreakIterator
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
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
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
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
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
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
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
Restore the object to the initial state.- Returns:
- this, for chaining
-
processLocales
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
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
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
This function can be overridden by subclasses to use different heuristics. -
guessCurrency
This function can be overridden by subclasses to use different heuristics -
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
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
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
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
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. -
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
Description copied from interface:Freezable
Freezes the object.- Specified by:
freeze
in interfaceFreezable<GlobalizationPreferences>
- Returns:
- the object itself.
-
cloneAsThawed
Description copied from interface:Freezable
Provides for the clone operation. Any clone is initially unfrozen.- Specified by:
cloneAsThawed
in interfaceFreezable<GlobalizationPreferences>
-