GithubHelp home page GithubHelp logo

apollonbar / multilanguages Goto Github PK

View Code? Open in Web Editor NEW

This project forked from michaeljokar/multilanguages

0.0 0.0 0.0 156 KB

android multi language support (support android O+)

Java 70.58% Groovy 29.42%

multilanguages's Introduction

Android 多语言切换(兼容8.0+) 2.0版本,一句代码完成多语言切换,现在支持第三方包里多语言切换(前提是有对应的语言资源)

2.0版本使用Transform API 编译插桩的方式来实现Activity,ServiceattachBaseContext方法覆盖重写

使用

  • 引入gradle plugin

    classpath 'com.github.jokar:multi-languages.plugin:0.0.3'
    
  • app buidle.gradle 文件引入plugin

    apply plugin: 'multi-languages'
    

    可以配置开关来控制是否重写(插件会耗时一部分的编译时间)

    multiLanguages {
        enable = true
    }
    
  • 导入Library

    implementation 'com.github.jokar:multi-languages:0.0.4'
    
  • application init

    public class MultiLanguagesApp extends Application {
    @Override
    protected void attachBaseContext(Context base) {
        //第一次进入app时保存系统选择语言(为了选择随系统语言时使用,如果不保存,切换语言后就拿不到了)
        LocalManageUtil.saveSystemCurrentLanguage(base);
        super.attachBaseContext(MultiLanguage.setLocal(base));
    }
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        //用户在系统设置页面切换语言时保存系统选择语言(为了选择随系统语言时使用,如果不保存,切换语言后就拿不到了)
        LocalManageUtil.saveSystemCurrentLanguage(getApplicationContext(), newConfig);
        MultiLanguage.onConfigurationChanged(getApplicationContext());
    }
    
    @Override
    public void onCreate() {
        super.onCreate();
        MultiLanguage.init(new LanguageLocalListener() {
            @Override
            public Locale getSetLanguageLocale(Context context) {
                //返回自己本地保存选择的语言设置
                return LocalManageUtil.getSetLanguageLocale(context);
            }
        });
        MultiLanguage.setApplicationLanguage(this);
    }
    }
    

    LocalManageUtil里做的是保存选择的语言设置

以上就完成了初始化了,

博客

多语言实现


效果图

multilanguages's People

Contributors

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