-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLanguage.kt
More file actions
62 lines (56 loc) · 1.69 KB
/
Language.kt
File metadata and controls
62 lines (56 loc) · 1.69 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
package to.bitkit.models
import kotlinx.serialization.Serializable
import java.util.Locale
@Serializable
enum class Language(
val displayName: String,
val languageCode: String,
val countryCode: String? = null,
val isSystemDefault: Boolean = false,
) {
SYSTEM_DEFAULT(
displayName = "System Settings",
languageCode = "system",
countryCode = null,
isSystemDefault = true
),
ARABIC("العربية", "ar"),
CATALAN("Català", "ca"),
CZECH("Čeština", "cs"),
DUTCH("Nederlands", "nl"),
ENGLISH("English", "en", "US"),
FRENCH("Français", "fr", "FR"),
GERMAN("Deutsch", "de"),
GREEK("Ελληνικά", "el"),
ITALIAN("Italiano", "it"),
POLISH("Polski", "pl"),
PORTUGUESE("Português", "pt", "BR"),
RUSSIAN("Русский", "ru"),
SPANISH("Español", "es", "ES"),
SPANISH_LATIN_AMERICA("Español (Latinoamérica)", "es", "419");
companion object {
fun fromLanguageCode(languageCode: String, countryCode: String? = null): Language? {
return entries.find { language ->
language.languageCode == languageCode &&
(countryCode == null || language.countryCode == countryCode)
}
}
fun fromLocale(locale: Locale): Language? {
return fromLanguageCode(locale.language, locale.country.ifEmpty { null })
}
}
}
fun Language.getLanguageTag(): String {
return if (isSystemDefault) {
""
} else {
if (countryCode != null) {
"$languageCode-$countryCode"
} else {
languageCode
}
}
}
fun Locale.toLanguage(): Language? {
return Language.fromLocale(this)
}