Unverified Commit 556733e8 authored by Filipe Brito's avatar Filipe Brito Committed by GitHub

Merge pull request #2268 from HusseinElFeky/language-setter

[IMPROVEMENT] Let language setter show the selected locale
parents fb7e4ad2 2e4d72fc
package chat.rocket.android.server.infrastructure package chat.rocket.android.server.infrastructure
import android.content.SharedPreferences import android.content.SharedPreferences
import java.util.*
private const val CURRENT_LANGUAGE = "current_language" private const val CURRENT_LANGUAGE = "current_language"
private const val CURRENT_LANGUAGE_COUNTRY = "current_language_country" private const val CURRENT_LANGUAGE_COUNTRY = "current_language_country"
...@@ -16,10 +17,10 @@ class SharedPrefsCurrentLanguageRepository(private val preferences: SharedPrefer ...@@ -16,10 +17,10 @@ class SharedPrefsCurrentLanguageRepository(private val preferences: SharedPrefer
} }
override fun getLanguage(): String? { override fun getLanguage(): String? {
return preferences.getString(CURRENT_LANGUAGE, "") return preferences.getString(CURRENT_LANGUAGE, Locale.getDefault().language)
} }
override fun getCountry(): String? { override fun getCountry(): String? {
return preferences.getString(CURRENT_LANGUAGE_COUNTRY, "") return preferences.getString(CURRENT_LANGUAGE_COUNTRY, Locale.getDefault().country)
} }
} }
package chat.rocket.android.settings.presentation package chat.rocket.android.settings.presentation
import android.content.Context
import android.os.Build
import chat.rocket.android.core.lifecycle.CancelStrategy import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.db.DatabaseManagerFactory import chat.rocket.android.db.DatabaseManagerFactory
import chat.rocket.android.helper.UserHelper import chat.rocket.android.helper.UserHelper
import chat.rocket.android.main.presentation.MainNavigator import chat.rocket.android.main.presentation.MainNavigator
import chat.rocket.android.server.domain.AnalyticsTrackingInteractor import chat.rocket.android.server.domain.AnalyticsTrackingInteractor
import chat.rocket.android.server.domain.GetCurrentLanguageInteractor
import chat.rocket.android.server.domain.GetCurrentServerInteractor import chat.rocket.android.server.domain.GetCurrentServerInteractor
import chat.rocket.android.server.domain.PermissionsInteractor import chat.rocket.android.server.domain.PermissionsInteractor
import chat.rocket.android.server.domain.RemoveAccountInteractor import chat.rocket.android.server.domain.RemoveAccountInteractor
...@@ -27,6 +28,7 @@ import chat.rocket.core.internal.rest.serverInfo ...@@ -27,6 +28,7 @@ import chat.rocket.core.internal.rest.serverInfo
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import timber.log.Timber import timber.log.Timber
import java.util.*
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Named import javax.inject.Named
...@@ -40,11 +42,11 @@ class SettingsPresenter @Inject constructor( ...@@ -40,11 +42,11 @@ class SettingsPresenter @Inject constructor(
private val tokenRepository: TokenRepository, private val tokenRepository: TokenRepository,
private val permissions: PermissionsInteractor, private val permissions: PermissionsInteractor,
private val rocketChatClientFactory: RocketChatClientFactory, private val rocketChatClientFactory: RocketChatClientFactory,
private val saveLanguageInteractor: SaveCurrentLanguageInteractor,
getCurrentServerInteractor: GetCurrentServerInteractor, getCurrentServerInteractor: GetCurrentServerInteractor,
removeAccountInteractor: RemoveAccountInteractor, removeAccountInteractor: RemoveAccountInteractor,
databaseManagerFactory: DatabaseManagerFactory, databaseManagerFactory: DatabaseManagerFactory,
connectionManagerFactory: ConnectionManagerFactory, connectionManagerFactory: ConnectionManagerFactory
private val saveLanguageInteractor: SaveCurrentLanguageInteractor
) : CheckServerPresenter( ) : CheckServerPresenter(
strategy = strategy, strategy = strategy,
factory = rocketChatClientFactory, factory = rocketChatClientFactory,
...@@ -93,7 +95,6 @@ class SettingsPresenter @Inject constructor( ...@@ -93,7 +95,6 @@ class SettingsPresenter @Inject constructor(
fun enableAnalyticsTracking(isEnabled: Boolean) { fun enableAnalyticsTracking(isEnabled: Boolean) {
analyticsTrackingInteractor.save(isEnabled) analyticsTrackingInteractor.save(isEnabled)
} }
fun logout() { fun logout() {
...@@ -127,6 +128,14 @@ class SettingsPresenter @Inject constructor( ...@@ -127,6 +128,14 @@ class SettingsPresenter @Inject constructor(
} }
} }
fun getCurrentLocale(context: Context): Locale {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
context.resources.configuration.locales.get(0)
} else {
context.resources.configuration.locale
}
}
fun saveLocale(language: String, country: String? = null) { fun saveLocale(language: String, country: String? = null) {
saveLanguageInteractor.save(language, country) saveLanguageInteractor.save(language, country)
} }
......
...@@ -35,8 +35,28 @@ internal const val TAG_SETTINGS_FRAGMENT = "SettingsFragment" ...@@ -35,8 +35,28 @@ internal const val TAG_SETTINGS_FRAGMENT = "SettingsFragment"
fun newInstance(): Fragment = SettingsFragment() fun newInstance(): Fragment = SettingsFragment()
class SettingsFragment : Fragment(), SettingsView, AppLanguageView { class SettingsFragment : Fragment(), SettingsView, AppLanguageView {
@Inject lateinit var analyticsManager: AnalyticsManager @Inject
@Inject lateinit var presenter: SettingsPresenter lateinit var analyticsManager: AnalyticsManager
@Inject
lateinit var presenter: SettingsPresenter
private val locales = arrayListOf(
"en",
"ar",
"de",
"es",
"fa",
"fr",
"hi,IN",
"it",
"ja",
"pt,BR",
"pt,PT",
"ru,RU",
"tr",
"uk",
"zh,CN",
"zh,TW"
)
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
...@@ -163,28 +183,33 @@ class SettingsFragment : Fragment(), SettingsView, AppLanguageView { ...@@ -163,28 +183,33 @@ class SettingsFragment : Fragment(), SettingsView, AppLanguageView {
private fun changeLanguage() { private fun changeLanguage() {
context?.let { context?.let {
val selectedLocale = presenter.getCurrentLocale(it)
var localeIndex = -1
locales.forEachIndexed { index, locale ->
val array = locale.split(",")
val language = array[0]
val country = if (array.size > 1) array[1] else ""
// If language and country are specified, return the respective locale, else return
// the first locale found if the language is as specified regardless of the country.
if (language == selectedLocale.language) {
if (country == selectedLocale.country) {
localeIndex = index
return@forEachIndexed
} else if (localeIndex == -1) {
localeIndex = index
}
}
}
AlertDialog.Builder(it) AlertDialog.Builder(it)
.setTitle(R.string.title_choose_language) .setTitle(R.string.title_choose_language)
.setSingleChoiceItems( .setSingleChoiceItems(
resources.getStringArray(R.array.languages), -1 resources.getStringArray(R.array.languages), localeIndex
) { dialog, option -> ) { dialog, option ->
when (option) { val array = locales[option].split(",")
0 -> updateLanguage("en") if (array.size > 1) {
1 -> updateLanguage("ar") updateLanguage(array[0], array[1])
2 -> updateLanguage("de") } else {
3 -> updateLanguage("es") updateLanguage(array[0])
4 -> updateLanguage("fa")
5 -> updateLanguage("fr")
6 -> updateLanguage("hi", "IN")
7 -> updateLanguage("it")
8 -> updateLanguage("ja")
9 -> updateLanguage("pt", "BR")
10 -> updateLanguage("pt", "PT")
11 -> updateLanguage("ru", "RU")
12 -> updateLanguage("tr")
13 -> updateLanguage("uk")
14 -> updateLanguage("zh", "CN")
15 -> updateLanguage("zh", "TW")
} }
dialog.dismiss() dialog.dismiss()
} }
...@@ -231,4 +256,4 @@ class SettingsFragment : Fragment(), SettingsView, AppLanguageView { ...@@ -231,4 +256,4 @@ class SettingsFragment : Fragment(), SettingsView, AppLanguageView {
.show() .show()
} }
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment