# Locale.LanguageRange

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

# LanguageRange

```
class LanguageRange
```

|---|------------------------------------------------------------------------------------------------------------------|
| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)                                   ||
| ↳ | [java.util.Locale.LanguageRange](https://developer.android.com/reference/kotlin/java/util/Locale.LanguageRange#) |

This class expresses a*Language Range* defined in[RFC 4647 Matching of Language Tags](https://tools.ietf.org/html/rfc4647). A language range is an identifier which is used to select language tag(s) meeting specific requirements by using the mechanisms described in java.util.Locale#. A list which represents a user's preferences and consists of language ranges is called a*Language Priority List*.

There are two types of language ranges: basic and extended. In RFC 4647, the syntax of language ranges is expressed in[ABNF](https://tools.ietf.org/html/rfc4234)as follows:  

```kotlin
basic-language-range    = (1*8ALPHA *("-" 1*8alphanum)) / "*"
      extended-language-range = (1*8ALPHA / "*")
                                *("-" (1*8alphanum / "*"))
      alphanum                = ALPHA / DIGIT
  
```
For example,`"en"`(English),`"ja-JP"`(Japanese, Japan),`"*"`(special language range which matches any language tag) are basic language ranges, whereas`"*-CH"`(any languages, Switzerland),`"es-*"`(Spanish, any regions), and`"zh-Hant-*"`(Traditional Chinese, any regions) are extended language ranges.

## Summary

|                                                                                                                            Constants                                                                                                                            ||
|----------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| static[Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html) | [MAX_WEIGHT](https://developer.android.com/reference/kotlin/java/util/Locale.LanguageRange#MAX_WEIGHT:kotlin.Double) A constant holding the maximum value of weight, 1. |
| static[Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html) | [MIN_WEIGHT](https://developer.android.com/reference/kotlin/java/util/Locale.LanguageRange#MIN_WEIGHT:kotlin.Double) A constant holding the minimum value of weight, 0. |

|                                                                                                                                                                                            Public constructors                                                                                                                                                                                             ||
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| [LanguageRange](https://developer.android.com/reference/kotlin/java/util/Locale.LanguageRange#LanguageRange(kotlin.String))`(`range:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`)` Constructs a`LanguageRange`using the given`range`.                                                                                                                           |
| [LanguageRange](https://developer.android.com/reference/kotlin/java/util/Locale.LanguageRange#LanguageRange(kotlin.String,%20kotlin.Double))`(`range:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`, `weight:` `[Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html)`)` Constructs a`LanguageRange`using the given`range`and`weight`. |

|                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               Public methods                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               ||
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html)                                                                                                                                       | [equals](https://developer.android.com/reference/kotlin/java/util/Locale.LanguageRange#equals(kotlin.Any))`(`other:` `[Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)?`)` Compares this object to the specified object.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)                                                                                                                                         | [getRange](https://developer.android.com/reference/kotlin/java/util/Locale.LanguageRange#getRange())`()` Returns the language range of this`LanguageRange`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html)                                                                                                                                         | [getWeight](https://developer.android.com/reference/kotlin/java/util/Locale.LanguageRange#getWeight())`()` Returns the weight of this`LanguageRange`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)                                                                                                                                               | [hashCode](https://developer.android.com/reference/kotlin/java/util/Locale.LanguageRange#hashCode())`()` {@return a hash code value for this object}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| static[MutableList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/index.html)\<[Locale.LanguageRange](https://developer.android.com/reference/kotlin/java/util/Locale.LanguageRange#)!\> | [mapEquivalents](https://developer.android.com/reference/kotlin/java/util/Locale.LanguageRange#mapEquivalents(kotlin.collections.MutableList,%20kotlin.collections.MutableMap))`(`priorityList:` `[MutableList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/index.html)<[Locale.LanguageRange](https://developer.android.com/reference/kotlin/java/util/Locale.LanguageRange#)!>`, `map:` `[MutableMap](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/index.html)<[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)!,` `[MutableList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/index.html)<[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)!>!>`)` Generates a new customized Language Priority List using the given`priorityList`and`map`. |
| static[MutableList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/index.html)\<[Locale.LanguageRange](https://developer.android.com/reference/kotlin/java/util/Locale.LanguageRange#)!\> | [parse](https://developer.android.com/reference/kotlin/java/util/Locale.LanguageRange#parse(kotlin.String))`(`ranges:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`)` Parses the given`ranges`to generate a Language Priority List.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| static[MutableList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/index.html)\<[Locale.LanguageRange](https://developer.android.com/reference/kotlin/java/util/Locale.LanguageRange#)!\> | [parse](https://developer.android.com/reference/kotlin/java/util/Locale.LanguageRange#parse(kotlin.String,%20kotlin.collections.MutableMap))`(`ranges:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`, `map:` `[MutableMap](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/index.html)<[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)!,` `[MutableList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/index.html)<[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)!>!>`)` Parses the given`ranges`to generate a Language Priority List, and then customizes the list using the given`map`.                                                                                                                                                  |
| [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)                                                                                                                                         | [toString](https://developer.android.com/reference/kotlin/java/util/Locale.LanguageRange#toString())`()` Returns an informative string representation of this`LanguageRange`object, consisting of language range and weight if the range is weighted and the weight is less than the max weight.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |

## Constants

### MAX_WEIGHT

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
static val MAX_WEIGHT: Double
```

A constant holding the maximum value of weight, 1.0, which indicates that the language range is a good fit for the user.  

    Value: 1.0

### MIN_WEIGHT

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
static val MIN_WEIGHT: Double
```

A constant holding the minimum value of weight, 0.0, which indicates that the language range is not a good fit for the user.  

    Value: 0.0

## Public constructors

### LanguageRange

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
LanguageRange(range: String)
```

Constructs a`LanguageRange`using the given`range`. Note that no validation is done against the IANA Language Subtag Registry at time of construction.

This is equivalent to`LanguageRange(range, MAX_WEIGHT)`.

|                                                 Parameters                                                 ||
|---------|---------------------------------------------------------------------------------------------------|
| `range` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):a language range |

|                                                              Exceptions                                                              ||
|--------------------------------------|------------------------------------------------------------------------------------------------|
| `java.lang.NullPointerException`     | if the given`range`is`null`                                                                    |
| `java.lang.IllegalArgumentException` | if the given`range`does not comply with the syntax of the language range mentioned in RFC 4647 |

### LanguageRange

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
LanguageRange(
    range: String, 
    weight: Double)
```

Constructs a`LanguageRange`using the given`range`and`weight`. Note that no validation is done against the IANA Language Subtag Registry at time of construction.

|                                                                  Parameters                                                                  ||
|----------|------------------------------------------------------------------------------------------------------------------------------------|
| `range`  | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):a language range                                  |
| `weight` | [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html):a weight value between`MIN_WEIGHT`and`MAX_WEIGHT` |

|                                                                                                   Exceptions                                                                                                    ||
|--------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `java.lang.NullPointerException`     | if the given`range`is`null`                                                                                                                                               |
| `java.lang.IllegalArgumentException` | if the given`range`does not comply with the syntax of the language range mentioned in RFC 4647 or if the given`weight`is less than`MIN_WEIGHT`or greater than`MAX_WEIGHT` |

## Public methods

### equals

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
fun equals(other: Any?): Boolean
```

Compares this object to the specified object. The result is true if and only if the argument is not`null`and is a`LanguageRange`object that contains the same`range`and`weight`values as this object.

|            Parameters             ||
|-------|----------------------------|
| `obj` | the object to compare with |

|                                                                                  Return                                                                                  ||
|------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------|
| [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | `true`if this object's`range`and`weight`are the same as the`obj`'s;`false`otherwise. |

### getRange

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
fun getRange(): String
```

Returns the language range of this`LanguageRange`.

|                                                Return                                                 ||
|----------------------------------------------------------------------------------|---------------------|
| [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html) | the language range. |

### getWeight

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
fun getWeight(): Double
```

Returns the weight of this`LanguageRange`.

|                                               Return                                                ||
|----------------------------------------------------------------------------------|-------------------|
| [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html) | the weight value. |

### hashCode

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
fun hashCode(): Int
```

{@return a hash code value for this object}

|                                                     Return                                                     ||
|----------------------------------------------------------------------------|------------------------------------|
| [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | a hash code value for this object. |

### mapEquivalents

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
static fun mapEquivalents(
    priorityList: MutableList<Locale.LanguageRange!>, 
    map: MutableMap<String!, MutableList<String!>!>
): MutableList<Locale.LanguageRange!>
```

Generates a new customized Language Priority List using the given`priorityList`and`map`. If the given`map`is empty, this method returns a copy of the given`priorityList`.

In the map, a key represents a language range whereas a value is a list of equivalents of it.`'*'`cannot be used in the map. Each equivalent language range has the same weight value as its original language range.  

```kotlin
An example of map:
     <b>Key</b><b>Value</b>"zh" (Chinese)                 "zh",
                                      "zh-Hans"(Simplified Chinese)
       "zh-HK" (Chinese, Hong Kong)   "zh-HK"
       "zh-TW" (Chinese, Taiwan)      "zh-TW"
  
```
The customization is performed after modification using the IANA Language Subtag Registry.

For example, if a user's Language Priority List consists of five language ranges (`"zh"`,`"zh-CN"`,`"en"`,`"zh-TW"`, and`"zh-HK"`), the newly generated Language Priority List which is customized using the above map example will consists of`"zh"`,`"zh-Hans"`,`"zh-CN"`,`"zh-Hans-CN"`,`"en"`,`"zh-TW"`, and`"zh-HK"`.

`"zh-HK"`and`"zh-TW"`aren't converted to`"zh-Hans-HK"`nor`"zh-Hans-TW"`even if they are included in the Language Priority List. In this example, mapping is used to clearly distinguish Simplified Chinese and Traditional Chinese.

If the`"zh"`-to-`"zh"`mapping isn't included in the map, a simple replacement will be performed and the customized list won't include`"zh"`and`"zh-CN"`.

|                                                                                                                                                                                                                             Parameters                                                                                                                                                                                                                             ||
|----------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `priorityList` | [MutableList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/index.html)\<[Locale.LanguageRange](https://developer.android.com/reference/kotlin/java/util/Locale.LanguageRange#)!\>:user's Language Priority List                                                                                                                                                                                                   |
| `map`          | [MutableMap](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/index.html)\<[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)!,[MutableList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/index.html)\<[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)!\>!\>:a map containing information to customize language ranges |

|                                                                                                                                           Return                                                                                                                                           ||
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------|
| [MutableList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/index.html)<[Locale.LanguageRange](https://developer.android.com/reference/kotlin/java/util/Locale.LanguageRange#)!> | a new Language Priority List with customization. The list is modifiable. |

|                         Exceptions                         ||
|----------------------------------|--------------------------|
| `java.lang.NullPointerException` | if`priorityList`is`null` |

**See Also**

- [#parse(String, Map)](https://developer.android.com/reference/kotlin/java/util/Locale.LanguageRange#parse(kotlin.String,%20kotlin.collections.MutableMap))  

### parse

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
static fun parse(ranges: String): MutableList<Locale.LanguageRange!>
```

Parses the given`ranges`to generate a Language Priority List.

This method performs a syntactic check for each language range in the given`ranges`but doesn't do validation using the IANA Language Subtag Registry.

The`ranges`to be given can take one of the following forms:  

```kotlin
"Accept-Language: ja,en;q=0.4"  (weighted list with Accept-Language prefix)
    "ja,en;q=0.4"                   (weighted list)
    "ja,en"                         (prioritized list)
  
```
In a weighted list, each language range is given a weight value. The weight value is identical to the "quality value" in[RFC 2616](https://tools.ietf.org/html/rfc2616), and it expresses how much the user prefers the language. A weight value is specified after a corresponding language range followed by`";q="`, and the default weight value is`MAX_WEIGHT`when it is omitted.

Unlike a weighted list, language ranges in a prioritized list are sorted in the descending order based on its priority. The first language range has the highest priority and meets the user's preference most.

In either case, language ranges are sorted in descending order in the Language Priority List based on priority or weight. If a language range appears in the given`ranges`more than once, only the first one is included on the Language Priority List.

The returned list consists of language ranges from the given`ranges`and their equivalents found in the IANA Language Subtag Registry. For example, if the given`ranges`is`"Accept-Language: iw,en-us;q=0.7,en;q=0.3"`, the elements in the list to be returned are:  

```kotlin
<b>Range</b><b>Weight</b>"iw" (older tag for Hebrew)             1.0
     "he" (new preferred code for Hebrew)    1.0
     "en-us" (English, United States)        0.7
     "en" (English)                          0.3
  
```
Two language ranges,`"iw"`and`"he"`, have the same highest priority in the list. By adding`"he"`to the user's Language Priority List, locale-matching method can find Hebrew as a matching locale (or language tag) even if the application or system offers only`"he"`as a supported locale (or language tag).

|                                                                                                                               Parameters                                                                                                                                ||
|----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `ranges` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):a list of comma-separated language ranges or a list of language ranges in the form of the "Accept-Language" header defined in[RFC 2616](https://tools.ietf.org/html/rfc2616) |

|                                                                                                                                                                                      Return                                                                                                                                                                                      ||
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [MutableList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/index.html)<[Locale.LanguageRange](https://developer.android.com/reference/kotlin/java/util/Locale.LanguageRange#)!> | a Language Priority List consisting of language ranges included in the given`ranges`and their equivalent language ranges if available. The list is modifiable. |

|                                                  Exceptions                                                   ||
|--------------------------------------|-------------------------------------------------------------------------|
| `java.lang.NullPointerException`     | if`ranges`is null                                                       |
| `java.lang.IllegalArgumentException` | if a language range or a weight found in the given`ranges`is ill-formed |

### parse

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
static fun parse(
    ranges: String, 
    map: MutableMap<String!, MutableList<String!>!>
): MutableList<Locale.LanguageRange!>
```

Parses the given`ranges`to generate a Language Priority List, and then customizes the list using the given`map`. This method is equivalent to`mapEquivalents(parse(ranges), map)`.

|                                                                                                                                                                                                                          Parameters                                                                                                                                                                                                                          ||
|----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `ranges` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):a list of comma-separated language ranges or a list of language ranges in the form of the "Accept-Language" header defined in[RFC 2616](https://tools.ietf.org/html/rfc2616)                                                                                                                                                                                      |
| `map`    | [MutableMap](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/index.html)\<[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)!,[MutableList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/index.html)\<[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)!\>!\>:a map containing information to customize language ranges |

|                                                                                                                                         Return                                                                                                                                         ||
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------|
| [MutableList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/index.html)<[Locale.LanguageRange](https://developer.android.com/reference/kotlin/java/util/Locale.LanguageRange#)!> | a Language Priority List with customization. The list is modifiable. |

|                                                  Exceptions                                                   ||
|--------------------------------------|-------------------------------------------------------------------------|
| `java.lang.NullPointerException`     | if`ranges`is null                                                       |
| `java.lang.IllegalArgumentException` | if a language range or a weight found in the given`ranges`is ill-formed |

**See Also**

- [#parse(String)](https://developer.android.com/reference/kotlin/java/util/Locale.LanguageRange#parse(kotlin.String))
- [#mapEquivalents](https://developer.android.com/reference/kotlin/java/util/Locale.LanguageRange#mapEquivalents(kotlin.collections.MutableList,%20kotlin.collections.MutableMap))  

### toString

Added in[API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
fun toString(): String
```

Returns an informative string representation of this`LanguageRange`object, consisting of language range and weight if the range is weighted and the weight is less than the max weight.

|                                                                 Return                                                                  ||
|----------------------------------------------------------------------------------|-------------------------------------------------------|
| [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html) | a string representation of this`LanguageRange`object. |