Android, IOS 史上最强多语言国际化,不仅第一次会尾随系统,并且会保存用户的语言设置

劲爆消息,我提供源代码了。你能够先看完再下载。也能够先下载再看完,

android源代码地址: https://github.com/hebiao6446/------Bluetooth-Android

IOS源代码地址:https://github.com/hebiao6446/------Bluetooth-iOS

我想说的是原理一样。仅仅只是ios使用的是通知


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


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


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

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




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

      



4.怎样在引用这些鸟东西了,是个Android开发者都会。。

 还是截个图吧,




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

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

        

public static final String SYSTEM_LOCALE_LANGUAGUE_STRING = "system_locale_languague_string";
	public static final String SYSTEM_LOCALE_COUNTRY_STRING = "system_locale_country_string";
	
	public static final String ENAME[]={"zh","en","fr","de","ja","ko","es","pt","ar"};
</pre><pre name="code" class="java"><pre name="code" class="java">public static Locale getSystemLacale(Context context) {
		SharedPreferences sharedPreferences = getCurrentSharedPreferences(context);
		String str = sharedPreferences.getString(SYSTEM_LOCALE_LANGUAGUE_STRING, "no_languague");
		String strc = sharedPreferences.getString(SYSTEM_LOCALE_COUNTRY_STRING, "");
		if ("no_languague".equals(str)) {
			
			Locale l=Locale.getDefault();
			String def="en";
			for (int i = 0; i < ENAME.length; i++) {
				if (ENAME[i].equals(l.getLanguage())) {
					def=ENAME[i];
					break;
				}
			}
			
			Locale nLocale=null;
			if ("zh".equals(def)) {
				if ("CN".equals(l.getCountry())) {
					nLocale=Locale.SIMPLIFIED_CHINESE;
				}else {
					nLocale=Locale.TRADITIONAL_CHINESE;
				}
				
				
			}else {
				nLocale=new Locale(def);
			}
			
			
			
			setSystemLacate(context, nLocale);
			
			return nLocale;
		} 
		
		
		return new Locale(str,strc);
	}
	
	public static void setSystemLacale(Context context,Locale locale){
		SharedPreferences sharedPreferences = getCurrentSharedPreferences(context);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putString(SYSTEM_LOCALE_LANGUAGUE_STRING, locale.getLanguage());
		editor.putString(SYSTEM_LOCALE_COUNTRY_STRING, locale.getCountry());
		editor.commit();
		
	}



</pre>不用我多解释吧。获取系统默认的 Lacale ,为什么这样写了?  我就不解释了。 假设你有更好的方法,大神你就在以下评论吧!<p></p><p><br /></p><p><span style="font-size:18px">增加改变语言的方法</span></p><p><span style="font-size:18px"></span></p><pre name="code" class="java">private void reloadLanguageAction(){
		Locale locale = StaticFunction.getSystemLacate(MainActivity.this);
		Locale.setDefault(locale);  
        Configuration config = new Configuration();  
        config.locale = locale;  
       getBaseContext().getResources().updateConfiguration(config, null);
       getBaseContext().getResources().flushLayoutCache();
       
	}


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






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

  


为什么要直接跳到首页, 不跳行不行了 ?    iOS能够不跳,iOS能够发送一个通知,全局一下子改变了, Android我最早的时候尝试过使用观察者模式,蛋碎了一地。

。。

。假设你使用观察者模式做到了我这样的效果,请评论吧。。

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








posted @ 2017-06-24 13:52  yjbjingcha  阅读(170)  评论(0)    收藏  举报