I am training to force my application to start in Arabic.
(我正在接受培训,以迫使我的申请开始使用阿拉伯语。)
so I set my class LocalHelper . (所以我设置了类LocalHelper 。)
object LocalHelper {
fun onAttach(context: Context): Context {
val lang = getPersistedData(context/*, Locale.getDefault().getLanguage()*/)
return setLocale(context, lang)
}
private fun setLocale(context: Context, language: String): Context {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
updateResources(context, language)
} else updateResourcesLegacy(context, language)
}
private fun getPersistedData(context: Context): String {
// val appComponent = Injector.INSTANCE.appComponent
// ?: return SharedPreferencesUtils(context, DATABASE_NAME_QURAN, Gson())
// .currentLanguage.id
return Language.ARABIC
}
@TargetApi(Build.VERSION_CODES.N)
private fun updateResources(context: Context, language: String): Context {
val locale = Locale(language)
Locale.setDefault(locale)
val configuration = context.resources.configuration
configuration.setLocale(locale)
configuration.setLayoutDirection(locale)
return context.createConfigurationContext(configuration)
}
private fun updateResourcesLegacy(context: Context, language: String): Context {
val locale = Locale(language)
Locale.setDefault(locale)
val resources = context.resources
val configuration = resources.configuration
configuration.locale = locale
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLayoutDirection(locale)
}
context.resources.configuration.setLayoutDirection(locale)
resources.updateConfiguration(configuration, resources.displayMetrics)
return context
}
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
LocalHelper.onAttach(context = this)
}
override fun attachBaseContext(base: Context) {
super.attachBaseContext(
LocalHelper.onAttach(
context = base
)
)
}
- Also, I create Strings.xml for Arabic.
(另外,我为阿拉伯语创建Strings.xml 。)
- Finally i update my BaseActivity configuration and attach such as Application class.
(最后,我更新了BaseActivity配置并附加了Application类。)
- When i run my application direction is correct but text is still English
(当我运行时,我的申请方向是正确的,但文本仍为英文)
So it work fine from android 8, but in android 7,6 or 5 it doesn't work.
(所以它可以在android 8上正常工作,但是在android 7,6或5上却不起作用。)
ask by Safwat Malek translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…