@@ -62,8 +62,6 @@ val baseSupportedLocales: List<Locale> =
6262 UKRAINIAN ,
6363 ).toList()
6464
65- private const val SEPARATOR : String = " _"
66-
6765fun findByCountryCode (countryCode : String ): Locale =
6866 availableLocales.firstOrNull { countryCode.toCapitalize(Locale .getDefault()) == it.country }
6967 ? : currentLocale
@@ -73,25 +71,19 @@ fun allCountries(): List<Locale> = countriesLocales.values.toList()
7371fun supportedLanguages (): List <Locale > =
7472 (baseSupportedLocales + currentLocale).distinct()
7573
74+ fun supportedLanguageTags (): List <String > =
75+ listOf (" " ) + baseSupportedLocales.map { it.toLanguageTag() }
76+
7677fun findByLanguageTag (languageTag : String ): Locale {
77- val languageTagPredicate: (Locale ) -> Boolean = {
78- val locale: Locale = fromLanguageTag(languageTag)
78+ if (languageTag.isEmpty()) return currentLocale
79+ val locale = Locale .forLanguageTag(languageTag)
80+ return baseSupportedLocales.firstOrNull {
7981 it.language == locale.language && it.country == locale.country
80- }
81- return supportedLanguages().firstOrNull(languageTagPredicate) ? : currentLocale
82+ } ? : currentLocale
8283}
8384
8485fun currentCountryCode (): String = currentLocale.country
8586
86- fun currentLanguageTag (): String = toLanguageTag(currentLocale )
87+ fun currentLanguageTag (): String = currentLocale. toLanguageTag()
8788
88- fun toLanguageTag (locale : Locale ): String = locale.language + SEPARATOR + locale.country
89-
90- private fun fromLanguageTag (languageTag : String ): Locale {
91- val codes: Array <String > = languageTag.split(SEPARATOR ).toTypedArray()
92- return when (codes.size) {
93- 1 -> Locale .forLanguageTag(codes[0 ])
94- 2 -> Locale .forLanguageTag(" ${codes[0 ]} -${codes[1 ].toCapitalize(currentLocale)} " )
95- else -> currentLocale
96- }
97- }
89+ fun toLanguageTag (locale : Locale ): String = locale.toLanguageTag()
0 commit comments