|
|
|
@ -1,9 +1,6 @@ |
|
|
|
package net.pokeranalytics.android.util |
|
|
|
package net.pokeranalytics.android.util |
|
|
|
|
|
|
|
|
|
|
|
import android.annotation.TargetApi |
|
|
|
|
|
|
|
import android.content.Context |
|
|
|
import android.content.Context |
|
|
|
import android.os.Build |
|
|
|
|
|
|
|
import android.preference.PreferenceManager |
|
|
|
|
|
|
|
import java.util.* |
|
|
|
import java.util.* |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -26,69 +23,11 @@ class LocaleUtils { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private val SELECTED_LANGUAGE = "Locale.Helper.Selected.Language" |
|
|
|
/** |
|
|
|
|
|
|
|
* |
|
|
|
fun onAttach(context: Context): Context { |
|
|
|
*/ |
|
|
|
val lang = getPersistedData(context, Locale.getDefault().language) |
|
|
|
fun setCurrentLocale(context: Context, language: String) { |
|
|
|
return setLocale(context, lang) |
|
|
|
Preferences.setString(Preferences.Keys.LOCALE_CODE, language, context) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun onAttach(context: Context, defaultLanguage: String): Context { |
|
|
|
|
|
|
|
val lang = getPersistedData(context, defaultLanguage) |
|
|
|
|
|
|
|
return setLocale(context, lang) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun getLanguage(context: Context): String? { |
|
|
|
|
|
|
|
return getPersistedData(context, Locale.getDefault().language) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun setLocale(context: Context, language: String?): Context { |
|
|
|
|
|
|
|
persist(context, language) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
|
|
|
|
|
|
|
updateResources(context, language) |
|
|
|
|
|
|
|
} else updateResourcesLegacy(context, language) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun getPersistedData(context: Context, defaultLanguage: String): String? { |
|
|
|
|
|
|
|
val preferences = PreferenceManager.getDefaultSharedPreferences(context) |
|
|
|
|
|
|
|
return preferences.getString(SELECTED_LANGUAGE, defaultLanguage) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun persist(context: Context, language: String?) { |
|
|
|
|
|
|
|
val preferences = PreferenceManager.getDefaultSharedPreferences(context) |
|
|
|
|
|
|
|
val editor = preferences.edit() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
editor.putString(SELECTED_LANGUAGE, language) |
|
|
|
|
|
|
|
editor.apply() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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 |
|
|
|
|
|
|
|
configuration.setLayoutDirection(locale) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
resources.updateConfiguration(configuration, resources.displayMetrics) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return context |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|