Commit 010a714e authored by Govind Dixit's avatar Govind Dixit

feat: Add multi language support to app

parent c83e48dd
......@@ -43,6 +43,8 @@ import chat.rocket.android.server.domain.UsersRepository
import chat.rocket.android.server.domain.BasicAuthRepository
import chat.rocket.android.server.domain.GetBasicAuthInteractor
import chat.rocket.android.server.domain.SaveBasicAuthInteractor
import chat.rocket.android.server.infraestructure.CurrentLanguageRepository
import chat.rocket.android.server.infraestructure.SharedPrefsCurrentLanguageRepository
import chat.rocket.android.server.infraestructure.SharedPrefsBasicAuthRepository
import chat.rocket.android.server.infraestructure.DatabaseMessageMapper
import chat.rocket.android.server.infraestructure.DatabaseMessagesRepository
......@@ -201,6 +203,12 @@ class AppModule {
return SharedPrefsConnectingServerRepository(prefs)
}
@Provides
@Singleton
fun provideCurrentLanguageRepository(prefs: SharedPreferences): CurrentLanguageRepository {
return SharedPrefsCurrentLanguageRepository(prefs)
}
@Provides
@Singleton
fun provideSettingsRepository(localRepository: LocalRepository): SettingsRepository {
......
......@@ -39,14 +39,17 @@ import dagger.android.support.HasSupportFragmentInjector
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.app_bar.*
import kotlinx.android.synthetic.main.nav_header.view.*
import java.util.Locale
import javax.inject.Inject
import android.app.NotificationManager
import chat.rocket.android.server.domain.GetCurrentLanguageInteractor
import chat.rocket.android.server.domain.SaveCurrentLanguageInteractor
import java.util.Locale
private const val CURRENT_STATE = "current_state"
private const val SETTING = "Settings"
private const val MY_LANG = "My_Lang"
private const val SETTING = "settings"
private const val MY_LANG = "my_lang"
class MainActivity : AppCompatActivity(), MainView, HasActivityInjector,
HasSupportFragmentInjector {
......@@ -58,6 +61,10 @@ class MainActivity : AppCompatActivity(), MainView, HasActivityInjector,
lateinit var presenter: MainPresenter
@Inject
lateinit var permissions: PermissionsInteractor
@Inject
lateinit var getLanguageInteractor: GetCurrentLanguageInteractor
@Inject
lateinit var saveLanguageInteractor: SaveCurrentLanguageInteractor
private var isFragmentAdded: Boolean = false
private var expanded = false
private val headerLayout by lazy { view_navigation.getHeaderView(0) }
......@@ -285,78 +292,26 @@ class MainActivity : AppCompatActivity(), MainView, HasActivityInjector,
progressDialog = null
}
fun changeLanguage() {
val languages = resources.getStringArray(R.array.languages)
val builder = AlertDialog.Builder(this)
builder.setTitle(R.string.title_choose_language)
builder.setSingleChoiceItems(languages, -1) { dialog, which ->
when(which){
0->{
setLocale("en")
recreate()
}
1->{
setLocale("hi")
recreate()
}
2->{
setLocale("ja")
recreate()
}
3->{
setLocale("ru")
recreate()
}
4->{
setLocale("it")
recreate()
}
5->{
setLocaleWithRegion("pt","BR")
recreate()
}
6->{
setLocaleWithRegion("pt", "PT")
recreate()
}
7->{
setLocale("zh")
recreate()
}
}
dialog.dismiss()
}
builder.create().show()
private fun loadLocale() {
val currentLanguage = getLanguageInteractor.get()!!
setLocale(currentLanguage)
}
private fun setLocale(lang: String) {
fun setLocale(lang: String) {
val locale = Locale(lang)
Locale.setDefault(locale)
val config = Configuration()
config.locale = locale
baseContext.resources.updateConfiguration(config, baseContext.resources.displayMetrics)
val editor = getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit()
editor.putString(MY_LANG, lang)
editor.apply()
saveLanguageInteractor.save(lang)
}
private fun setLocaleWithRegion(lang: String, country: String) {
fun setLocaleWithRegion(lang: String, country: String) {
val locale = Locale(lang, country)
Locale.setDefault(locale)
val config = Configuration()
config.locale = locale
baseContext.resources.updateConfiguration(config, baseContext.resources.displayMetrics)
val editor = getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit()
editor.putString(MY_LANG, lang)
editor.apply()
}
private fun loadLocale() {
val sharedPreferences = getSharedPreferences(SETTING, Activity.MODE_PRIVATE)
val language = sharedPreferences.getString(MY_LANG, "")
setLocale(language)
saveLanguageInteractor.save(lang)
}
}
\ No newline at end of file
package chat.rocket.android.server.domain
import chat.rocket.android.server.infraestructure.CurrentLanguageRepository
import javax.inject.Inject
class GetCurrentLanguageInteractor @Inject constructor(
private val repository: CurrentLanguageRepository
) {
fun get(): String? = repository.get()
}
\ No newline at end of file
package chat.rocket.android.server.domain
import chat.rocket.android.server.infraestructure.CurrentLanguageRepository
import javax.inject.Inject
class SaveCurrentLanguageInteractor @Inject constructor(
private val repository: CurrentLanguageRepository
) {
fun save(language: String) = repository.save(language)
}
\ No newline at end of file
......@@ -8,6 +8,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.net.toUri
import androidx.fragment.app.Fragment
......@@ -24,7 +25,6 @@ import chat.rocket.android.settings.password.ui.PasswordActivity
import chat.rocket.android.settings.presentation.SettingsView
import chat.rocket.android.util.extensions.addFragmentBackStack
import chat.rocket.android.util.extensions.inflate
import chat.rocket.android.util.extensions.showToast
import chat.rocket.android.webview.ui.webViewIntent
import dagger.android.support.AndroidSupportInjection
import kotlinx.android.synthetic.main.fragment_settings.*
......@@ -75,7 +75,7 @@ class SettingsFragment : Fragment(), SettingsView, AdapterView.OnItemClickListen
resources.getStringArray(R.array.settings_actions)[1] ->
activity?.startActivity(Intent(activity, PasswordActivity::class.java))
resources.getStringArray(R.array.settings_actions)[2] -> (activity as? MainActivity)?.changeLanguage()
resources.getStringArray(R.array.settings_actions)[2] -> changeLanguage()
resources.getStringArray(R.array.settings_actions)[3] -> shareApp()
......@@ -141,6 +141,54 @@ class SettingsFragment : Fragment(), SettingsView, AdapterView.OnItemClickListen
}
}
fun changeLanguage() {
val languages = resources.getStringArray(R.array.languages)
val mainActivity =(activity as? MainActivity)
context?.let {
AlertDialog.Builder(it)
.setTitle(R.string.title_choose_language)
.setSingleChoiceItems(languages, -1) { dialog, which ->
when (which) {
0 -> {
mainActivity?.setLocale("en")
activity?.recreate()
}
1 -> {
mainActivity?.setLocale("hi")
activity?.recreate()
}
2 -> {
mainActivity?.setLocale("ja")
activity?.recreate()
}
3 -> {
mainActivity?.setLocale("ru")
activity?.recreate()
}
4 -> {
mainActivity?.setLocale("it")
activity?.recreate()
}
5->{
mainActivity?.setLocaleWithRegion("pt","BR")
activity?.recreate()
}
6->{
mainActivity?.setLocaleWithRegion("pt", "PT")
activity?.recreate()
}
7->{
mainActivity?.setLocale("zh")
activity?.recreate()
}
}
dialog.dismiss()
}
.create().show()
}
}
companion object {
fun newInstance() = SettingsFragment()
}
......
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