Added an option to change the language

This commit is contained in:
Bnyro 2022-05-05 20:51:34 +02:00
parent 8c5a34861c
commit 9088236620
5 changed files with 88 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package com.github.libretube
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.net.Uri
@ -27,6 +28,7 @@ import androidx.navigation.ui.setupWithNavController
import androidx.preference.PreferenceManager
import com.google.android.material.color.DynamicColors
import java.lang.Exception
import java.util.*
class MainActivity : AppCompatActivity() {
val TAG = "MainActivity"
@ -40,6 +42,15 @@ class MainActivity : AppCompatActivity() {
RetrofitInstance.url = sharedPreferences.getString("instance", "https://pipedapi.kavin.rocks/")!!
DynamicColors.applyToActivitiesIfAvailable(application)
setContentView(R.layout.activity_main)
var languageName = sharedPreferences.getString("language", "en")!!
if (languageName != "") {
val locale = Locale("$languageName")
val res = resources
val dm = res.displayMetrics
val conf = res.configuration
conf.locale = locale
res.updateConfiguration(conf, dm)
}
when (sharedPreferences.getString("theme_togglee", "A")!!) {
"A" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
"L" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)

View File

@ -8,6 +8,7 @@ import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.LocaleList
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
@ -16,6 +17,7 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.os.LocaleListCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.preference.ListPreference
@ -27,8 +29,10 @@ import org.json.JSONTokener
import retrofit2.HttpException
import java.io.IOException
import java.io.InputStream
import java.util.*
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
import kotlin.collections.ArrayList
class Settings : PreferenceFragmentCompat() {
@ -189,6 +193,24 @@ class Settings : PreferenceFragmentCompat() {
}
true
}
val changeLanguage = findPreference<ListPreference>("language")
changeLanguage?.setOnPreferenceChangeListener { _, languageName ->
val locale = Locale("$languageName")
val res = resources
val dm = res.displayMetrics
val conf = res.configuration
conf.locale = locale
res.updateConfiguration(conf, dm)
val refresh = Intent(
context,
MainActivity::class.java
)
refresh.putExtra("$languageName", "$languageName")
startActivity(refresh)
true
}
val about = findPreference<Preference>("about")
about?.setOnPreferenceClickListener {
val uri = Uri.parse("https://libre-tube.github.io/")

View File

@ -405,6 +405,50 @@
<item>ZW</item>
</string-array>
<string-array name="languages">
<item>Arabic</item>
<item>Czech</item>
<item>English</item>
<item>French</item>
<item>German</item>
<item>Hindi</item>
<item>Hungarian</item>
<item>Italian</item>
<item>Korean</item>
<item>Marathi</item>
<item>Norwegian</item>
<item>Persian</item>
<item>Polish</item>
<item>Portuguese</item>
<item>Portuguese (Brazil)</item>
<item>Russian</item>
<item>Sorani</item>
<item>Spanish</item>
<item>Turkish</item>
</string-array>
<string-array name="languagesValue">
<item>ar</item>
<item>cs</item>
<item>en</item>
<item>fr</item>
<item>de</item>
<item>hi</item>
<item>hu</item>
<item>it</item>
<item>ko</item>
<item>mr</item>
<item>nb-rNO</item>
<item>ckb</item>
<item>pl</item>
<item>pt</item>
<item>pr-rBR</item>
<item>ru</item>
<item>fa</item>
<item>es</item>
<item>tr</item>
</string-array>
<string-array name="themes">
<item>System Default</item>
<item>Light Theme</item>

View File

@ -53,4 +53,5 @@
<string name="success">Success!</string>
<string name="fail">Failed :(</string>
<string name="about">About</string>
<string name="changeLanguage">Change Language</string>
</resources>

View File

@ -11,6 +11,16 @@
android:icon="@drawable/ic_language"
/>
<ListPreference
app:key="language"
app:title="@string/changeLanguage"
app:entries="@array/languages"
app:entryValues="@array/languagesValue"
app:defaultValue="en"
app:useSimpleSummaryProvider="true"
android:icon="@drawable/ic_language"
/>
<ListPreference
app:key="instance"
app:title="@string/instances"