From 90882366202a6e8cfa5b2d87079708d98c6c0759 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 5 May 2022 20:51:34 +0200 Subject: [PATCH] Added an option to change the language --- .../java/com/github/libretube/MainActivity.kt | 11 +++++ .../java/com/github/libretube/Settings.kt | 22 ++++++++++ app/src/main/res/values/array.xml | 44 +++++++++++++++++++ app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/settings.xml | 10 +++++ 5 files changed, 88 insertions(+) diff --git a/app/src/main/java/com/github/libretube/MainActivity.kt b/app/src/main/java/com/github/libretube/MainActivity.kt index a3ad8b095..e41e73b27 100644 --- a/app/src/main/java/com/github/libretube/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/MainActivity.kt @@ -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) diff --git a/app/src/main/java/com/github/libretube/Settings.kt b/app/src/main/java/com/github/libretube/Settings.kt index 3eb3fd6d1..85579e7a7 100644 --- a/app/src/main/java/com/github/libretube/Settings.kt +++ b/app/src/main/java/com/github/libretube/Settings.kt @@ -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("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("about") about?.setOnPreferenceClickListener { val uri = Uri.parse("https://libre-tube.github.io/") diff --git a/app/src/main/res/values/array.xml b/app/src/main/res/values/array.xml index b4771ebb8..365222ad7 100644 --- a/app/src/main/res/values/array.xml +++ b/app/src/main/res/values/array.xml @@ -405,6 +405,50 @@ ZW + + Arabic + Czech + English + French + German + Hindi + Hungarian + Italian + Korean + Marathi + Norwegian + Persian + Polish + Portuguese + Portuguese (Brazil) + Russian + Sorani + Spanish + Turkish + + + + ar + cs + en + fr + de + hi + hu + it + ko + mr + nb-rNO + ckb + pl + pt + pr-rBR + ru + fa + es + tr + + System Default Light Theme diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5956c75de..d866e46ef 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -53,4 +53,5 @@ Success! Failed :( About + Change Language diff --git a/app/src/main/res/xml/settings.xml b/app/src/main/res/xml/settings.xml index 41d6fa069..18ae15fe2 100644 --- a/app/src/main/res/xml/settings.xml +++ b/app/src/main/res/xml/settings.xml @@ -11,6 +11,16 @@ android:icon="@drawable/ic_language" /> + +