-
Notifications
You must be signed in to change notification settings - Fork 730
Expand file tree
/
Copy pathLocaleUtils.kt
More file actions
122 lines (104 loc) · 4.58 KB
/
LocaleUtils.kt
File metadata and controls
122 lines (104 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* WiFiAnalyzer
* Copyright (C) 2015 - 2026 VREM Software Development <VREMSoftwareDevelopment@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package com.vrem.util
import java.util.Locale
import java.util.SortedMap
private val currentLocale: Locale get() = Locale.getDefault()
private val countryCodes: Set<String> = Locale.getISOCountries().toSet()
private val availableLocales: List<Locale> = Locale.getAvailableLocales().filter { countryCodes.contains(it.country) }
private val countriesLocales: SortedMap<String, Locale>
get() =
availableLocales
.associateBy { it.country.toCapitalize(currentLocale) }
.toSortedMap()
val BULGARIAN: Locale = Locale.forLanguageTag("bg")
val CHINESE: Locale = Locale.forLanguageTag("zh")
val CHINESE_SIMPLIFIED: Locale = Locale.forLanguageTag("zh-Hans")
val CHINESE_TRADITIONAL: Locale = Locale.forLanguageTag("zh-Hant")
val DUTCH: Locale = Locale.forLanguageTag("nl")
val ENGLISH: Locale = Locale.forLanguageTag("en")
val FRENCH: Locale = Locale.forLanguageTag("fr")
val GERMAN: Locale = Locale.forLanguageTag("de")
val GREEK: Locale = Locale.forLanguageTag("el")
val HUNGARIAN: Locale = Locale.forLanguageTag("hu")
val ITALIAN: Locale = Locale.forLanguageTag("it")
val JAPANESE: Locale = Locale.forLanguageTag("ja")
val POLISH: Locale = Locale.forLanguageTag("pl")
val PORTUGUESE_BRAZIL: Locale = Locale.forLanguageTag("pt-BR")
val PORTUGUESE_PORTUGAL: Locale = Locale.forLanguageTag("pt-PT")
val RUSSIAN: Locale = Locale.forLanguageTag("ru")
val SPANISH: Locale = Locale.forLanguageTag("es")
val TURKISH: Locale = Locale.forLanguageTag("tr")
val UKRAINIAN: Locale = Locale.forLanguageTag("uk")
val baseSupportedLocales: List<Locale> =
listOf(
BULGARIAN,
CHINESE_SIMPLIFIED,
CHINESE_TRADITIONAL,
DUTCH,
ENGLISH,
FRENCH,
GERMAN,
GREEK,
HUNGARIAN,
ITALIAN,
JAPANESE,
POLISH,
PORTUGUESE_BRAZIL,
PORTUGUESE_PORTUGAL,
RUSSIAN,
SPANISH,
TURKISH,
UKRAINIAN,
)
fun findByCountryCode(countryCode: String): Locale =
availableLocales.firstOrNull { countryCode.uppercase(Locale.ROOT) == it.country }
?: currentLocale
fun allCountries(): List<Locale> = countriesLocales.values.toList()
fun supportedLanguages(): List<Locale> = (baseSupportedLocales + currentLocale).distinct()
fun supportedLanguageTags(): List<String> = listOf("") + baseSupportedLocales.map { it.toLanguageTag() }
private fun normalizeLanguageTag(languageTag: String): String = languageTag.replace('_', '-').trim()
private val chineseCountryToLocale: Map<String, Locale> =
mapOf(
"CN" to CHINESE_SIMPLIFIED,
"SG" to CHINESE_SIMPLIFIED,
"TW" to CHINESE_TRADITIONAL,
"HK" to CHINESE_TRADITIONAL,
"MO" to CHINESE_TRADITIONAL,
)
fun findByLanguageTag(languageTag: String): Locale {
val normalizedLanguageTag = normalizeLanguageTag(languageTag)
if (normalizedLanguageTag.isEmpty()) return currentLocale
return findSupportedLocale(Locale.forLanguageTag(normalizedLanguageTag))
}
fun findSupportedLocale(target: Locale): Locale {
if (target.language.isEmpty()) return currentLocale
if (target.language == "zh" && target.script.isEmpty()) {
if (target.country.isEmpty()) return CHINESE
return chineseCountryToLocale[target.country] ?: CHINESE
}
return baseSupportedLocales.find { it == target }
?: baseSupportedLocales.find { it.language == target.language && it.script == target.script }
?: baseSupportedLocales.find { it.language == target.language && it.country == target.country }
?: baseSupportedLocales.find { it.language == target.language }
?: currentLocale
}
fun currentCountryCode(): String = currentLocale.country
fun currentLanguageTag(): String = currentLocale.toLanguageTag()
fun toLanguageTag(locale: Locale): String = locale.toLanguageTag()
fun Locale.toSupportedLocaleTag(): String = findSupportedLocale(this).toLanguageTag()