All Projects → getActivity → Multilanguages

getActivity / Multilanguages

Licence: apache-2.0
Android 多语种适配框架,兼容高版本,适配第三方库语种

Programming Languages

java
68154 projects - #9 most used programming language
languages
34 projects

Projects that are alternatives of or similar to Multilanguages

blazor-ui-messages
Localization messages for Telerik UI for Blazor components: https://www.telerik.com/blazor-ui
Stars: ✭ 24 (-91.97%)
Mutual labels:  internationalization
Getx
Open screens/snackbars/dialogs/bottomSheets without context, manage states and inject dependencies easily with Get.
Stars: ✭ 5,578 (+1765.55%)
Mutual labels:  internationalization
Eo Locale
🌏Internationalize js apps 👔Elegant lightweight library based on Internationalization API
Stars: ✭ 290 (-3.01%)
Mutual labels:  internationalization
labels
Bolt Labels extension - Translatable labels for Bolt
Stars: ✭ 18 (-93.98%)
Mutual labels:  internationalization
Mojito
An automation platform that enables continuous localization.
Stars: ✭ 256 (-14.38%)
Mutual labels:  internationalization
Handlebars Intl
Handlebars helpers for internationalization.
Stars: ✭ 267 (-10.7%)
Mutual labels:  internationalization
i18n
Package i18n is for app Internationalization and Localization.
Stars: ✭ 79 (-73.58%)
Mutual labels:  internationalization
Gotext
Go (Golang) GNU gettext utilities package
Stars: ✭ 292 (-2.34%)
Mutual labels:  internationalization
Obfuscate
Guaranteed compile-time string literal obfuscation header-only library for C++14
Stars: ✭ 260 (-13.04%)
Mutual labels:  string
Awesome Falsehood
😱 Falsehoods Programmers Believe in
Stars: ✭ 16,614 (+5456.52%)
Mutual labels:  internationalization
Stryng
Swift strings taken to a whole new syntax level.
Stars: ✭ 255 (-14.72%)
Mutual labels:  string
Naza
🍀 Go basic library. || Go语言基础库
Stars: ✭ 253 (-15.38%)
Mutual labels:  string
Kind Of
Get the native JavaScript type of a value, fast. Used by superstruct, micromatch and many others!
Stars: ✭ 268 (-10.37%)
Mutual labels:  string
nim-ustring
utf-8 string for Nim
Stars: ✭ 12 (-95.99%)
Mutual labels:  string
Js Lingui
🌍📖 A readable, automated, and optimized (5 kb) internationalization for JavaScript
Stars: ✭ 3,249 (+986.62%)
Mutual labels:  internationalization
django-autotranslate
A simple Django app to automatically translate the pot (.po) files generated by django's makemessages command using google translate.
Stars: ✭ 59 (-80.27%)
Mutual labels:  internationalization
I18n4go
i18n tooling for Golang
Stars: ✭ 264 (-11.71%)
Mutual labels:  internationalization
Attributedstring
基于Swift插值方式优雅的构建富文本, 支持点击长按事件, 支持不同类型过滤, 支持自定义视图等.
Stars: ✭ 294 (-1.67%)
Mutual labels:  string
I18n Editor
GUI for editing your i18n translation files
Stars: ✭ 290 (-3.01%)
Mutual labels:  internationalization
Icu4x
Solving i18n for client-side and resource-constrained environments.
Stars: ✭ 275 (-8.03%)
Mutual labels:  internationalization

语种切换框架

集成步骤

dependencies {
    // 语种切换框架:https://github.com/getActivity/MultiLanguages
    implementation 'com.hjq:language:6.5'
}

初始化框架

  • 在 Application 中初始化框架
public final class XxxApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        // 初始化语种切换框架(自动适配第三方库中 Activity 语种)
        MultiLanguages.init(this);
    }
}
  • 重写 Application 的 attachBaseContext 方法
@Override
protected void attachBaseContext(Context base) {
    // 绑定语种
    super.attachBaseContext(MultiLanguages.attach(base));
}
  • 重写基类 BaseActivity 的 attachBaseContext 方法
@Override
protected void attachBaseContext(Context newBase) {
    // 绑定语种
    super.attachBaseContext(MultiLanguages.attach(newBase));
}
  • 只要是 Context 的子类都需要重写,Service 也雷同,这里不再赘述

语种设置

// 设置当前的语种(返回 true 需要重启 App)
MultiLanguages.setAppLanguage(Context context, Locale locale);

// 获取当前的语种
MultiLanguages.getAppLanguage();

其他 API

// 将 App 语种设置为系统语种(返回 true 需要重启 App)
MultiLanguages.setSystemLanguage(Context context);
// 获取系统的语种
MultiLanguages.getSystemLanguage();
// 是否跟随系统的语种
MultiLanguages.isSystemLanguage();

// 对比两个语言是否是同一个语种(比如:中文的简体和繁体,英语的美式和英式)
MultiLanguages.equalsLanguage(Locale locale1, Locale locale2);
// 对比两个语言是否是同一个地方的(比如:中国大陆用的中文简体,中国台湾用的中文繁体)
MultiLanguages.equalsCountry(Locale locale1, Locale locale2);

// 获取某个语种下的 String
MultiLanguages.getLanguageString(Context context, Locale locale, int stringId);
// 获取某个语种下的 Resources 对象
MultiLanguages.getLanguageResources(Context context, Locale locale);

// 更新 Context 的语种
MultiLanguages.updateAppLanguage(Context context);
// 更新 Resources 的语种
MultiLanguages.updateAppLanguage(Resources resources);

语种变化监听器

// 设置语种变化监听器
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.setSystemLanguage(this);
            break;
        // 简体中文
        case R.id.btn_language_cn:
            restart = MultiLanguages.setAppLanguage(this, Locale.CHINA);
            break;
        // 繁体中文
        case R.id.btn_language_tw:
            restart = MultiLanguages.setAppLanguage(this, Locale.TAIWAN);
            break;
        // 英语
        case R.id.btn_language_en:
            restart = MultiLanguages.setAppLanguage(this, Locale.ENGLISH);
            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();
    }
}

WebView 导致语种失效的解决方案

  • 由于 WebView 初始化会修改 Activity 语种配置,间接导致 Activity 语种会被还原回去,所以需要你手动重写 WebView 对这个问题进行修复
public final class LanguagesWebView extends WebView {

    public LanguagesWebView(@NonNull Context context) {
        this(context, null);
    }

    public LanguagesWebView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LanguagesWebView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 修复 WebView 初始化时会修改 Activity 语种配置的问题
        MultiLanguages.updateAppLanguage(context);
    }
}

其他资源:语言代码列表大全

作者的其他开源项目

微信公众号:Android轮子哥

Android 技术分享 QQ 群:78797078

如果您觉得我的开源库帮你节省了大量的开发时间,请扫描下方的二维码随意打赏,要是能打赏个 10.24 🐵就太👍了。您的支持将鼓励我继续创作

点击查看捐赠列表

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].