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

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