fix language names not shown by their native names

This commit is contained in:
AmirHossein Abdolmotallebi 2024-11-04 01:05:21 +03:30
parent 9d209cdec6
commit c86b76e3b3
3 changed files with 200 additions and 41 deletions

View File

@ -0,0 +1,172 @@
package ir.amirab.util.compose.localizationmanager
import java.util.Locale
interface ILanguageNameProvider {
fun getNativeName(myLocale: MyLocale): String
}
object LanguageNameProvider : ILanguageNameProvider {
private val list = mapOf(
"af" to "Afrikaans",
"ak" to "Akan",
"am" to "አማርኛ",
"ar" to "العربية",
"as" to "অসমীয়া",
"az" to "azərbaycan",
"be" to "беларуская",
"bg" to "български",
"bm" to "bamanakan",
"bn" to "বাংলা",
"bo" to "བོད་སྐད་",
"br" to "brezhoneg",
"bs" to "bosanski",
"ca" to "català",
"cs" to "čeština",
"cy" to "Cymraeg",
"da" to "dansk",
"de" to "Deutsch",
"de_AT" to "Österreichisches Deutsch",
"de_CH" to "Schweizer Hochdeutsch",
"dz" to "རྫོང་ཁ",
"ee" to "eʋegbe",
"el" to "Ελληνικά",
"en" to "English",
"eo" to "esperanto",
"es" to "español",
"et" to "eesti",
"eu" to "euskara",
"fa" to "فارسی",
"ff" to "Pulaar",
"fi" to "suomi",
"fo" to "føroyskt",
"fr" to "français",
"fr_CA" to "français canadien",
"fr_CH" to "français suisse",
"fy" to "West-Frysk",
"ga" to "Gaeilge",
"gd" to "Gàidhlig",
"gl" to "galego",
"gu" to "ગુજરાતી",
"gv" to "Gaelg",
"ha" to "Hausa",
"he" to "עברית",
"hi" to "हिंदी",
"hr" to "hrvatski",
"hu" to "magyar",
"hy" to "հայերեն",
"id" to "Bahasa Indonesia",
"ig" to "Igbo",
"ii" to "ꆈꌠꉙ",
"is" to "íslenska",
"it" to "italiano",
"ja" to "日本語",
"ka" to "ქართული",
"ki" to "Gikuyu",
"kk" to "қазақ тілі",
"kl" to "kalaallisut",
"km" to "ខ្មែរ",
"kn" to "ಕನ್ನಡ",
"ko" to "한국어",
"ks" to "کٲشُر",
"kw" to "kernewek",
"ky" to "кыргызча",
"lb" to "Lëtzebuergesch",
"lg" to "Luganda",
"ln" to "lingála",
"lo" to "ລາວ",
"lt" to "lietuvių",
"lu" to "Tshiluba",
"lv" to "latviešu",
"mg" to "Malagasy",
"mk" to "македонски",
"ml" to "മലയാളം",
"mn" to "монгол",
"mr" to "मराठी",
"ms" to "Bahasa Melayu",
"mt" to "Malti",
"my" to "ဗမာ",
"nb" to "norsk bokmål",
"nd" to "isiNdebele",
"ne" to "नेपाली",
"nl" to "Nederlands",
"nl_BE" to "Vlaams",
"nn" to "nynorsk",
"no" to "norsk",
"om" to "Oromoo",
"or" to "ଓଡ଼ିଆ",
"os" to "ирон",
"pa" to "ਪੰਜਾਬੀ",
"pl" to "polski",
"ps" to "پښتو",
"pt" to "português",
"pt_BR" to "português do Brasil",
"qu" to "Runasimi",
"rm" to "rumantsch",
"rn" to "Ikirundi",
"ro" to "română",
"ro_MD" to "moldovenească",
"ru" to "русский",
"rw" to "Kinyarwanda",
"se" to "davvisámegiella",
"sg" to "Sängö",
"sh" to "Srpskohrvatski",
"si" to "සිංහල",
"sk" to "slovenčina",
"sl" to "slovenščina",
"sn" to "chiShona",
"so" to "Soomaali",
"sq" to "shqip",
"sr" to "српски",
"sv" to "svenska",
"sw" to "Kiswahili",
"ta" to "தமிழ்",
"te" to "తెలుగు",
"th" to "ไทย",
"ti" to "ትግርኛ",
"tl" to "Tagalog",
"to" to "lea fakatonga",
"tr" to "Türkçe",
"ug" to "ئۇيغۇرچە",
"uk" to "українська",
"ur" to "اردو",
"uz" to "oʻzbekcha",
"vi" to "Tiếng Việt",
"yi" to "ייִדיש",
"yo" to "Èdè Yorùbá",
"zh" to "中文",
"zh_CN" to "简体中文",
"zh_TW" to "繁體中文",
"zu" to "isiZulu",
)
override fun getNativeName(myLocale: MyLocale): String {
val languageCode = myLocale.languageCode
val countryCode = myLocale.countryCode
if (countryCode != null) {
list["${languageCode}_${countryCode}"]?.let {
return it
}
}
list[languageCode]?.let {
return it
}
return default(myLocale)
}
private fun default(myLocale: MyLocale): String {
return myLocale
.toLocale()
.let { it.getDisplayName(it) }
}
}
private fun MyLocale.toLocale(): Locale {
val language = languageCode
val country = countryCode
return if (country == null) {
Locale(language)
} else {
Locale(language, country)
}
}

