Skip to content

Commit

Permalink
Fix custom language loading
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelekol committed Mar 3, 2020
1 parent 66b472f commit 9aba55b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 42 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ android {
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.2.0-alpha01'
implementation 'androidx.appcompat:appcompat:1.2.0-alpha02'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

def lifecycle_version = '2.2.0'
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<string name="NotAvailable">S. O.</string>
<string name="Wallet">%s portefeuille</string>
<!--Welcome-->
<string name="Welcome_Title">\nPortefeuille Unstoppable</string>
<string name="Welcome_Title">Portefeuille \nUnstoppable</string>
<string name="Welcome_SecurityCenter">Centre de Sécurité</string>
<string name="Welcome_CreateWallet">Nouveu Porte-Monnaie</string>
<string name="Welcome_RestoreWallet">Restaurer le porte-monnaie</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<string name="NotAvailable">Нет данных</string>
<string name="Wallet">%s Кошелек</string>
<!--Welcome-->
<string name="Welcome_Title">Unstoppable \n Кошелек</string>
<string name="Welcome_Title">Unstoppable \nКошелек</string>
<string name="Welcome_SecurityCenter">Безопасность</string>
<string name="Welcome_CreateWallet">Создать Кошелек</string>
<string name="Welcome_RestoreWallet">Восстановить Кошелек</string>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package io.horizontalsystems.core.helpers

import android.annotation.TargetApi
import android.content.Context
import android.content.SharedPreferences
import android.os.Build
import android.content.res.Configuration
import java.util.*

// https://github.com/zeugma-solutions/locale-helper-android

object LocaleHelper {

private const val SELECTED_LANGUAGE = "Locale.Helper.Selected.Language"
private const val SELECTED_COUNTRY = "Locale.Helper.Selected.Country"
private val RTL: Set<String> by lazy {
hashSetOf(
"ar",
Expand Down Expand Up @@ -48,9 +46,15 @@ object LocaleHelper {
return RTL.contains(locale.language)
}

private fun updateContextLocale(context: Context, locale: Locale): Context = when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 -> updateResources(context, locale)
else -> updateResourcesLegacy(context, locale)
private fun updateContextLocale(context: Context, locale: Locale): Context {
Locale.setDefault(locale)

val configuration = Configuration()
configuration.setLocale(locale)
configuration.setLayoutDirection(locale)
configuration.setLayoutDirection(locale)

return context.createConfigurationContext(configuration)
}

private fun getPreferences(context: Context): SharedPreferences {
Expand All @@ -62,45 +66,13 @@ object LocaleHelper {
getPreferences(context)
.edit()
.putString(SELECTED_LANGUAGE, locale.language)
.putString(SELECTED_COUNTRY, locale.country)
.apply()
}

private fun load(context: Context): Locale {
val preferences = getPreferences(context)
val language = preferences.getString(SELECTED_LANGUAGE, null) ?: Locale.getDefault().language
val country = preferences.getString(SELECTED_COUNTRY, null) ?: Locale.getDefault().country
return Locale(language, country)
}

@TargetApi(Build.VERSION_CODES.N)
private fun updateResources(context: Context, locale: Locale): Context {
Locale.setDefault(locale)

val configuration = context.resources.configuration
configuration.setLocale(locale)
configuration.setLayoutDirection(locale)
configuration.setLayoutDirection(locale)

// we need to call deprecated updateConfiguration() to make Application context work with custom locale
context.resources.updateConfiguration(configuration, context.resources.displayMetrics)

return context.createConfigurationContext(configuration)
}

@Suppress("DEPRECATION")
private fun updateResourcesLegacy(context: Context, locale: Locale): Context {
Locale.setDefault(locale)

val resources = context.resources

val configuration = resources.configuration
configuration.locale = locale
configuration.setLayoutDirection(locale)

resources.updateConfiguration(configuration, resources.displayMetrics)

return context
return Locale(language)
}

}

0 comments on commit 9aba55b

Please sign in to comment.