Android 實現APP內語言國際化

最近稍微閒暇,寫一下項目中用到的語言國際化。

1:首先再res文件下,創建我們需要國際化的各種語言。

 

2:在XML裏面,對String文件裏面的字符串進行引用

 


 

 

3:語言切換

 private void switchLanguage(int language) {
        //修改過語言就直接讀取然後設置語言
        Resources resources = getResources();
        Configuration config = resources.getConfiguration();
        DisplayMetrics dm = resources.getDisplayMetrics();
        switch (language) {
            case 0:
                config.locale = Locale.ENGLISH;
                break;
            case 1:
                /**德語*/
                config.locale = Locale.GERMANY;
                break;
            case 2:
                /**西班牙語*/
                config.locale = new Locale("ES", "es", "");
                break;
            case 3:
                /**法語*/
                config.locale = Locale.FRANCE;
                break;
            case 4:
                /**俄語*/
                config.locale = new Locale("RU", "ru", "");
                ;
                break;
            case 5:
                /**日語*/
                config.locale = Locale.JAPAN;
                break;
            default:
                break;
        }

        resources.updateConfiguration(config, dm);
//        Intent intent = new Intent(this, MainActivity.class);
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//        startActivity(intent);
//        android.os.Process.killProcess(android.os.Process.myPid());
//        System.exit(0);
        recreate();

    }

 

4:demo下載地址 https://download.csdn.net/download/qq_35153556/12466530

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章