diff --git a/app/src/main/java/com/hippo/content/ContextLocalWrapper.java b/app/src/main/java/com/hippo/content/ContextLocalWrapper.java new file mode 100644 index 000000000..87e2d5087 --- /dev/null +++ b/app/src/main/java/com/hippo/content/ContextLocalWrapper.java @@ -0,0 +1,61 @@ +/* + * Copyright 2018 Hippo Seven + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hippo.content; + +/* + * Created by Hippo on 2018/3/27. + */ + +import android.content.Context; +import android.content.ContextWrapper; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.os.Build; +import android.os.LocaleList; +import java.util.Locale; + +public class ContextLocalWrapper extends ContextWrapper { + + public ContextLocalWrapper(Context base) { + super(base); + } + + public static ContextLocalWrapper wrap(Context context, Locale newLocale) { + Resources res = context.getResources(); + Configuration configuration = res.getConfiguration(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + configuration.setLocale(newLocale); + + LocaleList localeList = new LocaleList(newLocale); + LocaleList.setDefault(localeList); + configuration.setLocales(localeList); + + context = context.createConfigurationContext(configuration); + + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + configuration.setLocale(newLocale); + context = context.createConfigurationContext(configuration); + + } else { + configuration.locale = newLocale; + res.updateConfiguration(configuration, res.getDisplayMetrics()); + } + + return new ContextLocalWrapper(context); + } +} diff --git a/app/src/main/java/com/hippo/ehviewer/Settings.java b/app/src/main/java/com/hippo/ehviewer/Settings.java index 226fda5c7..d642ec8f0 100755 --- a/app/src/main/java/com/hippo/ehviewer/Settings.java +++ b/app/src/main/java/com/hippo/ehviewer/Settings.java @@ -963,6 +963,17 @@ public class Settings { putBoolean(KEY_BUILT_IN_HOSTS, value); } + public static final String KEY_APP_LANGUAGE = "app_language"; + private static final String DEFAULT_APP_LANGUAGE = "system"; + + public static String getAppLanguage() { + return getString(KEY_APP_LANGUAGE, DEFAULT_APP_LANGUAGE); + } + + public static void putAppLanguage(String value) { + putString(KEY_APP_LANGUAGE, value); + } + /******************** ****** Guide ********************/ diff --git a/app/src/main/java/com/hippo/ehviewer/ui/EhActivity.java b/app/src/main/java/com/hippo/ehviewer/ui/EhActivity.java index f379ffdb7..ade0dee23 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/EhActivity.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/EhActivity.java @@ -16,15 +16,16 @@ package com.hippo.ehviewer.ui; +import android.content.Context; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; -import android.util.Log; import android.view.WindowManager; - import com.google.analytics.tracking.android.EasyTracker; +import com.hippo.content.ContextLocalWrapper; import com.hippo.ehviewer.EhApplication; import com.hippo.ehviewer.Settings; +import java.util.Locale; public abstract class EhActivity extends AppCompatActivity { @@ -74,4 +75,26 @@ public abstract class EhActivity extends AppCompatActivity { getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE); } } + + @Override + protected void attachBaseContext(Context newBase) { + Locale locale = null; + String language = Settings.getAppLanguage(); + if (language != null && !language.equals("system")) { + String[] split = language.split("-"); + if (split.length == 1) { + locale = new Locale(split[0]); + } else if (split.length == 2) { + locale = new Locale(split[0], split[0]); + } else if (split.length == 3) { + locale = new Locale(split[0], split[0], split[0]); + } + } + + if (locale != null) { + newBase = ContextLocalWrapper.wrap(newBase, locale); + } + + super.attachBaseContext(newBase); + } } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/EhPreferenceActivity.java b/app/src/main/java/com/hippo/ehviewer/ui/EhPreferenceActivity.java index 6863eebd4..9fd6f61f9 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/EhPreferenceActivity.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/EhPreferenceActivity.java @@ -16,15 +16,17 @@ package com.hippo.ehviewer.ui; +import android.content.Context; +import android.content.res.Resources; import android.os.Bundle; import android.support.annotation.Nullable; -import android.util.Log; import android.view.WindowManager; - import com.google.analytics.tracking.android.EasyTracker; import com.hippo.app.AppCompatPreferenceActivity; +import com.hippo.content.ContextLocalWrapper; import com.hippo.ehviewer.EhApplication; import com.hippo.ehviewer.Settings; +import java.util.Locale; public abstract class EhPreferenceActivity extends AppCompatPreferenceActivity { @@ -74,4 +76,27 @@ public abstract class EhPreferenceActivity extends AppCompatPreferenceActivity { getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE); } } + + @Override + protected void attachBaseContext(Context newBase) { + Locale locale = null; + String language = Settings.getAppLanguage(); + if (language != null && !language.equals("system")) { + String[] split = language.split("-"); + if (split.length == 1) { + locale = new Locale(split[0]); + } else if (split.length == 2) { + locale = new Locale(split[0], split[0]); + } else if (split.length == 3) { + locale = new Locale(split[0], split[0], split[0]); + } + } + + if (locale == null) { + locale = Resources.getSystem().getConfiguration().locale; + } + + newBase = ContextLocalWrapper.wrap(newBase, locale); + super.attachBaseContext(newBase); + } } diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index fc5c69749..3831f32a3 100755 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -494,6 +494,7 @@ 清理内存缓存 清理缩略图、画廊信息的内存缓存 阅读缓存大小 + App 界面语言 内置 hosts.txt 应用提供的主机到IP的映射\n可被自定义 hosts.txt 覆盖 自定义 hosts.txt @@ -571,6 +572,8 @@ 找不到任何数据 无法读取文件 + 系统语言(默认) + 开源许可 diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index 8520ccee0..25eb931f4 100755 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -493,6 +493,7 @@ 清理內存緩存 清理縮略圖、畫廊信息的內存緩存 閲讀緩存大小 + App 界面語言 內置 hosts.txt 應用提供的主機到IP的映射\n可被自定義 hosts.txt 覆蓋 自定義 hosts.txt @@ -570,6 +571,8 @@ 找不到任何數據 無法讀取文件 + 系統語言(默認) + 開源許可 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index c7d018f49..7ab48001d 100755 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -493,6 +493,7 @@ 清除記憶體快取 清除縮圖、圖庫詳細資料的記憶體快取 閱讀快取大小 + App 介面語言 內建 hosts.txt 應用提供的主機到IP的對映\n可被自定義 hosts.txt 覆蓋 自定義 hosts.txt @@ -570,6 +571,8 @@ 找不到任何資料 無法讀取檔案 + 系統語言(預設) + 授權 diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index fe370a59a..4faa1986f 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -217,4 +217,26 @@ @string/add_filter + + @string/app_language_system + @string/app_language_en + @string/app_language_es + @string/app_language_ja + @string/app_language_ko + @string/app_language_zh_cn + @string/app_language_zh_hk + @string/app_language_zh_tw + + + + system + en + es + ja + ko + zh-CN + zh-HK + zh-TW + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 64309c391..fca8cb7ff 100755 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -537,6 +537,7 @@ Clear memory cache Clear thumb, gallery info memory cache Read cache size + App language Built-in hosts.txt The host-to-IP mapping provided by the app\nIt can be overrode by custom hosts.txt Custom hosts.txt @@ -587,6 +588,15 @@ Can\'t find any data Can\'t read the file + System Language (Default) + English + Español + 日本語 + 한국어 + 中文(中国) + 中文(香港) + 國文(臺灣) + Excluded languages All diff --git a/app/src/main/res/xml/advanced_settings.xml b/app/src/main/res/xml/advanced_settings.xml index 731f59ebd..76b357441 100644 --- a/app/src/main/res/xml/advanced_settings.xml +++ b/app/src/main/res/xml/advanced_settings.xml @@ -48,6 +48,15 @@ app:entryValues="@array/read_cache_size_entry_values" android:defaultValue="160"/> + +