Commit 396d31a0 authored by Mygod's avatar Mygod

Fix Chinese locales once and for all (up to Nougat)

Seriously fix #927. From now on it will work even in Hong Kong (`/^zh-Han[st]-HK$/`) where for some reason people use Simplified Chinese and Traditional Chinese at the same time.
parent 812dc676
......@@ -22,7 +22,7 @@ shrinkResources in Android := true
typedResources in Android := false
resConfigs in Android := Seq("ru", "zh", "zh-rCN")
resConfigs in Android := Seq("ru", "zh-rCN", "zh-rTW")
dexMaxHeap in Android := "4g"
......
......@@ -40,13 +40,16 @@
package com.github.shadowsocks
import java.util
import java.util.Locale
import java.util.concurrent.TimeUnit
import android.app.Application
import android.content.res.Configuration
import android.os.{Build, LocaleList}
import android.preference.PreferenceManager
import android.support.v7.app.AppCompatDelegate
import com.github.shadowsocks.database.{DBHelper, ProfileManager}
import com.github.shadowsocks.utils.{Key, Utils, TcpFastOpen}
import com.github.shadowsocks.utils.{Key, TcpFastOpen, Utils}
import com.google.android.gms.analytics.{GoogleAnalytics, HitBuilders, StandardExceptionParser}
import com.google.android.gms.common.api.ResultCallback
import com.google.android.gms.tagmanager.{ContainerHolder, TagManager}
......@@ -54,6 +57,9 @@ import com.j256.ormlite.logger.LocalLog
object ShadowsocksApplication {
var app: ShadowsocksApplication = _
private final lazy val SIMPLIFIED_CHINESE = Locale.forLanguageTag("zh-Hans-CN")
private final lazy val TRADITIONAL_CHINESE = Locale.forLanguageTag("zh-Hant-TW")
}
class ShadowsocksApplication extends Application {
......@@ -88,10 +94,52 @@ class ShadowsocksApplication extends Application {
profileManager.getProfile(id) getOrElse profileManager.createProfile()
}
private def checkChineseLocale(locale: Locale): Locale = if (locale.getLanguage == "zh") locale.getCountry match {
case "CN" | "TW" => null // already supported
case _ => locale.getScript match { // fallback to the corresponding script
case "Hans" => SIMPLIFIED_CHINESE
case "Hant" => TRADITIONAL_CHINESE
}
} else null
private def checkChineseLocale(config: Configuration): Unit = if (Build.VERSION.SDK_INT >= 24) {
val localeList = config.getLocales
val newList = new Array[Locale](localeList.size())
var changed = false
for (i <- 0 until localeList.size()) {
val locale = localeList.get(i)
val newLocale = checkChineseLocale(locale)
if (newLocale == null) newList(i) = locale else {
newList(i) = newLocale
changed = true
}
}
if (changed) {
val newConfig = new Configuration(config)
newConfig.setLocales(new LocaleList(newList.distinct: _*))
val res = getResources
res.updateConfiguration(newConfig, res.getDisplayMetrics)
}
} else {
val newLocale = checkChineseLocale(config.locale)
if (config.locale != newLocale) {
val newConfig = new Configuration(config)
newConfig.locale = newLocale
val res = getResources
res.updateConfiguration(newConfig, res.getDisplayMetrics)
}
}
override def onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
checkChineseLocale(newConfig)
}
override def onCreate() {
if (!BuildConfig.DEBUG) java.lang.System.setProperty(LocalLog.LOCAL_LOG_LEVEL_PROPERTY, "ERROR")
app = this
if (!BuildConfig.DEBUG) java.lang.System.setProperty(LocalLog.LOCAL_LOG_LEVEL_PROPERTY, "ERROR")
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
checkChineseLocale(getResources.getConfiguration)
val tm = TagManager.getInstance(this)
val pending = tm.loadContainerPreferNonDefault("GTM-NT8WS8", R.raw.gtm_default_container)
val callback = new ResultCallback[ContainerHolder] {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment