Coder Social home page Coder Social logo

phoenix-626 / dynamic-locale Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pranavpandey/dynamic-locale

0.0 0.0 0.0 773 KB

A library to perform runtime locale changes on Android.

Home Page: https://pranavpandey.org/dynamic-locale

License: Apache License 2.0

Java 100.00%

dynamic-locale's Introduction

Dynamic Locale

License Build Status Release

A library to perform runtime locale changes on Android 4.1 (API 16) and above.

It uses AndroidX so, first migrate your project to AndroidX.


Contents


Installation

It can be installed by adding the following dependency to your build.gradle file:

dependencies {
    // For AndroidX enabled projects.
    implementation 'com.pranavpandey.android:dynamic-locale:2.0.0'
}

Usage

DynamicLocale is an interface that can be implemented in the Application or Activity class to provide the modified base context. It has optional feature to provide font scale to make the text smaller or larger. It would be beneficial for some specific locales and theme styles.

For complete reference, please read the documentation.

Application

Implement the DynamicLocale interface for the Application class by using the DynamicLocaleUtils helper class and register it in the AndroidManifest to apply the locale at runtime.

While using it for the application class, you should update the context when user configuration changes as Android caches the application in the memory and the attachBaseContext(Context) might not be called each time.

Please check an example here.

public class DynamicApp extends Application implements DynamicLocale {

    @Override
    public void attachBaseContext(@NonNull Context base) {
        // Set the dynamic locale.
        super.attachBaseContext(setLocale(base));
    }    

    @Override
    public @Nullable String[] getSupportedLocales() {
        // Returns an array of supported locales.
        return new String[] {
            Locale.ENGLISH.toString(),
                Locale.GERMAN.toString(),
                new Locale(DynamicLocaleUtils.ADS_LOCALE_SPANISH, "").toString(),
                new Locale(DynamicLocaleUtils.ADS_LOCALE_INDONESIA, "").toString(),
                Locale.ITALIAN.toString(),
                new Locale(DynamicLocaleUtils.ADS_LOCALE_TURKISH, "").toString(),
                Locale.CHINESE.toString() };
    }

    @Override
    public @NonNull Locale getDefaultLocale(@NonNull Context context) {
        // Returns the default locale to be used if no dynamic locale support is provided.
        return DynamicLocaleUtils.getDefaultLocale(context, getSupportedLocales());
    }
    
    @Override
    public @Nullable Locale getLocale() {
        // Returns the locale to be applied.
        return DynamicLocaleUtils.toLocale(DynamicLocaleUtils.ADS_LOCALE_HINDI);
    }

    @Override
    public @NonNull Context setLocale(@NonNull Context context) {
        // Apply the locale according to the configuration.
        return DynamicLocaleUtils.setLocale(context,
                DynamicLocaleUtils.getLocale(getLocale(),
                    getDefaultLocale(context)), getFontScale());
    }

    @Override
    public float getFontScale() {
        // Returns the font scale to be applied.
        // Use the default font scale.
        return 1f;
    }
}

Activity

Similarly, it can be used for the Activity to apply locale at runtime.

You should recreate() the activity when user configuration changes.

public class DynamicActivity extends Activity implements DynamicLocale {

    @Override
    public void attachBaseContext(@NonNull Context base) {
        // Set the dynamic locale.
        super.attachBaseContext(setLocale(base));
    }    

    ...

    @Override
    public @NonNull Context setLocale(@NonNull Context context) {
        // Apply the locale according to the configuration.
        return DynamicLocaleUtils.setLocale(context,
                DynamicLocaleUtils.getLocale(getLocale(),
                    getDefaultLocale(context)), getFontScale());
    }

    ...
}

Dependency

It depends on the dynamic-utils to perform various internal operations. So, its functions can also be used to perform other useful operations.


Author

Pranav Pandey

GitHub Follow on Twitter Donate via PayPal


License

Copyright 2019-2021 Pranav Pandey

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.

dynamic-locale's People

Contributors

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