7.0 以下
,需要在 build.gradle
文件中加入allprojects {
repositories {
// JitPack 远程仓库:https://jitpack.io
maven { url 'https://jitpack.io' }
}
}
7.0 及以上
,则需要在 settings.gradle
文件中加入dependencyResolutionManagement {
repositories {
// JitPack 远程仓库:https://jitpack.io
maven { url 'https://jitpack.io' }
}
}
build.gradle
文件中加入远程依赖dependencies {
// 语种切换框架:https://github.com/getActivity/MultiLanguages
implementation 'com.github.getActivity:MultiLanguages:9.3'
}
public final class XxxApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// 初始化语种切换框架
MultiLanguages.init(this);
}
}
@Override
protected void attachBaseContext(Context base) {
// 绑定语种
super.attachBaseContext(MultiLanguages.attach(base));
}
@Override
protected void attachBaseContext(Context newBase) {
// 绑定语种
super.attachBaseContext(MultiLanguages.attach(newBase));
}
只要是 Context 的子类都需要重写,Service 也雷同,这里不再赘述
温馨提示:Fragment 不需要重写此方法,因为它不是 Context 的子类
// 设置当前的语种(返回 true 表示需要重启 App)
MultiLanguages.setAppLanguage(Context context, Locale locale);
// 获取当前的语种
MultiLanguages.getAppLanguage(Context context);
// 跟随系统语种(返回 true 表示需要重启 App)
MultiLanguages.clearAppLanguage(Context context);
// 获取系统的语种
MultiLanguages.getSystemLanguage(Context context);
// 是否跟随系统的语种
MultiLanguages.isSystemLanguage(Context context);
// 对比两个语言是否是同一个语种(比如:中文有简体和繁体,英语有美式和英式)
MultiLanguages.equalsLanguage(Locale locale1, Locale locale2);
// 对比两个语言是否是同一个地方的(比如:中国大陆用的中文简体,中国台湾用的中文繁体)
MultiLanguages.equalsCountry(Locale locale1, Locale locale2);
// 获取某个语种下的 String
MultiLanguages.getLanguageString(Context context, Locale locale, int stringId);
// 生成某个语种下的 Resources 对象
MultiLanguages.generateLanguageResources(Context context, Locale locale);
// 更新 Context 的语种
MultiLanguages.updateAppLanguage(Context context);
// 更新 Resources 的语种
MultiLanguages.updateAppLanguage(Resources resources);
// 设置默认的语种(越早设置越好)
MultiLanguages.setDefaultLanguage(Locale locale);
// 设置语种变化监听器
MultiLanguages.setOnLanguageListener(new OnLanguageListener() {
@Override
public void onAppLocaleChange(Locale oldLocale, Locale newLocale) {
Log.d("MultiLanguages", "监听到应用切换了语种,旧语种:" + oldLocale + ",新语种:" + newLocale);
}
@Override
public void onSystemLocaleChange(Locale oldLocale, Locale newLocale) {
Log.d("MultiLanguages", "监听到系统切换了语种,旧语种:" + oldLocale + ",新语种:" + newLocale + ",是否跟随系统:" + MultiLanguages.isSystemLanguage());
}
});
@Override
public void onClick(View v) {
// 是否需要重启
boolean restart;
switch (v.getId()) {
// 跟随系统
case R.id.btn_language_auto:
restart = MultiLanguages.clearAppLanguage(this);
break;
// 简体中文
case R.id.btn_language_cn:
restart = MultiLanguages.setAppLanguage(this, LocaleContract.getSimplifiedChineseLocale());
break;
// 繁体中文
case R.id.btn_language_tw:
restart = MultiLanguages.setAppLanguage(this, LocaleContract.getTraditionalChineseLocale());
break;
// 英语
case R.id.btn_language_en:
restart = MultiLanguages.setAppLanguage(this, LocaleContract.getEnglishLocale());
break;
default:
restart = false;
break;
}
if (restart) {
// 我们可以充分运用 Activity 跳转动画,在跳转的时候设置一个渐变的效果
startActivity(new Intent(this, LanguageActivity.class));
overridePendingTransition(R.anim.activity_alpha_in, R.anim.activity_alpha_out);
finish();
}
}
proguard-rules.pro
中加入-keep class com.hjq.language.** {*;}
安卓技术中台:AndroidProject
安卓技术中台 Kt 版:AndroidProject-Kotlin
权限框架:XXPermissions
吐司框架:Toaster
网络框架:EasyHttp
标题栏框架:TitleBar
悬浮窗框架:EasyWindow
Shape 框架:ShapeView
Gson 解析容错:GsonFactory
日志查看框架:Logcat
Android 版本适配:AndroidVersionAdapter
Android 代码规范:AndroidCodeStandard
Android 资源大汇总:AndroidIndex
Android 开源排行榜:AndroidGithubBoss
Studio 精品插件:StudioPlugins
表情包大集合:EmojiPackage
AI 资源大汇总:AiIndex
省市区 Json 数据:ProvinceJson
Markdown 语法文档:MarkdownDoc
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中
【腾讯云】中小企业福利专场,多款刚需产品,满足企业通用场景需求
Copyright 2019 Huang JinQun
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.