Skip to content

Latest commit

 

History

History
253 lines (184 loc) · 11.7 KB

File metadata and controls

253 lines (184 loc) · 11.7 KB

Language Switching Framework

Integration Steps

  • If your project's Gradle configuration is below 7.0, add the following to your build.gradle file:
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 your settings.gradle file:
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.gradle file under your app module:
dependencies {
    // Language switching framework: https://github.com/getActivity/MultiLanguages
    implementation 'com.github.getActivity:MultiLanguages:10.2'
}

Initialize the Framework

  • 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.

Language Settings

// 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);

Other APIs

// 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);

Language Change Listener

// 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());
    }
});

Usage Example

@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 Open Source Projects by the Author

License

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.