@@ -20,80 +20,103 @@ package com.vrem.util
2020import java.util.Locale
2121import java.util.SortedMap
2222
23- private object SyncAvoid {
24- val defaultLocale: Locale = Locale .getDefault()
25- val countryCodes: Set <String > = Locale .getISOCountries().toSet()
26- val availableLocales: List <Locale > = Locale .getAvailableLocales().filter { countryCodes.contains(it.country) }
27-
28- val countriesLocales: SortedMap <String , Locale > =
23+ private val currentLocale: Locale get() = Locale .getDefault()
24+ private val countryCodes: Set <String > = Locale .getISOCountries().toSet()
25+ private val availableLocales: List <Locale > = Locale .getAvailableLocales().filter { countryCodes.contains(it.country) }
26+ private val countriesLocales: SortedMap <String , Locale >
27+ get() =
2928 availableLocales
30- .associateBy { it.country.toCapitalize(Locale .getDefault() ) }
29+ .associateBy { it.country.toCapitalize(currentLocale ) }
3130 .toSortedMap()
32- val supportedLocales: List <Locale > =
33- setOf (
34- BULGARIAN ,
35- DUTCH ,
36- GREEK ,
37- HUNGARIAN ,
38- Locale .SIMPLIFIED_CHINESE ,
39- Locale .TRADITIONAL_CHINESE ,
40- Locale .ENGLISH ,
41- Locale .FRENCH ,
42- Locale .GERMAN ,
43- Locale .ITALIAN ,
44- Locale .JAPANESE ,
45- POLISH ,
46- PORTUGUESE_BRAZIL ,
47- PORTUGUESE_PORTUGAL ,
48- SPANISH ,
49- RUSSIAN ,
50- TURKISH ,
51- UKRAINIAN ,
52- defaultLocale,
53- ).toList()
54- }
5531
5632val BULGARIAN : Locale = Locale .forLanguageTag(" bg" )
33+ val CHINESE : Locale = Locale .forLanguageTag(" zh" )
34+ val CHINESE_SIMPLIFIED : Locale = Locale .forLanguageTag(" zh-Hans" )
35+ val CHINESE_TRADITIONAL : Locale = Locale .forLanguageTag(" zh-Hant" )
5736val DUTCH : Locale = Locale .forLanguageTag(" nl" )
37+ val ENGLISH : Locale = Locale .forLanguageTag(" en" )
38+ val FRENCH : Locale = Locale .forLanguageTag(" fr" )
39+ val GERMAN : Locale = Locale .forLanguageTag(" de" )
5840val GREEK : Locale = Locale .forLanguageTag(" el" )
5941val HUNGARIAN : Locale = Locale .forLanguageTag(" hu" )
42+ val ITALIAN : Locale = Locale .forLanguageTag(" it" )
43+ val JAPANESE : Locale = Locale .forLanguageTag(" ja" )
6044val POLISH : Locale = Locale .forLanguageTag(" pl" )
61- val PORTUGUESE_PORTUGAL : Locale = Locale .forLanguageTag(" pt-PT" )
6245val PORTUGUESE_BRAZIL : Locale = Locale .forLanguageTag(" pt-BR" )
63- val SPANISH : Locale = Locale .forLanguageTag(" es " )
46+ val PORTUGUESE_PORTUGAL : Locale = Locale .forLanguageTag(" pt-PT " )
6447val RUSSIAN : Locale = Locale .forLanguageTag(" ru" )
48+ val SPANISH : Locale = Locale .forLanguageTag(" es" )
6549val TURKISH : Locale = Locale .forLanguageTag(" tr" )
6650val UKRAINIAN : Locale = Locale .forLanguageTag(" uk" )
6751
68- private const val SEPARATOR : String = " _"
52+ val baseSupportedLocales: List <Locale > =
53+ listOf (
54+ BULGARIAN ,
55+ CHINESE_SIMPLIFIED ,
56+ CHINESE_TRADITIONAL ,
57+ DUTCH ,
58+ ENGLISH ,
59+ FRENCH ,
60+ GERMAN ,
61+ GREEK ,
62+ HUNGARIAN ,
63+ ITALIAN ,
64+ JAPANESE ,
65+ POLISH ,
66+ PORTUGUESE_BRAZIL ,
67+ PORTUGUESE_PORTUGAL ,
68+ RUSSIAN ,
69+ SPANISH ,
70+ TURKISH ,
71+ UKRAINIAN ,
72+ )
6973
7074fun findByCountryCode (countryCode : String ): Locale =
71- SyncAvoid . availableLocales.firstOrNull { countryCode.toCapitalize (Locale .getDefault() ) == it.country }
72- ? : SyncAvoid .defaultLocale
75+ availableLocales.firstOrNull { countryCode.uppercase (Locale .ROOT ) == it.country }
76+ ? : currentLocale
7377
74- fun allCountries (): List <Locale > = SyncAvoid . countriesLocales.values.toList()
78+ fun allCountries (): List <Locale > = countriesLocales.values.toList()
7579
76- fun findByLanguageTag (languageTag : String ): Locale {
77- val languageTagPredicate: (Locale ) -> Boolean = {
78- val locale: Locale = fromLanguageTag(languageTag)
79- it.language == locale.language && it.country == locale.country
80- }
81- return SyncAvoid .supportedLocales.firstOrNull(languageTagPredicate) ? : SyncAvoid .defaultLocale
82- }
80+ fun supportedLanguages (): List <Locale > = (baseSupportedLocales + currentLocale).distinct()
8381
84- fun supportedLanguages (): List <Locale > = SyncAvoid .supportedLocales
82+ fun supportedLanguageTags (): List <String > = listOf ( " " ) + baseSupportedLocales.map { it.toLanguageTag() }
8583
86- fun defaultCountryCode ( ): String = SyncAvoid .defaultLocale.country
84+ private fun normalizeLanguageTag ( languageTag : String ): String = languageTag.replace( ' _ ' , ' - ' ).trim()
8785
88- fun defaultLanguageTag (): String = toLanguageTag(SyncAvoid .defaultLocale)
86+ private val chineseCountryToLocale: Map <String , Locale > =
87+ mapOf (
88+ " CN" to CHINESE_SIMPLIFIED ,
89+ " SG" to CHINESE_SIMPLIFIED ,
90+ " TW" to CHINESE_TRADITIONAL ,
91+ " HK" to CHINESE_TRADITIONAL ,
92+ " MO" to CHINESE_TRADITIONAL ,
93+ )
8994
90- fun toLanguageTag (locale : Locale ): String = locale.language + SEPARATOR + locale.country
95+ fun findByLanguageTag (languageTag : String ): Locale {
96+ val normalizedLanguageTag = normalizeLanguageTag(languageTag)
97+ if (normalizedLanguageTag.isEmpty()) return currentLocale
98+ return findSupportedLocale(Locale .forLanguageTag(normalizedLanguageTag))
99+ }
91100
92- private fun fromLanguageTag ( languageTag : String ): Locale {
93- val codes : Array < String > = languageTag.split( SEPARATOR ).toTypedArray()
94- return when (codes.size) {
95- 1 -> Locale .forLanguageTag(codes[ 0 ])
96- 2 -> Locale .forLanguageTag( " ${codes[ 0 ]} - ${codes[ 1 ].toCapitalize( Locale .getDefault ())} " )
97- else -> SyncAvoid .defaultLocale
101+ fun findSupportedLocale ( target : Locale ): Locale {
102+ if (target.language.isEmpty()) return currentLocale
103+
104+ if (target.language == " zh " && target.script.isEmpty()) {
105+ if (target.country.isEmpty ()) return CHINESE
106+ return chineseCountryToLocale[target.country] ? : CHINESE
98107 }
108+
109+ return baseSupportedLocales.find { it == target }
110+ ? : baseSupportedLocales.find { it.language == target.language && it.script == target.script }
111+ ? : baseSupportedLocales.find { it.language == target.language && it.country == target.country }
112+ ? : baseSupportedLocales.find { it.language == target.language }
113+ ? : currentLocale
99114}
115+
116+ fun currentCountryCode (): String = currentLocale.country
117+
118+ fun currentLanguageTag (): String = currentLocale.toLanguageTag()
119+
120+ fun toLanguageTag (locale : Locale ): String = locale.toLanguageTag()
121+
122+ fun Locale.toSupportedLocaleTag (): String = findSupportedLocale(this ).toLanguageTag()
0 commit comments