优雅的实现app国际化功能

       最近在做国际化功能,这里讲过程中的技术点记录一下。

        

       第一。如果切换不同语言之后读取不同的values语言内容

       1.所有的activity中(一般都是baseactivit中),重写下面的方法

  1. /** 
  2.      * 设置修改语言 
  3.      * 
  4.      * @param newBase 
  5.      */  
  6.     @Override  
  7.     protected void attachBaseContext(Context newBase) {  
  8.         Context context = MyContextWrapper.wrap(newBase);  
  9.         super.attachBaseContext(context);  
  10.     }  
/**
     * 设置修改语言
     *
     * @param newBase
     */
    @Override
    protected void attachBaseContext(Context newBase) {
        Context context = MyContextWrapper.wrap(newBase);
        super.attachBaseContext(context);
    }

      2.重写contextwarpper类

  1. public class MyContextWrapper extends android.content.ContextWrapper {  
  2.   
  3.     public MyContextWrapper(Context base) {  
  4.         super(base);  
  5.     }  
  6.   
  7.     public static ContextWrapper wrap(Context context) {  
  8.         Locale newLocale;  
  9.         switch (context.getSharedPreferences(MainActivity.SP_NAME, MODE_PRIVATE).getInt(MainActivity.LANGUAGE, -1)) {  
  10.             case -1://没有做过设置,读取手机语言  
  11.                 String locale = Locale.getDefault().getLanguage();  
  12.                 if (TextUtils.isEmpty(locale)) {  
  13.                     context.getSharedPreferences(MainActivity.SP_NAME, MODE_PRIVATE).edit().putInt(MainActivity.LANGUAGE, 1).commit();  
  14.                     newLocale = Locale.CHINESE;  
  15.                     MainActivity.sLanguage = 1;  
  16.                 } else if (locale.contains(“en”)) {  
  17.                     context.getSharedPreferences(MainActivity.SP_NAME, MODE_PRIVATE).edit().putInt(MainActivity.LANGUAGE, 2).commit();  
  18.                     newLocale = Locale.ENGLISH;  
  19.                     MainActivity.sLanguage = 2;  
  20.                 } else {  
  21.                     context.getSharedPreferences(MainActivity.SP_NAME, MODE_PRIVATE).edit().putInt(MainActivity.LANGUAGE, 1).commit();  
  22.                     newLocale = Locale.CHINESE;  
  23.                     MainActivity.sLanguage = 1;  
  24.                 }  
  25.                 break;  
  26.             case 2://设置为英语  
  27.                 newLocale = Locale.ENGLISH;  
  28.                 MainActivity.sLanguage = 2;  
  29.                 break;  
  30.             default://默认为汉语  
  31.                 newLocale = Locale.CHINESE;  
  32.                 MainActivity.sLanguage = 1;  
  33.                 break;  
  34.         }  
  35.         Resources res = context.getResources();  
  36.         Configuration configuration = res.getConfiguration();  
  37.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {  
  38.             configuration.setLocale(newLocale);  
  39.             LocaleList localeList = new LocaleList(newLocale);  
  40.             LocaleList.setDefault(localeList);  
  41.             configuration.setLocales(localeList);  
  42.             context = context.createConfigurationContext(configuration);  
  43.         } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {  
  44.             configuration.setLocale(newLocale);  
  45.             context = context.createConfigurationContext(configuration);  
  46.         }  
  47.         return new ContextWrapper(context);  
  48.     }  
  49. }  
public class MyContextWrapper extends android.content.ContextWrapper {

    public MyContextWrapper(Context base) {
        super(base);
    }

    public static ContextWrapper wrap(Context context) {
        Locale newLocale;
        switch (context.getSharedPreferences(MainActivity.SP_NAME, MODE_PRIVATE).getInt(MainActivity.LANGUAGE, -1)) {
            case -1://没有做过设置,读取手机语言
                String locale = Locale.getDefault().getLanguage();
                if (TextUtils.isEmpty(locale)) {
                    context.getSharedPreferences(MainActivity.SP_NAME, MODE_PRIVATE).edit().putInt(MainActivity.LANGUAGE, 1).commit();
                    newLocale = Locale.CHINESE;
                    MainActivity.sLanguage = 1;
                } else if (locale.contains("en")) {
                    context.getSharedPreferences(MainActivity.SP_NAME, MODE_PRIVATE).edit().putInt(MainActivity.LANGUAGE, 2).commit();
                    newLocale = Locale.ENGLISH;
                    MainActivity.sLanguage = 2;
                } else {
                    context.getSharedPreferences(MainActivity.SP_NAME, MODE_PRIVATE).edit().putInt(MainActivity.LANGUAGE, 1).commit();
                    newLocale = Locale.CHINESE;
                    MainActivity.sLanguage = 1;
                }
                break;
            case 2://设置为英语
                newLocale = Locale.ENGLISH;
                MainActivity.sLanguage = 2;
                break;
            default://默认为汉语
                newLocale = Locale.CHINESE;
                MainActivity.sLanguage = 1;
                break;
        }
        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.KITKAT) {
            configuration.setLocale(newLocale);
            context = context.createConfigurationContext(configuration);
        }
        return new ContextWrapper(context);
    }
}

    第二.经过上面的步骤已经可以实现切换语言之后读取不同的values下面的内容值了。但是呢,由于现在都是app内部实现语言切换,所以如果仅仅实现上面的步骤,那么仅仅实现了系统语言切换app也跟着切换语言。下面就是实现app内部实现语言切换的过程。

    1.app内部切换语言通常都是会进入一个单独的页面,然后切换语言。当切换语言页面关闭的时候呢,我们需要将任务栈中的所有页面都清空。这个过程如下:

  1. Intent intent = new Intent(this, MainActivity.class);  
  2.         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);  
  3.         startActivity(intent);  
  4.         overridePendingTransition(R.anim.change_language_enter, R.anim.change_language_exit);  
Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        overridePendingTransition(R.anim.change_language_enter, R.anim.change_language_exit);

    这样就可以实现了。上面加入了页面关闭的切换动画,为了是切换更加顺畅。但是会有如下问题。

    2.上面的操作如果mainactivity有大量的任务需要做,比如嵌套很多的fragment等等,那么就会出现明显的闪屏现象,非常不友好,为此需要给mainactivity的主题增加如下属性:

  1. <item name=“android:windowDisablePreview”>true</item>  
<item name="android:windowDisablePreview">true</item>

    至此,一个优雅的切换国际化语言的方案结束了。

demo下载地址 :https://download.csdn.net/download/zhq217217/10448158

            </div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值