转自彪哥:Android 史上最强多语言国际化,不仅第一次会跟随系统,而且会保存用户的语言设置

来源:互联网 发布:js正则表达式数字 编辑:程序博客网 时间:2024/06/06 05:43

1.我等屌丝喜欢简单粗暴,首先来一幅图


哥们我是大陆人,当然默认语言是 中文简体,但是我刚刚切换成了繁体了


2.看下配置文件,按照这个格式 ,看图吧,简单粗暴,别问为什么,你就按照这样写,如果你想知道为什么这样写,以及详细的步骤,请百度 :  Android 多语言 

阿拉伯语 ar, 德语 de ,英语 en ,西班牙 es, 法语 fr ,日语 ja ,韩语 ko ,葡萄牙 pt , 我大天朝 和 港澳台 就略过了,不解释,




3.注意下所有语言配置文件  string.xml 里面的文本格式是一样的,只是语言不同  ,name 是相同的,对应的值不同

      



4.如何在引用这些鸟东西了,是个Android开发人员都会。。。  还是截个图吧,




5.进入史上最牛逼的环节, 第一次进入APP

    首先引进 公共方法,上代码 

        

  1. public static final String SYSTEM_LOCALE_LANGUAGUE_STRING = "system_locale_languague_string";  
  2.     public static final String SYSTEM_LOCALE_COUNTRY_STRING = "system_locale_country_string";  
  3.       
  4.     public static final String ENAME[]={"zh","en","fr","de","ja","ko","es","pt","ar"};  
  1.   
  1. <pre name="code" class="java">public static Locale getSystemLacale(Context context) {  
  2.         SharedPreferences sharedPreferences = getCurrentSharedPreferences(context);  
  3.         String str = sharedPreferences.getString(SYSTEM_LOCALE_LANGUAGUE_STRING, "no_languague");  
  4.         String strc = sharedPreferences.getString(SYSTEM_LOCALE_COUNTRY_STRING, "");  
  5.         if ("no_languague".equals(str)) {  
  6.               
  7.             Locale l=Locale.getDefault();  
  8.             String def="en";  
  9.             for (int i = 0; i < ENAME.length; i++) {  
  10.                 if (ENAME[i].equals(l.getLanguage())) {  
  11.                     def=ENAME[i];  
  12.                     break;  
  13.                 }  
  14.             }  
  15.               
  16.             Locale nLocale=null;  
  17.             if ("zh".equals(def)) {  
  18.                 if ("CN".equals(l.getCountry())) {  
  19.                     nLocale=Locale.SIMPLIFIED_CHINESE;  
  20.                 }else {  
  21.                     nLocale=Locale.TRADITIONAL_CHINESE;  
  22.                 }  
  23.                   
  24.                   
  25.             }else {  
  26.                 nLocale=new Locale(def);  
  27.             }  
  28.               
  29.               
  30.               
  31.             setSystemLacate(context, nLocale);  
  32.               
  33.             return nLocale;  
  34.         }   
  35.           
  36.           
  37.         return new Locale(str,strc);  
  38.     }  
  39.       
  40.     public static void setSystemLacale(Context context,Locale locale){  
  41.         SharedPreferences sharedPreferences = getCurrentSharedPreferences(context);  
  42.         SharedPreferences.Editor editor = sharedPreferences.edit();  
  43.         editor.putString(SYSTEM_LOCALE_LANGUAGUE_STRING, locale.getLanguage());  
  44.         editor.putString(SYSTEM_LOCALE_COUNTRY_STRING, locale.getCountry());  
  45.         editor.commit();  
  46.           
  47.     }  


  1.   
不用我多解释吧,获取系统默认的 Lacale ,为什么这样写了?  我就不解释了, 如果你有更好的方法,大神你就在下面评论吧!


加入改变语言的方法

  1. private void reloadLanguageAction(){  
  2.         Locale locale = StaticFunction.getSystemLacate(MainActivity.this);  
  3.         Locale.setDefault(locale);    
  4.         Configuration config = new Configuration();    
  5.         config.locale = locale;    
  6.        getBaseContext().getResources().updateConfiguration(config, null);  
  7.        getBaseContext().getResources().flushLayoutCache();  
  8.          
  9.     }  


在哪里调用了 ?   这个问题跟 学挖掘机哪家强是同一个类型的问题。 上图






6.语言切换,也就是 你看到的第一张图,这里面的功能怎么搞

  


为什么要直接跳到首页, 不跳行不行了 ?    iOS可以不跳,iOS可以发送一个通知,全局一下子改变了, Android我最早的时候尝试过使用观察者模式,蛋碎了一地。。。。如果你使用观察者模式做到了我这种效果,请评论吧。。

2015年08月12日00:36:48 ,擦嘞, 苦逼的程序猿明天还要上班 

0 0