This commit is contained in:
Tlaster 2020-04-21 14:59:15 +08:00
parent f0d3865141
commit 412891fc09
1 changed files with 23 additions and 6 deletions

View File

@ -20,16 +20,33 @@
package androidx.core.os
import android.annotation.SuppressLint
import android.os.Build
import java.util.*
@SuppressLint("RestrictedApi")
object LocaleHelperAccessor {
fun forLanguageTag(str: String): Locale = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Locale.forLanguageTag(str)
fun forLanguageTag(str: String): Locale {
if (str.contains("-")) {
val args = str.split("-").dropLastWhile { it.isEmpty() }.toTypedArray()
if (args.size > 2) {
return Locale(args[0], args[1], args[2])
} else if (args.size > 1) {
return Locale(args[0], args[1])
} else if (args.size == 1) {
return Locale(args[0])
}
} else if (str.contains("_")) {
val args = str.split("_").dropLastWhile { it.isEmpty() }.toTypedArray()
if (args.size > 2) {
return Locale(args[0], args[1], args[2])
} else if (args.size > 1) {
return Locale(args[0], args[1])
} else if (args.size == 1) {
return Locale(args[0])
}
} else {
Locale(str)// TODO: Dose it work?
// TODO("VERSION.SDK_INT < LOLLIPOP")
return Locale(str)
}
throw IllegalArgumentException("Can not parse language tag: [$str]")
}
}