View File

@ -108,30 +108,17 @@ class LanguageManager(
val fileSystem = FileSystem.RESOURCES
return fileSystem
.list(LOCALES_PATH.toPath())
.mapNotNull {
.mapNotNull { path ->
kotlin.runCatching {
if (fileSystem.metadataOrNull(it)?.isRegularFile == false) {
if (fileSystem.metadataOrNull(path)?.isRegularFile == false) {
return@runCatching null
}
val languageCodeAndCountryCode = extractLanguageCodeAndCountryCodeFromFileName(it.name)
val languageCodeAndCountryCode = extractLanguageCodeAndCountryCodeFromFileName(path.name)
?: return@runCatching null
val locale = if (languageCodeAndCountryCode.countryCode != null) {
Locale(languageCodeAndCountryCode.languageCode, languageCodeAndCountryCode.countryCode)
} else {
Locale(languageCodeAndCountryCode.languageCode)
}
locale to it
}.getOrNull()
}.let {
val localesWithPath = it
localesWithPath.map { (locale, path) ->
locale.toLanguageInfo(
languageCodeAndCountryCode.toLanguageInfo(
path = "$RESOURCE_PROTOCOL://$path",
hasMultiRegion = localesWithPath.count {
it.first.language == locale.language
} > 1
)
}
}.getOrNull()
}
}
@ -153,28 +140,27 @@ class LanguageManager(
}.buffer().inputStream()
}
private fun Locale.toLanguageInfo(
private fun MyLocale.toLanguageInfo(
path: String,
hasMultiRegion: Boolean,
): LanguageInfo {
return LanguageInfo(
languageCode = language,
countryCode = country,
nativeName = if (hasMultiRegion) getDisplayName(this) else getDisplayLanguage(this),
path = URI(path)
languageCode = languageCode,
countryCode = countryCode,
nativeName = LanguageNameProvider.getNativeName(this),
path = URI(path),
)
}
private val rtlLanguages = arrayOf("ar", "fa", "he", "iw", "ji", "ur", "yi")
private fun extractLanguageCodeAndCountryCodeFromFileName(name: String): LanguageCodeAndCountryCode? {
private fun extractLanguageCodeAndCountryCodeFromFileName(name: String): MyLocale? {
return name
.split(".")
.firstOrNull()
?.takeIf { it.isNotBlank() }
?.let {
it.split("_").run {
LanguageCodeAndCountryCode(
MyLocale(
languageCode = get(0),
countryCode = getOrNull(1)
)
@ -184,21 +170,6 @@ class LanguageManager(
}
}
private data class LanguageCodeAndCountryCode(
val languageCode: String,
val countryCode: String?,
) {
override fun toString(): String {
return buildString {
append(languageCode)
countryCode?.let {
append("_")
append(it)
}
}
}
}
interface MessageData {
fun getMessage(key: String): String?
}

View File

@ -0,0 +1,16 @@
package ir.amirab.util.compose.localizationmanager
data class MyLocale(
val languageCode: String,
val countryCode: String?,
) {
override fun toString(): String {
return buildString {
append(languageCode)
countryCode?.let {
append("_")
append(it)
}
}
}
}