-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from IamRezaMousavi/add-locale
add en locale
- Loading branch information
Showing
14 changed files
with
481 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
app/src/main/kotlin/com/github/iamrezamousavi/mafia/data/local/SharedPreferences.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.github.iamrezamousavi.mafia.data.local | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import androidx.core.content.edit | ||
import com.github.iamrezamousavi.mafia.data.model.Language | ||
import com.github.iamrezamousavi.mafia.data.model.toLanguage | ||
|
||
const val PREF_APP_LANGUAGE = "AppLanguage" | ||
|
||
val Context.preferences: SharedPreferences | ||
get() = getSharedPreferences("${packageName}_preferences", Context.MODE_PRIVATE) | ||
|
||
fun SharedPreferences.saveLanguage(language: Language) = edit { | ||
putString(PREF_APP_LANGUAGE, language.code) | ||
} | ||
|
||
fun SharedPreferences.getLanguage() = getString(PREF_APP_LANGUAGE, Language.FA.code).toLanguage()!! |
17 changes: 17 additions & 0 deletions
17
app/src/main/kotlin/com/github/iamrezamousavi/mafia/data/model/Language.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.github.iamrezamousavi.mafia.data.model | ||
|
||
import java.util.Locale | ||
|
||
enum class Language( | ||
val code: String, | ||
val nativeName: String | ||
) { | ||
FA("fa", "فارسی"), | ||
EN("en", "English"); | ||
|
||
fun asSystemLocale() = Locale(code) | ||
} | ||
|
||
fun String?.toLanguage() = Language.entries.find { it.code == this } | ||
|
||
fun String?.toNativeLanguage() = Language.entries.find { it.nativeName == this } |
12 changes: 12 additions & 0 deletions
12
app/src/main/kotlin/com/github/iamrezamousavi/mafia/utils/Configuration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.github.iamrezamousavi.mafia.utils | ||
|
||
import android.os.Build | ||
import androidx.annotation.ChecksSdkIntAtLeast | ||
|
||
@get:ChecksSdkIntAtLeast(api = Build.VERSION_CODES.TIRAMISU) | ||
inline val isAtLeastAndroid13 | ||
get() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU | ||
|
||
@get:ChecksSdkIntAtLeast(api = Build.VERSION_CODES.N) | ||
inline val isAtLeastAndroid7 | ||
get() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N |
37 changes: 37 additions & 0 deletions
37
app/src/main/kotlin/com/github/iamrezamousavi/mafia/utils/Locale.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.github.iamrezamousavi.mafia.utils | ||
|
||
import android.content.Context | ||
import android.content.ContextWrapper | ||
import com.github.iamrezamousavi.mafia.data.local.getLanguage | ||
import com.github.iamrezamousavi.mafia.data.local.preferences | ||
import java.util.Locale | ||
|
||
@Suppress("DEPRECATION") | ||
fun getContextWrapper(context: Context): ContextWrapper { | ||
val resources = context.resources | ||
val configuration = resources.configuration | ||
|
||
val language = context.preferences.getLanguage() | ||
val locale = Locale(language.code) | ||
Locale.setDefault(locale) | ||
|
||
val currentLocale = | ||
if (isAtLeastAndroid7) { | ||
configuration.locales.get(0) | ||
} else { | ||
configuration.locale | ||
} | ||
|
||
if (currentLocale.language == language.code) { | ||
return ContextWrapper(context) | ||
} | ||
|
||
configuration.setLocale(locale) | ||
|
||
if (isAtLeastAndroid7) { | ||
context.createConfigurationContext(configuration) | ||
} | ||
resources.updateConfiguration(configuration, resources.displayMetrics) | ||
|
||
return ContextWrapper(context) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
app/src/main/kotlin/com/github/iamrezamousavi/mafia/view/SettingsFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.github.iamrezamousavi.mafia.view | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.AdapterView | ||
import android.widget.ArrayAdapter | ||
import androidx.fragment.app.Fragment | ||
import com.github.iamrezamousavi.mafia.data.local.getLanguage | ||
import com.github.iamrezamousavi.mafia.data.local.preferences | ||
import com.github.iamrezamousavi.mafia.data.local.saveLanguage | ||
import com.github.iamrezamousavi.mafia.data.model.Language | ||
import com.github.iamrezamousavi.mafia.data.model.toNativeLanguage | ||
import com.github.iamrezamousavi.mafia.databinding.FragmentSettingsBinding | ||
|
||
class SettingsFragment : Fragment() { | ||
|
||
private lateinit var binding: FragmentSettingsBinding | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View { | ||
binding = FragmentSettingsBinding.inflate(inflater, container, false) | ||
return binding.root | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
setupLanguageSpinner() | ||
} | ||
|
||
private fun setupLanguageSpinner() { | ||
val currentLanguage = requireContext().preferences.getLanguage() | ||
val items = Language | ||
.entries | ||
.map { it.nativeName } | ||
.sorted() | ||
.let { languages -> | ||
listOf(currentLanguage.nativeName) + | ||
languages.filter { it != currentLanguage.nativeName } | ||
} | ||
val arrayAdapter = ArrayAdapter( | ||
requireContext(), | ||
android.R.layout.simple_spinner_item, | ||
items | ||
).also { adapter -> | ||
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) | ||
} | ||
|
||
binding.langSpinner.apply { | ||
adapter = arrayAdapter | ||
onItemSelectedListener = object : AdapterView.OnItemSelectedListener { | ||
override fun onItemSelected( | ||
parent: AdapterView<*>, | ||
view: View?, | ||
position: Int, | ||
id: Long | ||
) { | ||
val selectedItem = parent.getItemAtPosition(position).toString() | ||
val newLanguage = selectedItem.toNativeLanguage() | ||
val currentLang = requireContext().preferences.getLanguage() | ||
if (newLanguage != currentLang) { | ||
if (newLanguage != null) { | ||
requireContext().preferences.saveLanguage(newLanguage) | ||
setSelection(0) | ||
} | ||
} | ||
} | ||
|
||
@Suppress("EmptyFunctionBlock") | ||
override fun onNothingSelected(parent: AdapterView<*>?) { | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".view.SettingsFragment"> | ||
|
||
<com.google.android.material.appbar.AppBarLayout | ||
android:id="@+id/settingsAppBar" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
app:layout_constraintLeft_toLeftOf="parent" | ||
app:layout_constraintRight_toRightOf="parent" | ||
app:layout_constraintTop_toTopOf="parent"> | ||
|
||
<com.google.android.material.appbar.MaterialToolbar | ||
android:id="@+id/settingsToolBar" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:minHeight="?attr/actionBarSize" | ||
app:title="@string/settings" | ||
app:titleCentered="true" /> | ||
|
||
</com.google.android.material.appbar.AppBarLayout> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="0dp" | ||
android:orientation="vertical" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@id/settingsAppBar"> | ||
|
||
<androidx.cardview.widget.CardView | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_margin="10dp" | ||
app:cardBackgroundColor="@color/md_theme_secondaryContainer" | ||
app:cardCornerRadius="5dp"> | ||
|
||
<androidx.constraintlayout.widget.ConstraintLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:padding="10dp"> | ||
|
||
<TextView | ||
android:id="@+id/textView1" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_margin="10dp" | ||
android:text="@string/language" | ||
android:textColor="@color/md_theme_onSecondaryContainer" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
<Spinner | ||
android:id="@+id/langSpinner" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> | ||
|
||
</androidx.cardview.widget.CardView> | ||
|
||
</LinearLayout> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.