1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.compose.ui.text.intl
18 
19 import androidx.compose.runtime.Immutable
20 import androidx.compose.ui.text.SpanStyle
21 import androidx.compose.ui.text.TextStyle
22 
23 /**
24  * A `Locale` object represents a specific geographical, political, or cultural region. An operation
25  * that requires a `Locale` to perform its task is called _locale-sensitive_ and uses the `Locale`
26  * to tailor information for the user. For example, displaying a number is a locale-sensitive
27  * operation— the number should be formatted according to the customs and conventions of the user's
28  * native country, region, or culture.
29  *
30  * @param platformLocale Platform specific Locale object that provides the actual values for the
31  *   members of this class.
32  * @see TextStyle
33  * @see SpanStyle
34  */
35 @Immutable
36 class Locale internal constructor(val platformLocale: PlatformLocale) {
37     companion object {
38         /** Returns a [Locale] object which represents current locale */
39         val current: Locale
40             get() = platformLocaleDelegate.current[0]
41     }
42 
43     /**
44      * Create Locale object from a language tag.
45      *
46      * @param languageTag A [IETF BCP47](https://tools.ietf.org/html/bcp47) compliant language tag.
47      * @return a locale object
48      */
49     constructor(languageTag: String) : this(platformLocaleDelegate.parseLanguageTag(languageTag))
50 
51     /** The ISO 639 compliant language code. */
52     val language: String
53         get() = platformLocale.language
54 
55     /** The ISO 15924 compliant 4-letter script code. */
56     val script: String
57         get() = platformLocale.script
58 
59     /** The ISO 3166 compliant region code. */
60     val region: String
61         get() = platformLocale.region
62 
63     /**
64      * Returns a IETF BCP47 compliant language tag representation of this Locale.
65      *
66      * @return A IETF BCP47 compliant language tag.
67      */
toLanguageTagnull68     fun toLanguageTag(): String = platformLocale.getLanguageTag()
69 
70     override fun equals(other: Any?): Boolean {
71         if (other == null) return false
72         if (other !is Locale) return false
73         if (this === other) return true
74         return toLanguageTag() == other.toLanguageTag()
75     }
76 
77     // We don't use data class since we cannot offer copy function here.
hashCodenull78     override fun hashCode(): Int = toLanguageTag().hashCode()
79 
80     override fun toString(): String = toLanguageTag()
81 }
82