parent
b5ab558f8b
commit
7b93bf6e25
@ -0,0 +1,51 @@ |
||||
package io.legado.app.utils |
||||
|
||||
import android.content.Context |
||||
import android.content.res.Configuration |
||||
import android.content.res.Resources |
||||
import android.os.Build |
||||
import io.legado.app.constant.PreferKey |
||||
import java.util.* |
||||
|
||||
|
||||
object LanguageUtils { |
||||
|
||||
/** |
||||
* 设置语言 |
||||
*/ |
||||
fun setConfiguration(context: Context): Context { |
||||
val resources: Resources = context.resources |
||||
val targetLocale: Locale = when (context.getPrefString(PreferKey.language)) { |
||||
"zh" -> Locale.CHINESE |
||||
"tw" -> Locale.TRADITIONAL_CHINESE |
||||
"en" -> Locale.ENGLISH |
||||
else -> getSystemLocale() |
||||
} |
||||
val configuration: Configuration = resources.configuration |
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
||||
configuration.setLocale(targetLocale) |
||||
context.createConfigurationContext(configuration) |
||||
} else { |
||||
@Suppress("DEPRECATION") |
||||
configuration.locale = targetLocale |
||||
@Suppress("DEPRECATION") |
||||
resources.updateConfiguration(configuration, resources.displayMetrics) |
||||
context |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 当前系统语言 |
||||
*/ |
||||
private fun getSystemLocale(): Locale { |
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //7.0有多语言设置获取顶部的语言 |
||||
Resources.getSystem().configuration.locales.get(0) |
||||
} else { |
||||
@Suppress("DEPRECATION") |
||||
Resources.getSystem().configuration.locale |
||||
} |
||||
} |
||||
|
||||
|
||||
} |
Loading…
Reference in new issue