{"id":1251,"date":"2023-03-18T09:42:53","date_gmt":"2023-03-18T01:42:53","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1251"},"modified":"2023-04-29T09:29:21","modified_gmt":"2023-04-29T01:29:21","slug":"switch-languages-within-android-applications","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/18\/switch-languages-within-android-applications\/","title":{"rendered":"Android\u5e94\u7528\u5185\u5207\u6362\u8bed\u8a00"},"content":{"rendered":"<h2>\u6dfb\u52a0\u591a\u8bed\u8a00\u6587\u4ef6<\/h2>\n<p>\u5728\u4e0d\u540c\u7684 value\u6587\u4ef6\u5939\u4e0b\uff08\u4f8b\u5982value \u3001value-en\u3001values-zh-rCN\u3001values-zh-rTW\u3001value-th \u6587\u4ef6\u5939\uff09\u6dfb\u52a0\u4e0d\u540c\u8bed\u8a00\u7684 string.xml<\/p>\n<h2>\u5bf9\u4e8eAndroid 7.0\u53ca\u4ee5\u4e0b\u7248\u672c<\/h2>\n<p>Android 7.0\u53ca\u4ee5\u524d\u7248\u672c\uff0c<code>Configuration<\/code>\u4e2d\u7684\u8bed\u8a00\u76f8\u5f53\u4e8e\u662fApp\u7684\u5168\u5c40\u8bbe\u7f6e\uff1a<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">\/**\n * @param context\n * @param newLanguage \u60f3\u8981\u5207\u6362\u7684\u8bed\u8a00\u7c7b\u578b \u6bd4\u5982 &quot;en&quot; ,&quot;zh&quot;\n *\/\n@SuppressWarnings(&quot;deprecation&quot;)\npublic static void changeAppLanguage(Context context, String newLanguage) {\n    if (TextUtils.isEmpty(newLanguage)) {\n        return;\n    }\n\n    Resources resources = context.getResources();\n    Configuration configuration = resources.getConfiguration();\n\n    \/\/ app locale\n    \/\/\u83b7\u53d6\u60f3\u8981\u5207\u6362\u7684\u8bed\u8a00\u7c7b\u578b\n    Locale locale = getLocaleByLanguage(newLanguage);\n\n    if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.JELLY_BEAN_MR1) {\n        configuration.setLocale(locale);\n    } else {\n        configuration.locale = locale;\n    }\n\n    \/\/ updateConfiguration\n    DisplayMetrics dm = resources.getDisplayMetrics();\n    resources.updateConfiguration(configuration, dm);\n}<\/code><\/pre>\n<p>\u7136\u540e\u5728\u7ee7\u627f<code>Application<\/code>\u7684\u7c7b\u4e2d\u8c03\u7528\u66f4\u6362\u8bed\u8a00\u7684\u65b9\u6cd5\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"language-java\">public class App extends Application {\n\n    @Override\n    public void onCreate() {\n        super.onCreate();\n        \/\/\u5bf9\u4e8e7.0\u4ee5\u4e0b\uff0c\u9700\u8981\u5728Application\u521b\u5efa\u7684\u65f6\u5019\u8fdb\u884c\u8bed\u8a00\u5207\u6362\n        onLanguageChange();\n    }\n\n    \/**\n     * Handling Configuration Changes\n     * @param newConfig newConfig\n     *\/\n    @Override\n    public void onConfigurationChanged(Configuration newConfig) {\n        super.onConfigurationChanged(newConfig);\n        onLanguageChange();\n    }\n\n    private void onLanguageChange() {\n        String language = SpUtil.getInstance(this).getString(SpUtil.LANGUAGE); \/\/\u8bfb\u53d6App\u914d\u7f6e\n        if (TextUtils.isEmpty(language)) {\n            language = SettingUtil.getLanguage();\n        }\n        LanguageUtils.changeAppLanguage(this, language);\n    }\n}<\/code><\/pre>\n<p>\u7ecf\u8fc7\u4e0a\u9762\u7684\u64cd\u4f5c\u5c31\u53ef\u4ee5\u57287.0\u4ee5\u4e0b\u5b9e\u73b0\u5e94\u7528\u5185\u5207\u6362\u8bed\u8a00<\/p>\n<h2>\u5bf9\u4e8eAndroid 7.0\u53ca\u4ee5\u4e0a\u7248\u672c<\/h2>\n<p>Android 7.0\u53ca\u4e4b\u540e\u7248\u672c\uff0c\u4f7f\u7528\u4e86<code>LocaleList<\/code>\uff0c<code>Configuration<\/code>\u4e2d\u7684\u8bed\u8a00\u8bbe\u7f6e\u53ef\u80fd\u83b7\u53d6\u7684\u4e0d\u540c\uff0c\u800c\u662f\u751f\u6548\u4e8e\u5404\u81ea\u7684Context\u3002\u8fd9\u4f1a\u5bfc\u81f4\uff1aAndroid 7.0\u4f7f\u7528\u65e7\u7684\u65b9\u5f0f\uff0c\u6709\u4e9bActivity\u53ef\u80fd\u4f1a\u663e\u793a\u4e3a\u624b\u673a\u7684\u7cfb\u7edf\u8bed\u8a00<\/p>\n<p>Android 7.0\u4f18\u5316\u4e86\u5bf9\u591a\u8bed\u8a00\u7684\u652f\u6301\uff0c\u5e9f\u5f03\u4e86<code>updateConfiguration()<\/code>\u65b9\u6cd5\uff0c\u66ff\u4ee3\u65b9\u6cd5\uff1a<code>createConfigurationContext()<\/code>\uff0c\u800c\u8fd4\u56de\u7684\u662fContext\u3002\u4e5f\u5c31\u662f\u8bed\u8a00\u9700\u8981\u690d\u5165\u5230Context\u4e2d\uff0c\u6bcf\u4e2aContext\u90fd\u690d\u5165\u4e00\u904d<\/p>\n<p>\uff081\uff09\u5b9a\u4e49\u4e00\u4e2a<code>BaseActivity<\/code>\uff0c\u91cd\u5199<code>attachBaseContext<\/code>\u65b9\u6cd5\uff0c\u5728\u6b64\u65b9\u6cd5\u91cc\u8fdb\u884c\u8bed\u8a00\u5207\u6362<\/p>\n<pre><code class=\"language-java\">public class BaseActivity extends AppCompatActivity {\n\n    \/**\n     * \u6b64\u65b9\u6cd5\u5148\u4e8e onCreate()\u65b9\u6cd5\u6267\u884c\n     * @param newBase\n     *\/\n    @Override\n    protected void attachBaseContext(Context newBase) {\n        \/\/\u83b7\u53d6\u6211\u4eec\u5b58\u50a8\u7684\u8bed\u8a00\u73af\u5883 \u6bd4\u5982 &quot;en&quot;,&quot;zh&quot;\u7b49\u7b49\n        String language = SpUtil.getInstance(App.getContext()).getString(SpUtil.LANGUAGE);\n        \/\/attach \u5bf9\u5e94\u8bed\u8a00\u73af\u5883\u4e0b\u7684context\n        super.attachBaseContext(LanguageUtils.attachBaseContext(newBase, language));\n    }\n}<\/code><\/pre>\n<p>\uff082\uff09<code>LanguageUtils<\/code>\u4e2d\u7684<code>attachBaseContext()<\/code>\u65b9\u6cd5<\/p>\n<pre><code class=\"language-java\">public static Context attachBaseContext(Context context, String language) {\n    Log.d(TAG, &quot;attachBaseContext: &quot;+Build.VERSION.SDK_INT);\n    if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.N) {\n        return updateResources(context, language);\n    } else {\n        return context;\n    }\n}\n\n@TargetApi(Build.VERSION_CODES.N)\nprivate static Context updateResources(Context context, String language) {\n    Resources resources = context.getResources();\n    Locale locale = LanguageUtils.getLocaleByLanguage(language);\n\n    Configuration configuration = resources.getConfiguration();\n    configuration.setLocale(locale);\n    configuration.setLocales(new LocaleList(locale));\n    return context.createConfigurationContext(configuration);\n}<\/code><\/pre>\n<p>\u5728<code>attachBaseContext()<\/code>\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u5224\u65ad\u4e00\u4e0b\uff0c\u5982\u679c\u5f53\u524dapi\u5927\u4e8e24\uff0c\u90a3\u4e48\u5c31\u8c03\u7528<code>updateResources()<\/code>\u65b9\u6cd5\u66f4\u65b0context<\/p>\n<p>\u5b9a\u4e49\u597d<code>BaseActivity<\/code>\u4ee5\u540e\uff0c\u6211\u4eec\u53ea\u9700\u8981\u8ba9\u6240\u6709\u7684Activity\u90fd\u7ee7\u627f\u8fd9\u4e2a\u57fa\u7c7b\u5373\u53ef<\/p>\n<h2>\u624b\u52a8\u5207\u6362\u8bed\u8a00<\/h2>\n<p>\u5b9a\u4e49\u4e00\u4e2a<code>ChangeLanguageActivity<\/code><\/p>\n<pre><code class=\"language-java\">public void onClick(View view) {\n    String language = null;\n    switch (view.getId()) {\n        case R.id.btn_chinese:\n            \/\/\u5207\u6362\u4e3a\u7b80\u4f53\u4e2d\u6587\n            language = LanguageType.CHINESE.getLanguage();\n            break;\n        case R.id.btn_english:\n            \/\/\u5207\u6362\u4e3a\u82f1\u8bed\n            language = LanguageType.ENGLISH.getLanguage();\n            break;\n        case R.id.btn_thailand:\n            \/\/\u5207\u6362\u4e3a\u6cf0\u8bed\n            language = LanguageType.THAILAND.getLanguage();\n            break;\n        default:\n            break;\n    }\n    changeLanguage(language);\n}\n\nprivate void changeLanguage(String language) {\n    if (Build.VERSION.SDK_INT &lt; Build.VERSION_CODES.N) {\n        LanguageUtils.changeAppLanguage(getApplicationContext(), language);\n    } else {\n        LanguageUtils.changeAppLanguage(this, newLanguage);\n    }\n    SpUtil.getInstance(this).putString(SpUtil.LANGUAGE, language);\n\n    recreate();\n    ActivityUtil.getInstance().recreateAllOtherActivity(this);\n}<\/code><\/pre>\n<p>\u5982\u679c\u63a7\u4ef6\u5c3a\u5bf8\u56fa\u5b9a\uff0c\u5f53\u5207\u6362\u8bed\u8a00\u7684\u65f6\u5019\uff0c\u6587\u5b57\u957f\u77ed\u53d1\u751f\u53d8\u5316\u5982\u4f55\u89e3\u51b3\uff1f<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528google\u63d0\u4f9b\u7684\u65b0\u7279\u6027\u6765\u89e3\u51b3\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/developer.android.com\/guide\/topics\/ui\/look-and-feel\/autosizing-textview\" title=\"Autosizing TextViews\">Autosizing TextViews<\/a><\/p>\n<h2>\u4ee3\u7801\u5c01\u88c5<\/h2>\n<p>\uff081\uff09\u521b\u5efa\u5de5\u5177\u7c7b<\/p>\n<pre><code class=\"language-java\">public class AppLanguageUtils {\n\n    public static HashMap&lt;String, Locale&gt; mAllLanguages = new HashMap&lt;String, Locale&gt;(8) {{\n        put(Constants.ENGLISH, Locale.ENGLISH);\n        put(Constants.CHINESE, Locale.SIMPLIFIED_CHINESE);\n        put(Constants.SIMPLIFIED_CHINESE, Locale.SIMPLIFIED_CHINESE);\n        put(Constants.TRADITIONAL_CHINESE, Locale.TRADITIONAL_CHINESE);\n        put(Constants.FRANCE, Locale.FRANCE);\n        put(Constants.GERMAN, Locale.GERMANY);\n        put(Constants.HINDI, new Locale(Constants.HINDI, &quot;IN&quot;));\n        put(Constants.ITALIAN, Locale.ITALY);\n    }};\n\n    @SuppressWarnings(&quot;deprecation&quot;)\n    public static void changeAppLanguage(Context context, String newLanguage) {\n        Resources resources = context.getResources();\n        Configuration configuration = resources.getConfiguration();\n\n        \/\/ app locale\n        Locale locale = getLocaleByLanguage(newLanguage);\n\n        if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.JELLY_BEAN_MR1) {\n            configuration.setLocale(locale);\n        } else {\n            configuration.locale = locale;\n        }\n\n        \/\/ updateConfiguration\n        DisplayMetrics dm = resources.getDisplayMetrics();\n        resources.updateConfiguration(configuration, dm);\n    }\n\n    private static boolean isSupportLanguage(String language) {\n        return mAllLanguages.containsKey(language);\n    }\n\n    public static String getSupportLanguage(String language) {\n        if (isSupportLanguage(language)) {\n            return language;\n        }\n\n        if (null == language) {\/\/\u4e3a\u7a7a\u5219\u8868\u793a\u9996\u6b21\u5b89\u88c5\u6216\u672a\u9009\u62e9\u8fc7\u8bed\u8a00\uff0c\u83b7\u53d6\u7cfb\u7edf\u9ed8\u8ba4\u8bed\u8a00\n            Locale locale = Locale.getDefault();\n            for (String key : mAllLanguages.keySet()) {\n                if (TextUtils.equals(mAllLanguages.get(key).getLanguage(), locale.getLanguage())) {\n                    return locale.getLanguage();\n                }\n            }\n        }\n        return Constants.ENGLISH;\n    }\n\n    \/**\n     * \u83b7\u53d6\u6307\u5b9a\u8bed\u8a00\u7684locale\u4fe1\u606f\uff0c\u5982\u679c\u6307\u5b9a\u8bed\u8a00\u4e0d\u5b58\u5728{@link #mAllLanguages}\uff0c\u8fd4\u56de\u672c\u673a\u8bed\u8a00\uff0c\u5982\u679c\u672c\u673a\u8bed\u8a00\u4e0d\u662f\u8bed\u8a00\u96c6\u5408\u4e2d\u7684\u4e00\u79cd{@link #mAllLanguages}\uff0c\u8fd4\u56de\u82f1\u8bed\n     *\n     * @param language language\n     * @return\n     *\/\n    public static Locale getLocaleByLanguage(String language) {\n        if (isSupportLanguage(language)) {\n            return mAllLanguages.get(language);\n        } else {\n            Locale locale = Locale.getDefault();\n            for (String key : mAllLanguages.keySet()) {\n                if (TextUtils.equals(mAllLanguages.get(key).getLanguage(), locale.getLanguage())) {\n                    return locale;\n                }\n            }\n        }\n        return Locale.ENGLISH;\n    }\n\n    public static Context attachBaseContext(Context context, String language) {\n        if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.N) {\n            return updateResources(context, language);\n        } else {\n            return context;\n        }\n    }\n\n    @TargetApi(Build.VERSION_CODES.N)\n    private static Context updateResources(Context context, String language) {\n        Resources resources = context.getResources();\n        Locale locale = AppLanguageUtils.getLocaleByLanguage(language);\n\n        Configuration configuration = resources.getConfiguration();\n        configuration.setLocale(locale);\n        configuration.setLocales(new LocaleList(locale));\n        return context.createConfigurationContext(configuration);\n    }\n\n}<\/code><\/pre>\n<p>\uff082\uff09\u5728\u7ee7\u627fApplication\u7684\u7c7b\u4e2d\u91cd\u5199<code>attachBaseContext()<\/code>\u65b9\u6cd5\u7b49\u64cd\u4f5c<\/p>\n<pre><code class=\"language-java\">public class AppApplication extends Application {\n    private static Context sContext;\n    private String language;\n\n    @Override\n    protected void attachBaseContext(Context base) {\n        super.attachBaseContext(AppLanguageUtils.attachBaseContext(base, getAppLanguage(base)));\n    }\n\n    @Override\n    public void onCreate() {\n        super.onCreate();\n        sContext = this;\n        spu = new SharedPreferencesUtil(getApplicationContext());\n        language = spu.getString(&quot;language&quot;);\n        onLanguageChange();\n    }\n\n    public static Context getContext() {\n        return sContext;\n    }\n\n    \/**\n     * Handling Configuration Changes\n     * @param newConfig newConfig\n     *\/\n    @Override\n    public void onConfigurationChanged(Configuration newConfig) {\n        super.onConfigurationChanged(newConfig);\n        onLanguageChange();\n    }\n\n    private void onLanguageChange() {\n        \/\/AppLanguageUtils.changeAppLanguage(this, AppLanguageUtils.getSupportLanguage(getAppLanguage(this)));\n        AppLanguageUtils.changeAppLanguage(this, AppLanguageUtils.getSupportLanguage(language));\n    }\n\n    private String getAppLanguage(Context context) {\n        String appLang = PreferenceManager.getDefaultSharedPreferences(context)\n                .getString(&quot;language&quot;, Constants.ENGLISH);\n        return appLang ;\n    }\n}<\/code><\/pre>\n<p>\uff083\uff09\u5728\u9700\u8981\u5207\u6362\u8bed\u8a00\u7684SetLanguageActivity\u4e2d\u8bbe\u7f6e\u5207\u6362\u65b9\u6cd5<\/p>\n<pre><code class=\"language-java\">private void onChangeAppLanguage(String newLanguage) {\n    spu.putString(&quot;language&quot;, newLanguage);\n    AppLanguageUtils.changeAppLanguage(this, newLanguage);\n    AppLanguageUtils.changeAppLanguage(App.getContext(), newLanguage);\n    this.recreate();\n}<\/code><\/pre>\n<p>\uff084\uff09\u8df3\u8f6c\u5230SetLanguageActivity\u7684\u539f\u754c\u9762\u8bed\u8a00\u9700\u8981\u5237\u65b0<\/p>\n<pre><code class=\"language-java\">\/\/\u643a\u53c2\u8df3\u8f6c\nstartActivityForResult(new Intent(OriginActivity.this, SetLanguageActivity.class), CHANGE_LANGUAGE_REQUEST_CODE);<\/code><\/pre>\n<pre><code class=\"language-java\">\/\/\u5207\u6362\u540e\u8fd4\u56de\u5237\u65b0\n@Override\nprotected void onActivityResult(int requestCode, int resultCode, Intent data) {\n    super.onActivityResult(requestCode, resultCode, data);\n     if (requestCode == CHANGE_LANGUAGE_REQUEST_CODE) {\n        recreate();\n    }\n}<\/code><\/pre>\n<p>\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/yanlu.me\/android-7-0-app-language-switch\/\">https:\/\/yanlu.me\/android-7-0-app-language-switch\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6dfb\u52a0\u591a\u8bed\u8a00\u6587\u4ef6 \u5728\u4e0d\u540c\u7684 value\u6587\u4ef6\u5939\u4e0b\uff08\u4f8b\u5982value \u3001value-en\u3001values-zh-rCN\u3001 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[55],"class_list":["post-1251","post","type-post","status-publish","format-standard","hentry","category-android-basic","tag-internationalization"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1251"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1251\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}