GithubHelp home page GithubHelp logo

tchigher / mlang Goto Github PK

View Code? Open in Web Editor NEW

This project forked from linxueyuanstdio/mlang

0.0 0.0 0.0 693 KB

Android 动态化多语言框架,支持语言包的动态下发、升级、删除,一处安装,到处使用

License: Apache License 2.0

Java 100.00%

mlang's Introduction

MLang 动态化多语言框架

MLang 是 MultiLanguage 的简写,是一款动态化的多语言框架。

  • 动态下发语言包
  • 语言包的增加、升级、删除
  • 语言包内部任意字符串的增加、升级、删除
  • 自定义语言包的存储路径
  • 跟随系统语言
  • 处理各种语言的时区、时间格式化问题
  • 处理各种语言的复数格式化问题
  • static 方法 + 单例模式,一处安装,到处使用

使用

使用字符串

// 本地和云端都存在的字符串
MyLang.getString("local_string", R.string.local_string)

// 云端存在 remote_string_only
// 但本地没有 R.string.remote_string_only,用 R.string.fallback_string 代替
MyLang.getString("remote_string_only", R.string.fallback_string)

使用语言包

//应用一种语言(这里自动处理了语言包的升级、语言包内部字符串的升级)
MyLang.getInstance().applyLanguage(Context, LocaleInfo, force=true, init=false);

//删除一种语言
MyLang.getInstance().deleteLanguage(Context, LocaleInfo);

LocaleInfo 可以在以下地方找到

//1. 所有云端的语言包
MyLang.getInstance().remoteLanguages

//2. 所有下载到本地、可用的语言包
MyLang.getInstance().languages

//3. 所有非官方的语言包
MyLang.getInstance().unofficialLanguages

//4. 除内置支持的语言外,另外安装的云端的语言包
MyLang.getInstance().otherLanguages

安装

  1. 引入
//build.gradle
allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://raw.githubusercontent.com/LinXueyuanStdio/MLang/main" }
    }
}

//app/build.gradle
implementation 'com.timecat.component:MLang:1.0.1'
  1. 在 Application 中初始化,并监听系统语言的更改(如果跟随系统语言的话):
public class MyApplication extends Application {
    @SuppressLint("StaticFieldLeak")
    public static volatile Context applicationContext;
    public static volatile Handler applicationHandler;

    @Override
    public void onCreate() {
        super.onCreate();
        applicationContext = this;
        applicationHandler = new Handler(applicationContext.getMainLooper());
        MyLang.init(applicationContext);
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        MyLang.onConfigurationChanged(newConfig);
    }
}

其中建议自己新建一个静态类 MyLang 来隔绝 MLang 的 api 变化,提高兼容性和稳定性。

public class MyLang {
    public static void init(@NonNull Context applicationContext) {
        try {
            MLang.getInstance(applicationContext);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void onConfigurationChanged(@NonNull Configuration newConfig) {
        MLang.getInstance(getContext()).onDeviceConfigurationChange(getContext(), newConfig);
    }
    public static Context getContext() {
        return MyApplication.applicationContext;
    }
    ...
}

LangAction 抽象类定义了必要的网络接口,注释和demo给出了详细的说明和实现,只需照抄即可。

mlang's People

Contributors

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