switch lang

This commit is contained in:
LagradOst 2021-09-02 16:06:43 +02:00
parent 57811c0bf5
commit 532a845c95
5 changed files with 70 additions and 15 deletions

View file

@ -6,6 +6,7 @@ import android.content.ComponentName
import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.ColorStateList
import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import android.view.*
@ -42,6 +43,7 @@ import com.lagradost.cloudstream3.utils.UIHelper.shouldShowPIPMode
import com.lagradost.cloudstream3.utils.UIHelper.toPx
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_result.*
import java.util.*
import kotlin.concurrent.thread
@ -111,6 +113,16 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
}
}
fun setLocale(activity: Activity?, languageCode: String?) {
if (activity == null || languageCode == null) return
val locale = Locale(languageCode)
Locale.setDefault(locale)
val resources = activity.resources
val config: Configuration = resources.configuration
config.setLocale(locale)
resources.updateConfiguration(config, resources.displayMetrics)
}
}
private fun enterPIPMode() {
@ -227,6 +239,10 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
true
) // THEME IS SET BEFORE VIEW IS CREATED TO APPLY THE THEME TO THE MAIN VIEW
val settingsManager = PreferenceManager.getDefaultSharedPreferences(this)
val localeCode = settingsManager.getString(getString(R.string.locale_key), null)
setLocale(this, localeCode)
super.onCreate(savedInstanceState)
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)

View file

@ -5,39 +5,37 @@ import android.widget.Toast
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager
import com.lagradost.cloudstream3.MainActivity.Companion.setLocale
import com.lagradost.cloudstream3.MainActivity.Companion.showToast
import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.mvvm.normalSafeApiCall
import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.ui.subtitles.SubtitlesFragment
import com.lagradost.cloudstream3.utils.Coroutines.main
import com.lagradost.cloudstream3.utils.InAppUpdater.Companion.runAutoUpdate
import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showDialog
import com.lagradost.cloudstream3.utils.UIHelper.hideKeyboard
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import java.util.*
import kotlin.Exception
import kotlin.concurrent.thread
class SettingsFragment : PreferenceFragmentCompat() {
var count = 0
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
hideKeyboard()
setPreferencesFromResource(R.xml.settings, rootKey)
val updatePrefrence = findPreference<Preference>(getString(R.string.manual_check_update_key))!!
val benenePref = findPreference<Preference>(getString(R.string.benene_count))!!
val updatePreference = findPreference<Preference>(getString(R.string.manual_check_update_key))!!
val localePreference = findPreference<Preference>(getString(R.string.locale_key))!!
val benenePreference = findPreference<Preference>(getString(R.string.benene_count))!!
try {
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)
count = settingsManager.getInt(getString(R.string.benene_count), 0)
benenePref.summary =
benenePreference.summary =
if (count <= 0) getString(R.string.benene_count_text_none) else getString(R.string.benene_count_text).format(
count
)
benenePref.setOnPreferenceClickListener {
benenePreference.setOnPreferenceClickListener {
try {
count++
settingsManager.edit().putInt(getString(R.string.benene_count), count).apply()
@ -52,16 +50,50 @@ class SettingsFragment : PreferenceFragmentCompat() {
e.printStackTrace()
}
updatePrefrence.setOnPreferenceClickListener {
updatePreference.setOnPreferenceClickListener {
thread {
if (!requireActivity().runAutoUpdate(false)) {
activity?.runOnUiThread {
showToast(activity, "No Update Found", Toast.LENGTH_SHORT)
showToast(activity, R.string.no_update_found, Toast.LENGTH_SHORT)
}
}
}
return@setOnPreferenceClickListener true
}
localePreference.setOnPreferenceClickListener { pref ->
val languages = listOf(
Triple("\uD83C\uDDEC\uD83C\uDDE7", "English", "en"),
Triple("\uD83C\uDDF3\uD83C\uDDF1", "Dutch", "nl"),
Triple("\uD83C\uDDEC\uD83C\uDDF7", "Greek", "gr"),
) // idk, if you find a way of automating this it would be great
val current = getCurrentLocale()
val languageCodes = languages.map { it.third }
val languageNames = languages.map { "${it.first} ${it.second}" }
val index = languageCodes.indexOf(current)
pref?.context?.showDialog(
languageNames, index, getString(R.string.app_language), true, { }
) { languageIndex ->
try {
val code = languageCodes[languageIndex]
setLocale(activity, code)
val settingsManager = PreferenceManager.getDefaultSharedPreferences(pref.context)
settingsManager.edit().putString(getString(R.string.locale_key), code).apply()
activity?.recreate()
} catch (e: Exception) {
logError(e)
}
}
return@setOnPreferenceClickListener true
}
}
private fun getCurrentLocale(): String {
val res = context!!.resources
// Change locale settings in the app.
// val dm = res.displayMetrics
val conf = res.configuration
return conf?.locale?.language ?: "en"
}
override fun onPreferenceTreeClick(preference: Preference?): Boolean {

View file

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM18.92,8h-2.95c-0.32,-1.25 -0.78,-2.45 -1.38,-3.56 1.84,0.63 3.37,1.91 4.33,3.56zM12,4.04c0.83,1.2 1.48,2.53 1.91,3.96h-3.82c0.43,-1.43 1.08,-2.76 1.91,-3.96zM4.26,14C4.1,13.36 4,12.69 4,12s0.1,-1.36 0.26,-2h3.38c-0.08,0.66 -0.14,1.32 -0.14,2 0,0.68 0.06,1.34 0.14,2L4.26,14zM5.08,16h2.95c0.32,1.25 0.78,2.45 1.38,3.56 -1.84,-0.63 -3.37,-1.9 -4.33,-3.56zM8.03,8L5.08,8c0.96,-1.66 2.49,-2.93 4.33,-3.56C8.81,5.55 8.35,6.75 8.03,8zM12,19.96c-0.83,-1.2 -1.48,-2.53 -1.91,-3.96h3.82c-0.43,1.43 -1.08,2.76 -1.91,3.96zM14.34,14L9.66,14c-0.09,-0.66 -0.16,-1.32 -0.16,-2 0,-0.68 0.07,-1.35 0.16,-2h4.68c0.09,0.65 0.16,1.32 0.16,2 0,0.68 -0.07,1.34 -0.16,2zM14.59,19.56c0.6,-1.11 1.06,-2.31 1.38,-3.56h2.95c-0.96,1.65 -2.49,2.93 -4.33,3.56zM16.36,14c0.08,-0.66 0.14,-1.32 0.14,-2 0,-0.68 -0.06,-1.34 -0.14,-2h3.38c0.16,0.64 0.26,1.31 0.26,2s-0.1,1.36 -0.26,2h-3.38z"/>
</vector>

View file

@ -212,4 +212,6 @@
<string name="episode_action_auto_download">Auto Download</string>
<string name="episode_action_download_mirror">Download Mirror</string>
<string name="episode_action_reload_links">Reload Links</string>
<string name="no_update_found">"No Update Found"</string>
</resources>

View file

@ -80,7 +80,7 @@
<Preference
android:key="@string/locale_key"
android:title="@string/app_language"
android:icon="@drawable/ic_outline_subtitles_24">
android:icon="@drawable/ic_baseline_language_24">
</Preference>
<SwitchPreference
android:key="acra.disable"