Не знаю, что происходит, но не могу загрузить ресурс после обновления действия, я думаю, языковой стандарт меняется, но ресурсы загружаются неправильно
Структура моего кода выглядит следующим образом
Я поддерживаю хинди язык ![After adding my resource file, the structure will look like this](https://i.stack.imgur.com/2CZ8W.png)
Вот код, который я сделал до сих пор
LocaleManagerMew.kt
val SELECTED_LANGUAGE = "MEW_CURRENT_-- USER_LANGUAGE"
var mEnglishFlag = "en"
var mHindiFlag = "hi"
fun setLocale(context: Context?): Context? {
return updateResources(context, getCurrentLanguage(context))
}
inline fun setNewLocale(context: Context, language: String) {
persistLanguagePreference(context, language)
updateResources(context, language)
}
inline fun getCurrentLanguage(context: Context?): String {
context?.let {
Prefs.Builder()
.setContext(it)
.setMode(ContextWrapper.MODE_PRIVATE)
.setPrefsName(it.packageName)
.setUseDefaultSharedPreference(true)
.build()
}
val mCurrentLanguage: String? = Prefs.getString(SELECTED_LANGUAGE, mEnglishFlag)
return mCurrentLanguage ?: ""
}
fun persistLanguagePreference(context: Context, language: String?) {
Prefs.Builder()
.setContext(context)
.setMode(ContextWrapper.MODE_PRIVATE)
.setPrefsName(context.packageName)
.setUseDefaultSharedPreference(true)
.build()
Prefs.putString(SELECTED_LANGUAGE, language ?: mHindiFlag)
}
fun updateResources(context: Context?, language: String): Context? {
var contextFun = context
val locale = Locale(language)
Locale.setDefault(locale)
val resources = context?.resources
val configuration = Configuration(resources?.configuration)
if (Build.VERSION.SDK_INT >= 17) {
configuration.setLocale(locale)
contextFun = context?.createConfigurationContext(configuration)
} else {
configuration.locale = locale
resources?.updateConfiguration(configuration, resources.displayMetrics)
}
return contextFun
}
}
Примечание: я использую Prefs только для хранения настроек
Класс приложения
override fun attachBaseContext(base: Context?) {
super.attachBaseContext(LocaleManagerMew.setLocale(base))
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
LocaleManagerMew.setLocale(this)
Timber.d("onConfigurationChanged: ${newConfig.locale.language}")
}
AndroidManifest. XML
<application
android:name=".AppName"
android:allowBackup="false"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning"
tools:replace="android:allowBackup">
...
<activity
android:name=".intro.IntroActivity"
android:configChanges="locale|layoutDirection"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar" />
...
</application>
IntroActivity.kt
При нажатии кнопки я меняю язык
override fun onClick(view: View?) {
when (view?.id) {
R.id.btn_next -> {
val mCurrentLanguage =
LocaleManagerMew.getCurrentLanguage(this@IntroActivity.applicationContext)
if (mCurrentLanguage == LocaleManagerMew.mEnglishFlag) {
showToast("To Hindi")
LocaleManagerMew.setNewLocale(
this@IntroActivity.applicationContext,
LocaleManagerMew.mHindiFlag
)
} else if (mCurrentLanguage == LocaleManagerMew.mHindiFlag) {
showToast("To English")
LocaleManagerMew.setNewLocale(
this@IntroActivity.applicationContext,
LocaleManagerMew.mEnglishFlag
)
}
recreate()
}
}
}
Вещи, которые я наблюдал - после вызова метода refreshate () onCreate () действия снова вызывает, но класс приложения onConfigurationChanged не вызывает
Любая помощь будет оценена