Android通過代碼實現(xiàn)多語言切換

前言

最近接手一個Android項目,需要實現(xiàn)對維吾爾族語的支持。雖然做了這么久的android開發(fā),只做過多語言支持,但做應用內(nèi)部多語言支持還是第一次,而且還是對維吾爾語的支持。所以,又是一次面向搜索引擎編程。

面向搜索編程

如果我們搜索“android 多語言切換”,我相信得到大部分的答案是千篇一律的,連文章都長的一樣:

Locale locale = new Locale("zh");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

以上代碼應該說沒什么大問題,畢竟確實可以工作,可以實現(xiàn)需求。但是,作為一個強迫癥患者,我實際受不了有2處劃線的地方,也就是說上面代碼中有2處被廢棄了(沒錯,API 25被廢棄的):

 config.locale = locale;
 getBaseContext().getResources().updateConfiguration(config, 
 getBaseContext().getResources().getDisplayMetrics());

看到有2處被廢棄了,必須進去看看啊。

/**
     * Current user preference for the locale, corresponding to
     * <a href="{@docRoot}guide/topics/resources/providing-resources.html#LocaleQualifier">locale</a>
     * resource qualifier.
     *
     * @deprecated Do not set or read this directly. Use {@link #getLocales()} and
     * {@link #setLocales(LocaleList)}. If only the primary locale is needed,
     * <code>getLocales().get(0)</code> is now the preferred accessor.
     */
    @Deprecated public Locale locale;

很顯然我們找到了替代方法:

config.setLocales(LocaleList);
config.setLocale(locale);

很好,沒問題。繼續(xù)查看下一個:

    /**
     * Store the newly updated configuration.
     *
     * @deprecated See {@link android.content.Context#createConfigurationContext(Configuration)}.
     */
    @Deprecated
    public void updateConfiguration(Configuration config, DisplayMetrics metrics) {
        updateConfiguration(config, metrics, null);
    }

很簡單嘛,代替方法也找到了哦。于是就這么寫了下去:

    Locale locale = new Locale("ug");
    final Resources res = getResources();
    final Configuration config = res.getConfiguration();
    config.setLocale(locale); // getLocale() should return a Locale
    createConfigurationContext(config);

完全不好用!怎么寫就是不好用。再仔細一看,文檔上說createConfigurationContext(config)會返回一個新的Context,然而對新Context如何處理只字未提。又是一番面向搜索編程,終于找到了正確姿勢:

    @Override
    protected void attachBaseContext(Context newBase) {
        Locale locale = new Locale("ug");
        final Resources res = newBase.getResources();
        final Configuration config = res.getConfiguration();
        config.setLocale(locale); // getLocale() should return a Locale
        final Context newContext = newBase.createConfigurationContext(config);
        super.attachBaseContext(newContext);
    }

我們的Activity應該重寫這個方法。然后當Activity被創(chuàng)建的時候,新的Context將被應用。需要注意的,如果你想讓當前Activity生效,你需要調(diào)用recreate()。

當然啦,我們也可以寫個工具類:

public class ConfigurationWrapper {
    private ConfigurationWrapper() {
    }

    public static Context wrapConfiguration(@NonNull final Context context, @NonNull final Configuration config) {
        return context.createConfigurationContext(config);
    }
 
    
    public static Context wrapLocale(@NonNull final Context context,@NonNull final Locale locale) {
        final Resources res = context.getResources();
        final Configuration config = res.getConfiguration();
        config.setLocale(locale);
        return wrapConfiguration(context, config);
    }
}

然后你就可以這樣使用。如果你想更改configuration,就可以用wrapConfiguration。

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(ConfigurationWrapper.wrapLocale(newContext, getLocale()));
}

關(guān)于Locale

實現(xiàn)多語言切換用到了Locale。Locale里很多常見國家和地區(qū)以及語言,如果我們做常見的語言,可以直接調(diào)用系統(tǒng)的,比如Locale.CHINESE。但是這次做的維吾爾語的適配,略我坑一下。
首先,維吾爾語的英文名字叫Uyghur。Locale里并沒有,也可能是我找的不對。
其次,Uyghur是阿拉伯系的。嗯,沒錯,有個詞叫RTL,會出現(xiàn)神奇的東西。
最后,AndroidStudio的values里面是有Uyghur選項的。并且有相對應選項,其中就包括中國,我猜這就是維吾爾語。所以就建了一個,全名是:values-ug-rCN。
問題是,我最開始直接這樣寫的:

 Locale locale = new Locale("ug-rCN");

好吧,是我太天真,不好用。機智的我看了看Locale的構(gòu)造方法。后來猜了一個結(jié)論

ug代表語種,rCN代表國家和地區(qū)。比如說同樣都是中文zh,但分了很多國家和地區(qū),如中國,臺灣,香港以及新家坡等

所以下面的寫法才是正確姿勢:

Locale locale = new Locale("ug", Locale.CHINA.getCountry());

結(jié)束

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 最近公司的 App 里需要用到多語言切換,簡單來說,就是如果用戶沒有選擇語言選項時,App 默認跟隨系統(tǒng)語言,如果...
    牙鍋子閱讀 7,348評論 0 20
  • 這里的多語言切換專指應用內(nèi)的多語言切換,不涉及直接通過應用修改系統(tǒng)語言設(shè)置的功能。比如微信里面的 我 -> 設(shè)置 ...
    apkcore閱讀 5,257評論 0 3
  • 一、簡介二、效果預覽三、實現(xiàn)步驟1、功能實現(xiàn)2、配置多語言文件四、MultiLanguageUtils分析五、De...
    斌林誠上閱讀 6,343評論 16 38
  • 引言 事情是這樣的,我們接到一個需求,是要為我們的應用做多語言版本并且提供多語言切換。事后證明,這個事情還真的是很...
    luminos閱讀 5,210評論 2 6
  • 推薦指數(shù): 6.0 書籍主旨關(guān)鍵詞:特權(quán)、焦點、注意力、語言聯(lián)想、情景聯(lián)想 觀點: 1.統(tǒng)計學現(xiàn)在叫數(shù)據(jù)分析,社會...
    Jenaral閱讀 5,982評論 0 5

友情鏈接更多精彩內(nèi)容