add basic multiple language support

This commit is contained in:
Kr328 committed 2020-02-17 01:54:12 +08:00
1 parent 4848a0f6d9
commit 72932a255d
3 files changed
+37 -8

No files matched your search

@@ -20,6 +20,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import java.util.*
abstract class BaseActivity : AppCompatActivity(), CoroutineScope by MainScope() {
class EmptyBroadcastReceiver : BroadcastReceiver() {
@@ -55,6 +56,7 @@ abstract class BaseActivity : AppCompatActivity(), CoroutineScope by MainScope()
var menu: Menu? = null
lateinit var uiPreference: UiPreferences
private set
lateinit var language: String
open suspend fun onClashStarted() {}
open suspend fun onClashStopped(reason: String?) {}
@@ -81,17 +83,40 @@ abstract class BaseActivity : AppCompatActivity(), CoroutineScope by MainScope()
super.setContentView(base)
}
override fun attachBaseContext(newBase: Context?) {
val base = newBase ?: return super.attachBaseContext(newBase)
uiPreference = UiPreferences(base)
language = uiPreference.get(UiPreferences.LANGUAGE)
val languageOverride = language.split("-")
if ( language.isEmpty() )
return super.attachBaseContext(base)
val configuration = base.resources.configuration
val localeOverride = if ( languageOverride.size == 2 )
Locale(languageOverride[0], languageOverride[1])
else
Locale(languageOverride[0])
configuration.setLocale(localeOverride)
super.attachBaseContext(base.createConfigurationContext(configuration))
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
uiPreference = UiPreferences(this)
resetLightNavigationBar()
}
override fun onStart() {
super.onStart()
if ( language != uiPreference.get(UiPreferences.LANGUAGE) )
recreate()
Broadcasts.register(receiver)
}
@@ -11,10 +11,15 @@ class UiPreferences(context: Context) {
const val PROXY_SORT_NAME = "name"
const val PROXY_SORT_DELAY = "delay"
const val LANGUAGE_DEFAULT = ""
const val LANGUAGE_EN = "en"
const val LANGUAGE_ZH_CN = "zh-CN"
val PROXY_GROUP_SORT = StringEntry("proxy_group_sort", PROXY_SORT_DEFAULT)
val PROXY_PROXY_SORT = StringEntry("proxy_proxy_sort", PROXY_SORT_DEFAULT)
val PROXY_LAST_SELECT_GROUP = StringEntry("proxy_last_select_group", "")
val PROXY_MERGE_PREFIX = BooleanEntry("proxy_merge_prefix", true)
val LANGUAGE = StringEntry("language", "")
}
interface Entry<T> {
@@ -22,7 +27,7 @@ class UiPreferences(context: Context) {
fun put(editor: SharedPreferences.Editor, value: T)
}
class StringEntry(private val key: String, private val defaultValue: String? = null) :
class StringEntry(private val key: String, private val defaultValue: String) :
Entry<String> {
override fun get(sharedPreferences: SharedPreferences): String {
return sharedPreferences.getString(key, defaultValue)!!
+4 -5
View File
@@ -20,10 +20,9 @@
android:gravity="center_vertical">
<View
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="36dp"
android:contentDescription="@string/application_name"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginStart="40dp"
android:background="@drawable/ic_logo" />
<TextView
@@ -33,7 +32,7 @@
android:textColor="?attr/colorPrimary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp" />
android:layout_marginStart="25dp" />
</LinearLayout>
<com.github.kr328.clash.design.view.ColorfulTextCard