国际化

国际化可根据不同国家使用对应的文字和图标等,只需要在res中设置对应国家的标识,放入对应的xml文件就可.

当用户在手机写选择不同语言的时候就会自动选择对应的资源!

 

1.国际化的英文单词是Internationalization,因为这个单词太长了,有时也简称为I18N,其中的I是这个单词的第一个字

符,18表示中间省略的字母个数,而N代表这个单词的最后一个字母。所以,I18N也就是国际化的意思。Android程序国际化,

也就是程序可以根据系统所使用的语言,将界面中的文字翻译成与之对应的语言。这样,可以让程序更加通用。Android可以通

过资源文件非常方便的实现程序的国际化。

 

2.在编写Android项目时,通常都是将程序中要使用的字符串资源放置在res/values目录下的strings.xml文件中,为了给这些

字符串资源实现国际化,可以在Android项目的res目录下,创建对应于各个语言的资源文件夹(例如,为了让程序兼容简体中

文、繁体中文和美式英文,可以分别创建名称为values-zh-rCNvalues-zh-rTWvalues-en-rUS的文件夹),然后在每个

文件夹中创建一个对应的strings.xml文件,并在该文件中定义对应语言的字符串即可。这样,当程序运行时,就会自动根据操

作系统所使用的语言来显示对应的字符串信息了。

 

package com.laoderman.example;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;

import java.util.Locale;

import static android.content.Context.MODE_PRIVATE;
//语言工具类
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);
        }
    }

    /**
     * 获取系统的locale
     */
    private static Locale getSystemLocale() {
        return Locale.getDefault();
    }

    // 创建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 = context.getSharedPreferences("config", MODE_PRIVATE);
        //如果当前程序没有设置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) {
        context.getSharedPreferences("config", MODE_PRIVATE).edit()
                .putString("language", paramString).commit();
    }

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

    public void checkSysChanged(String cuerSysLanguage) {
        //如果系统语言设置发生变化-
        if (!cuerSysLanguage.equals(context.getSharedPreferences("config", MODE_PRIVATE).getString(
                "PreSysLanguage", "zh"))) {
            //如果系统保存了this对象,就在这里修改defaultLanguage的值为当前系统语言cuerSysLanguage
            this.defaultLanguage = cuerSysLanguage;
            saveLanguage(cuerSysLanguage);
            saveSystemLanguage();
        }
    }
}
public class MainActivity extends AppCompatActivity {
    private LanguageSettingUtil languageSetting;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btnChage = findViewById(R.id.btn_change);
        TextView tv1 = (TextView) findViewById(R.id.tv1);
        TextView tv2 = (TextView) findViewById(R.id.tv2);
        TextView tv3 = (TextView) findViewById(R.id.tv3);

        LanguageSettingUtil.init(this);// 初始化
        languageSetting = LanguageSettingUtil.get();// 检查是否已经初始化
        Locale locale = languageSetting.getLocale();
        System.out.println( "系统locale"+locale.toString());
        btnChage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                languageSetting.saveLanguage("en");// 设置为"en"语言
                LanguageSettingUtil.get().refreshLanguage();// 刷新
                tv1.setText(R.string.hello);
                tv2.setText(R.string.change);
                tv3.setText(R.string.world);
            }
        });

    }
}
public class LocalChangeReceiver extends BroadcastReceiver {

    @Override

    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals(Intent.ACTION_LOCALE_CHANGED)) {
            //发生改变的时候,写对应的逻辑
            //如跟随系统,则进行应用重启等
        }

    }
}
       <receiver
            android:name=".LocalChangeReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.LOCALE_CHANGED" />
            </intent-filter>
        </receiver>

 

posted on 2017-02-12 20:57  LoaderMan  阅读(680)  评论(0)    收藏  举报

导航