public class LocalePriorityList extends Object implements Iterable<ULocale>
In theory, Accept-Language indicates the relative 'quality' of each item, but in practice, all of the browsers just take an ordered list, like "en, fr, de", and synthesize arbitrary quality values that put these in the right order, like: "en, fr;q=0.7, de;q=0.3". The quality values in these de facto semantics thus have nothing to do with the relative qualities of the original. Accept-Language also doesn't specify the interpretation of multiple instances, eg what "en, fr, en;q=.5" means.
There are various ways to build a LocalePriorityList, such as using the following equivalent patterns:
list = LocalePriorityList.add("af, en, fr;q=0.9").build(); list2 = LocalePriorityList .add(ULocale.forString("af")) .add(ULocale.ENGLISH) .add(ULocale.FRENCH, 0.9d) .build();When the list is built, the internal values are sorted in descending order by weight, and then by input order. That is, if two languages/locales have the same weight, the first one in the original order comes first. If exactly the same language tag appears multiple times, the last one wins.
There are two options when building. If preserveWeights are on, then "de;q=0.3, ja;q=0.3, en, fr;q=0.7, de " would result in the following:
en;q=1.0 de;q=1.0 fr;q=0.7 ja;q=0.3If it is off (the default), then all weights are reset to 1.0 after reordering. This is to match the effect of the Accept-Language semantics as used in browsers, and results in the following:
en;q=1.0 de;q=1.0 fr;q=1.0 ja;q=1.0
Modifier and Type | Class and Description |
---|---|
static class |
LocalePriorityList.Builder
Class used for building LocalePriorityLists.
|
Modifier and Type | Method and Description |
---|---|
static LocalePriorityList.Builder |
add(LocalePriorityList list)
Creates a Builder and adds locales with weights.
|
static LocalePriorityList.Builder |
add(String acceptLanguageString)
Creates a Builder, parses the RFC 2616 string, and adds locales with weights accordingly.
|
static LocalePriorityList.Builder |
add(ULocale... locales)
Creates a Builder and adds locales, each with weight 1.0.
|
static LocalePriorityList.Builder |
add(ULocale locale,
double weight)
Creates a Builder and adds a locale with a specified weight.
|
boolean |
equals(Object o) |
Set<ULocale> |
getULocales()
Returns the locales as an immutable Set view.
|
Double |
getWeight(ULocale locale)
Returns the weight for a given language/locale, or null if there is none.
|
int |
hashCode() |
Iterator<ULocale> |
iterator() |
String |
toString() |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
forEach, spliterator
public static LocalePriorityList.Builder add(ULocale... locales)
locales
- locales/languages to be addedpublic static LocalePriorityList.Builder add(ULocale locale, double weight)
locale
- locale/language to be addedweight
- value from 0.0 to 1.0public static LocalePriorityList.Builder add(LocalePriorityList list)
list
- list of locales with weightspublic static LocalePriorityList.Builder add(String acceptLanguageString)
acceptLanguageString
- String in RFC 2616 format (leniently parsed)public Double getWeight(ULocale locale)
locale
- to get weight ofpublic Set<ULocale> getULocales()
Copyright © 2016 Unicode, Inc. and others.