dynamic-locale

Perform runtime locale changes on Android.

APACHE-2.0 License

Stars
7
Committers
1

Dynamic Locale

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. Since v2.1.0, it is dependent on Java 8 due to the dependency on Dynamic Utils. Since v2.4.1, it is targeting Java 17 to provide maximum compatibility. Since v2.5.0, the minimum SDK is Android 4.4 (API 19) to comply with the latest policies.


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.5.0'
}

Usage

It provides 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 a complete reference, please read the documentation.

Application

Implement the DynamicLocale interface for the application class by using the 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


License

Copyright 2019-2024 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.