Start working on change language

feature/top10
Aurelien Hubert 7 years ago
parent 21802eb7ec
commit bc132c2529
  1. 5
      app/src/main/java/net/pokeranalytics/android/PokerAnalyticsApplication.kt
  2. 7
      app/src/main/java/net/pokeranalytics/android/ui/fragment/SettingsFragment.kt
  3. 4
      app/src/main/java/net/pokeranalytics/android/ui/view/rowrepresentable/SettingRow.kt
  4. 97
      app/src/main/java/net/pokeranalytics/android/util/LocaleUtils.kt
  5. 1
      app/src/main/java/net/pokeranalytics/android/util/Preferences.kt

@ -1,6 +1,7 @@
package net.pokeranalytics.android
import android.app.Application
import android.content.Context
import com.crashlytics.android.Crashlytics
import io.fabric.sdk.android.Fabric
import io.realm.Realm
@ -69,6 +70,10 @@ class PokerAnalyticsApplication : Application() {
}
}
override fun attachBaseContext(base: Context?) {
super.attachBaseContext(base)
}
/**
* Create fake sessions if we have less than 10 sessions
*/

@ -20,6 +20,7 @@ import net.pokeranalytics.android.ui.adapter.StaticRowRepresentableDataSource
import net.pokeranalytics.android.ui.fragment.components.PokerAnalyticsFragment
import net.pokeranalytics.android.ui.view.RowRepresentable
import net.pokeranalytics.android.ui.view.rowrepresentable.SettingRow
import net.pokeranalytics.android.util.LocaleUtils
import net.pokeranalytics.android.util.Preferences
import net.pokeranalytics.android.util.URL
import net.pokeranalytics.android.util.extensions.openContactMail
@ -56,8 +57,8 @@ class SettingsFragment : PokerAnalyticsFragment(), RowRepresentableDelegate, Sta
override fun stringForRow(row: RowRepresentable): String {
return when (row) {
SettingRow.VERSION -> BuildConfig.VERSION_NAME + if (BuildConfig.DEBUG) " DEBUG" else ""
SettingRow.LANGUAGE -> LocaleUtils.getCurrentLocale(requireContext()).displayLanguage
SettingRow.CURRENCY -> Currency.getInstance(Preferences.getCurrencyLocale(this.parentActivity)).symbol
else -> ""
}
}
@ -93,6 +94,10 @@ class SettingsFragment : PokerAnalyticsFragment(), RowRepresentableDelegate, Sta
SettingRow.RATE_APP -> parentActivity.openPlayStorePage()
SettingRow.CONTACT_US -> parentActivity.openContactMail(R.string.contact)
SettingRow.BUG_REPORT -> parentActivity.openContactMail(R.string.bug_report_subject)
SettingRow.LANGUAGE -> {
LocaleUtils.setLocale(requireContext(), "fr")
activity?.recreate()
}
SettingRow.CURRENCY -> CurrenciesActivity.newInstanceForResult(this@SettingsFragment, SettingsFragment.REQUEST_CODE_CURRENCY)
SettingRow.FOLLOW_US -> {
when (position) {

@ -18,6 +18,7 @@ enum class SettingRow : RowRepresentable {
FOLLOW_US,
// Preferences
LANGUAGE,
CURRENCY,
// Data management
@ -80,6 +81,7 @@ enum class SettingRow : RowRepresentable {
PRIVACY_POLICY -> R.string.privacy_policy
TERMS_OF_USE -> R.string.terms_of_use
FOLLOW_US -> R.string.follow_us
LANGUAGE -> R.string.language
CURRENCY -> R.string.currency
GDPR -> R.string.gdpr
else -> null
@ -92,7 +94,7 @@ enum class SettingRow : RowRepresentable {
get() {
return when (this) {
VERSION -> RowViewType.TITLE_VALUE.ordinal
CURRENCY -> RowViewType.TITLE_VALUE_ARROW.ordinal
LANGUAGE, CURRENCY -> RowViewType.TITLE_VALUE_ARROW.ordinal
FOLLOW_US -> RowViewType.ROW_FOLLOW_US.ordinal
else -> RowViewType.TITLE_ARROW.ordinal
}

@ -0,0 +1,97 @@
package net.pokeranalytics.android.util
import android.annotation.TargetApi
import android.content.Context
import android.os.Build
import android.preference.PreferenceManager
import java.util.*
class LocaleUtils {
companion object {
/**
* Return the current locale
*/
fun getCurrentLocale(context: Context) : Locale {
val defaultLocaleCode = Preferences.getString(Preferences.Keys.LOCALE_CODE, context)
var locale = Locale.getDefault()
if (defaultLocaleCode != null) {
locale = Locale(defaultLocaleCode)
Locale.setDefault(locale)
}
return locale
}
private val SELECTED_LANGUAGE = "Locale.Helper.Selected.Language"
fun onAttach(context: Context): Context {
val lang = getPersistedData(context, Locale.getDefault().language)
return setLocale(context, lang)
}
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
}
}
}

@ -8,6 +8,7 @@ class Preferences {
enum class Keys(var identifier: String) {
CURRENCY_CODE("CurrencyCode"),
LOCALE_CODE("LocaleCode"),
FIRST_LAUNCH("firstLaunch")
}

Loading…
Cancel
Save