-
Project address: Github
- If your project's Gradle configuration is
below 7.0, add the following to yourbuild.gradlefile:
allprojects {
repositories {
// JitPack remote repository: https://jitpack.io
maven { url 'https://jitpack.io' }
}
}- If your Gradle configuration is
7.0 or above, add the following to yoursettings.gradlefile:
dependencyResolutionManagement {
repositories {
// JitPack remote repository: https://jitpack.io
maven { url 'https://jitpack.io' }
}
}- After configuring the remote repository, add the remote dependency in the
build.gradlefile under your app module:
dependencies {
// Language switching framework: https://github.com/getActivity/MultiLanguages
implementation 'com.github.getActivity:MultiLanguages:10.2'
}- Initialize the framework in your Application class:
public final class XxxApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// Initialize the language switching framework
MultiLanguages.init(this);
}
}- Override the attachBaseContext method in your Application:
@Override
protected void attachBaseContext(Context base) {
// Bind language
super.attachBaseContext(MultiLanguages.attach(base));
}- Override the attachBaseContext method in your base class BaseActivity:
@Override
protected void attachBaseContext(Context newBase) {
// Bind language
super.attachBaseContext(MultiLanguages.attach(newBase));
}-
Any subclass of Context needs to override this method, including Service. This will not be repeated here.
-
Note: Fragment does not need to override this method because it is not a subclass of Context.
// Set the current language (returns true if the app needs to be restarted)
MultiLanguages.setAppLanguage(Context context, Locale locale);
// Get the current language
MultiLanguages.getAppLanguage(Context context);
// Follow system language (returns true if the app needs to be restarted)
MultiLanguages.clearAppLanguage(Context context);// Get the system language
MultiLanguages.getSystemLanguage(Context context);
// Whether to follow the system language
MultiLanguages.isSystemLanguage(Context context);
// Compare if two languages are the same (e.g., Simplified and Traditional Chinese, American and British English)
MultiLanguages.equalsLanguage(Locale locale1, Locale locale2);
// Compare whether the two languages are of the same language and from the same place (for example: simplified Chinese used in Chinese mainland and traditional Chinese used in Taiwan, China).
MultiLanguages.equalsLanguageAndCountry(Locale locale1, Locale locale2);
// Notes: If it is to determine whether two Locale objects are the same, please use the equalsLanguageAndCountry method for judgment instead of the equalsLanguage method
// Also, please do not use the equals method that comes with the Locale class to make a judgment, as it will compare more information and may lead to a judgment error.
// Github issue : https://github.com/getActivity/MultiLanguages/issues/63
// Get a String in a specific language
MultiLanguages.getLanguageString(Context context, Locale locale, int stringId);
// Generate a Resources object for a specific language
MultiLanguages.generateLanguageResources(Context context, Locale locale);
// Update the language of Context
MultiLanguages.updateAppLanguage(Context context);
// Update the language of Resources
MultiLanguages.updateAppLanguage(Resources resources);
// Set the default language (the earlier, the better)
MultiLanguages.setDefaultLanguage(Locale locale);// Set language change listener
MultiLanguages.setOnLanguageListener(new OnLanguageListener() {
@Override
public void onAppLocaleChange(Locale oldLocale, Locale newLocale) {
Log.d("MultiLanguages", "Detected app language change, old language: " + oldLocale + ", new language: " + newLocale);
}
@Override
public void onSystemLocaleChange(Locale oldLocale, Locale newLocale) {
Log.d("MultiLanguages", "Detected system language change, old language: " + oldLocale + ", new language: " + newLocale + ", following system: " + MultiLanguages.isSystemLanguage());
}
});@Override
public void onClick(View v) {
// Whether restart is needed
boolean restart;
switch (v.getId()) {
// Follow system
case R.id.btn_language_auto:
restart = MultiLanguages.clearAppLanguage(this);
break;
// Simplified Chinese
case R.id.btn_language_cn:
restart = MultiLanguages.setAppLanguage(this, LocaleContract.getSimplifiedChineseLocale());
break;
// Traditional Chinese
case R.id.btn_language_tw:
restart = MultiLanguages.setAppLanguage(this, LocaleContract.getTraditionalChineseLocale());
break;
// English
case R.id.btn_language_en:
restart = MultiLanguages.setAppLanguage(this, LocaleContract.getEnglishLocale());
break;
default:
restart = false;
break;
}
if (restart) {
// You can make full use of Activity transition animations, and set a fade effect when switching
Intent intent = new Intent(this, MainActivity.class);
// Github address: https://github.com/getActivity/MultiLanguages/issues/55
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
overridePendingTransition(R.anim.activity_alpha_in, R.anim.activity_alpha_out);
finish();
}
}Other resources: Complete List of Language Codes
-
Android middle office: AndroidProject
-
Android middle office kt version: AndroidProject-Kotlin
-
Permissions framework: XXPermissions
-
Toast framework: Toaster
-
Network framework: EasyHttp
-
Title bar framework: TitleBar
-
Floating window framework: EasyWindow
-
Device compatibility framework:DeviceCompat
-
Shape view framework: ShapeView
-
Shape drawable framework: ShapeDrawable
-
Gson parsing fault tolerance: GsonFactory
-
Logcat viewing framework: Logcat
-
Nested scrolling layout framework:NestedScrollLayout
-
Android cmd tools:AndroidCmdTools
-
Android version guide: AndroidVersionAdapter
-
Android code standard: AndroidCodeStandard
-
Android resource summary:AndroidIndex
-
Android open source leaderboard: AndroidGithubBoss
-
Studio boutique plugins: StudioPlugins
-
Emoji collection: EmojiPackage
-
China provinces json: ProvinceJson
-
Markdown documentation:MarkdownDoc
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.
