Android国际化多语言实现

随着这个国内IT行业的不断兴起,也随着公司的不断壮大,走国际化路线已经是越来越大众化,这里只是将自己做这个国际化多语言简单做一个记录为今后开发更快速。

 1.首先对自己string资源文件进行处理,同时创建以下如图文件,我这里只做啦中文 、中文繁体及英文三种语言:Android国际化多语言实现_第1张图片

2.资源文件以后就该去写切换界面啦:一下为activity_language.xml 界面的layout文件



    

    
    

        

        
    
    
    

        

        
    
    
    

        

        
    
    
    

        

        
    
    

    

3.布局文件完成以后就是activity具体时间啦,废话不多说代码如下:


import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.Message;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * @author:created by Geno
 * @Detail:多语言设置
 * @Time: 2019/8/28
 */
public class LanguageActivity extends BaseActivity {
    @BindView(R.id.tvLanguage)
    TextView tvLanguage;
    @BindView(R.id.tvConfirm)
    TextView tvConfirm;
    @BindView(R.id.ivEn)
    ImageView ivEn;
    @BindView(R.id.ivCns)
    ImageView ivCns;
    @BindView(R.id.ivCnt)
    ImageView ivCnt;
    @BindView(R.id.ivAuto)
    ImageView ivAuto;

    private Resources resources;
    private DisplayMetrics dm;
    private Configuration config;
    private String strsKey = "language";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_language);
        ButterKnife.bind(this);
        initLanguage();
        initInter();
    }
    private void initLanguage(){
        resources = getContext().getResources();
        dm = resources.getDisplayMetrics();
        config = resources.getConfiguration();
        String systemLocale = config.locale.toString();
        switchLanguage(systemLocale);


    }
    private void initInter() {
        //字体设置
        Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/Inter-Black-3.otf");
        tvLanguage.setTypeface(typeFace);
        tvConfirm.setTypeface(typeFace);
    }

    @OnClick({R.id.login_back,R.id.rlEn,R.id.rlCns,R.id.rlCnt,R.id.rlAuto,R.id.tvConfirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_back:
                onBackPressed();
                break;
            case R.id.rlEn:
               switchLanguage("en");
                break;
            case R.id.rlCns:
                switchLanguage("zh");
                break;
            case R.id.rlCnt:
                switchLanguage("zh_TW");
                break;
            case R.id.rlAuto:
                switchLanguage("anto");
                break;
            case R.id.tvConfirm:
                changeLanguage();
                break;
            default:
                break;
        }
    }
    private void changeLanguage(){
        // 应用用户选择语言
        resources.updateConfiguration(config, dm);
        Intent intent = new Intent(this, MeshBaseActivity.class);
        startActivity(intent);
    }
    private void switchLanguage(String language){
        switch (language){
            case "zh":
                ivEn.setVisibility(View.INVISIBLE);
                ivCns.setVisibility(View.VISIBLE);
                ivCnt.setVisibility(View.INVISIBLE);
                ivAuto.setVisibility(View.INVISIBLE);
                config.locale = Locale.CHINESE;
                break;
            case "en":
                ivEn.setVisibility(View.VISIBLE);
                ivCns.setVisibility(View.INVISIBLE);
                ivCnt.setVisibility(View.INVISIBLE);
                ivAuto.setVisibility(View.INVISIBLE);
                config.locale = Locale.ENGLISH;
                break;
            case "zh_TW":
                ivEn.setVisibility(View.INVISIBLE);
                ivCns.setVisibility(View.INVISIBLE);
                ivCnt.setVisibility(View.VISIBLE);
                ivAuto.setVisibility(View.INVISIBLE);
                config.locale = Locale.TRADITIONAL_CHINESE;
                break;
                default:
                    ivEn.setVisibility(View.INVISIBLE);
                    ivCns.setVisibility(View.INVISIBLE);
                    ivCnt.setVisibility(View.INVISIBLE);
                    ivAuto.setVisibility(View.VISIBLE);
                    config.locale = Locale.getDefault();
                    break;
        }
        SaveDataUtils.putData(strsKey,language);
    }
    @Override
    protected boolean handleMessage(Message msg) {
        return false;
    }
}

到这里就记录OK啦,有疑问请留言哦。

你可能感兴趣的:(Android国际化多语言实现)