Code Monkey home page Code Monkey logo

i18ndemo's Introduction

Android多语言切换完美解决方案

最近公司开始做多语言版本,由于之前没有做过,所以在网上搜寻了一番这方面的资料,最后经过实践、总结,写下了这篇文章。源码Github

多语言的切换功能

首先,实现多语言的切换功能,参考Android App 多语言切换

1.在res资源文件目录下添加不同语言的values,如图:

添加多语言.png

2.通过修改Configuration中的locale来实现app语言的切换,具体代码如下:

Resources resources = context.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
Configuration config = resources.getConfiguration();
resources.updateConfiguration(config, dm);

3.根据本地缓存的type获取对应的locale,其中7.0以上的系统需要另做处理,具体代码如下:

Locale locale;
// 应用用户选择语言
switch (type) {
case 0:
//由于API仅支持7.0,需要判断,否则程序会crash(解决7.0以上系统不能跟随系统语言问题)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList localeList = LocaleList.getDefault();
locale = localeList.get(localeList.size() - 1);
} else {
locale = Locale.getDefault();
}
break;
、、、
default:
locale = thLocale;
break;
}

4.在AppApplication中初始化时设置本地语言,用于每次启动APP后切换到本地缓存的语言

//  设置本地化语言
I18NUtils.setLocale(this);

5.在BaseActivity的OnCreate()方法中设置语言,用于处理每次切换系统语言后app语言会跟随系统变化的问题

if(!I18NUtils.isSameLanguage(this)) {
    I18NUtils.setLocale(this);
    I18NUtils.toRestartMainActvity(this);
}

6.手动切换语言时,先更新locale配置,然后通过跳转到主Activity实现。Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK通过清理掉进程中的所有activity重新配置。

Intent intent = new Intent(activity, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
activity.startActivity(intent);
// 杀掉进程,如果是跨进程则杀掉当前进程
//  android.os.Process.killProcess(android.os.Process.myPid());
//  System.exit(0);

解决7.0以上系统存在的兼容问题

这个问题的解决参考了Android 7.0 语言设置爬坑。由于Android7.0以上Configuration将通过LocaleList来管理语言,并且系统切换语言后,系统默认语言可能并不在LocaleList顶部官方API说明

进测试得出结论,如果APP手动选择过语言则系统语言是第二个,否则是第一个。获取当前系统locale,代码如下:

//由于API仅支持7.0,需要判断,否则程序会crash(解决7.0以上系统不能跟随系统语言问题)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
 LocaleList localeList = LocaleList.getDefault();
int spType = getLanguageType(AppApplication.getAppContext());
// 如果app已选择不跟随系统语言,则取第二个数据为系统默认语言
if(spType != 0 && localeList.size() > 1) {
    locale = localeList.get(1);
    } else {
        locale = localeList.get(0);
    }
} else {
locale = Locale.getDefault();
}

7.0以上系统WebView所在Activity没有跟随语言切换问题

更新日期:2017年12月14日15:56:47

在后续的测试发现,在有webView的activitiy中语言并没有随着语言进行切换。参考多语言切换失效的问题以及stackoverflow后,通过在切换语言之前执行new WebView(context).destroy()解决。代码:

// 解决webview所在的activity语言没有切换问题
new WebView(context).destroy();
// 切换语言
Resources resources = context.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
Configuration config = resources.getConfiguration();
config.locale = getLocaleByType(type);
LogUtils.logd("setLocale: " + config.locale.toString());
resources.updateConfiguration(config, dm);

总结

自此,多语言切换的问题已经完美解决了。经测试,完全兼容7.0以上系统的多语言切换。具体代码我已上传至Github

简书地址:http://www.jianshu.com/p/16efe98d4554/)

E-mail:[email protected]

i18ndemo's People

Contributors

fitem avatar

Watchers

kenney avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.