Android实现国际化

1.使用系统切换语言实现国际化


使用系统语言实现国际化还是比较简单的,主要的的也就是创建各种不同的values-之类的东西,其实很简单 可以直接去搜一下就能找到 比如按照下面的链接完全可以去实现 (http://jingyan.baidu.com/article/9f63fb91a90ca3c8410f0e62.html)

这里我要说的就是在 系统自带的那个values中的String可以使用默认的英文了,没有必要非得去写一个values-en

2.在app内部实现国际化

我们主要来说下这种实现方式
首先我们去建立需要的资源文件也就是values中的strings.xml  
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">wangqiu</string>

</resources>
    还有一个就是中文下的values-zh-rCN的strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">玩笑</string>

</resources>
接下来就是去写我们的布局文件了
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
  >

    <TextView
        android:id="@+id/h"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name" />
    <Button 
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:onClick="change"
        />

</LinearLayout>

然后我们会用到第一个工具类,去获取系统的语言,保存我们设置的语言(便于下次进入的时候还是这种语言),等等一些操作
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;
import java.util.Locale;

public class LanguageSettingUtil {
    //单例模式-
    private static LanguageSettingUtil instance;
    private Context context;
    //存储当前系统的language设置-
    private String defaultLanguage;
    //存储当前系统Locale-
    private Locale defaultLocale;
    public final static String ENGLISH = "en";
    public final static String CHINESE = "zh";

    private LanguageSettingUtil(Context paramContext) {
        //得到系统语言-
        Locale localLocale = Locale.getDefault();
        this.defaultLocale = localLocale;

        //保存系统语言到defaultLanguage
        String str = this.defaultLocale.getLanguage();
        this.defaultLanguage = str;

        this.context = paramContext;
    }

    //检验自身是否被创建-
    public static LanguageSettingUtil get() {
        if (instance == null)
            throw new IllegalStateException(
                    "language setting not initialized yet");
        return instance;
    }

    //初始化-
    public static void init(Context paramContext) {
        if (instance == null) {
            instance = new LanguageSettingUtil(paramContext);
        }
    }

    // 创建Configuration-
    private Configuration getUpdatedLocaleConfig(String paramString) {

        Configuration localConfiguration = context.getResources()
                .getConfiguration();
        Locale localLocale = getLocale(paramString);
        localConfiguration.locale = localLocale;
        return localConfiguration;
    }

    //得到APP配置文件目前的语言设置-
    public String getLanguage() {
        SharedPreferences localSharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(this.context);
        //如果当前程序没有设置language属性就返回系统语言,如果有,就返回以前的-
        return localSharedPreferences.getString("language",
                this.defaultLanguage);
    }

    //如果配置文件中没有语言设置-
    public Locale getLocale() {
        String str = getLanguage();
        return getLocale(str);
    }

    //创建新Locale并覆盖原Locale-
    public Locale getLocale(String paramString) {
        Locale localLocale = new Locale(paramString);
        Locale.setDefault(localLocale);
        return localLocale;
    }

    //刷新显示配置-
    public void refreshLanguage() {
        String str = getLanguage();
        Resources localResources = this.context.getResources();
        if (!localResources.getConfiguration().locale.getLanguage().equals(str)) {
            Configuration localConfiguration = getUpdatedLocaleConfig(str);
            // A structure describing general information about a display, such
            // as its size, density, and font scaling.
            DisplayMetrics localDisplayMetrics = localResources
                    .getDisplayMetrics();
            localResources.updateConfiguration(localConfiguration,
                    localDisplayMetrics);
        }
    }

    //设置系统语言-
    public void saveLanguage(String paramString) {
        PreferenceManager.getDefaultSharedPreferences(this.context).edit()
                .putString("language", paramString).commit();
    }

    //保存系统的语言设置到SharedPreferences-
    public void saveSystemLanguage() {
        PreferenceManager.getDefaultSharedPreferences(this.context).edit()
                .putString("PreSysLanguage", this.defaultLanguage).commit();
    }

    public void checkSysChanged(String cuerSysLanguage) {
        //如果系统语言设置发生变化-
        if (!cuerSysLanguage.equals(PreferenceManager
                .getDefaultSharedPreferences(this.context).getString(
                        "PreSysLanguage", "zh"))) {
            //如果系统保存了this对象,就在这里修改defaultLanguage的值为当前系统语言cuerSysLanguage
            this.defaultLanguage = cuerSysLanguage;
            saveLanguage(cuerSysLanguage);
            saveSystemLanguage();
        }
    }
}
最后就是在activity中的使用了:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

	 private LanguageSettingUtil languageSetting;
	    private TextView h,w;
	    private Button b;
	    @Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_main);
	        
	        h = (TextView) findViewById(R.id.h);
	        b = (Button) findViewById(R.id.btn);
	        LanguageSettingUtil.init(this);//初始化
	        languageSetting = LanguageSettingUtil.get();//检查是否初始化
	    }


	    public void change(View view){
	        languageSetting.saveLanguage("en");//设置为"en"语言(英语),cn,是中文
	        LanguageSettingUtil.get().refreshLanguage();// 刷新
	        h.setText(R.string.app_name);//重新加载一次,不然界面更新不了
	        b.setText(R.string.app_name);//同上。当然 也可以使用recreate();代替这两行代码不过会出现闪屏,用户的体验不好
	    }
}


总结

用到的修改系统的语言的工具类也就是
private Configuration config;
private DisplayMetrics dm;
private Resources resources

初始化的方法
resources = getResources();// 获得res资源对象
config = resources.getConfiguration();// 获得设置对象
dm = resources.getDisplayMetrics();

使用到的方法:
//修改 语言
config.locale = Locale.US;//英文,简体中文是Locale.SIMPLIFIED_CHINESE
resources.updateConfiguration(config, dm);


参考文:
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值