Giter Site home page Giter Site logo

alybahary / locale-helper-android Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zeugma-solutions/locale-helper-android

0.0 1.0 0.0 235 KB

Change Language Programmatically in Android

Home Page: https://gunhansancar.com/change-language-programmatically-in-android/

License: Apache License 2.0

Kotlin 94.18% Java 5.82%

locale-helper-android's Introduction

Change Language Programmatically in Android

Header image

This is a helper library to change the language programmatically in Android.

Android by default uses the locale of the device to select the appropriate language dependent resources. And most of the time this behaviour is enough for common applications.

However, there are cases where you would want to change the language of your app and the UI of the app. As a result, LocaleHelper has emerged.

Download

implementation 'com.zeugmasolutions.localehelper:locale-helper-android:1.5.1'

Features

  1. Changes language on-the-fly
  2. Persists the changes in Preferences automatically
  3. Detects changes when activity loads from backstack
  4. Detects Right-To-Left (RTL) languages and updates layout direction
  5. Supports DayNight themes
  6. Small footprint (~3KB, ~50 methods), easy to use

Demo

Demo video

Demo source code

Setup

(Option 1) Using base classes

  1. Extend your app class
class App : LocaleAwareApplication() {
}
  1. Extend your base activity class
open class BaseActivity : LocaleAwareCompatActivity() {  
}

LocaleAwareCompatActivity provides a helper method called updateLocale

That's it.

(Option 2) Using delegates

This option requires you to do extra steps if you don't want to extend from base classes.

  1. On your custom Application class override methods below. For more details check: LocaleAwareApplication
class MyApp : Application() {  
    private val localeAppDelegate = LocaleHelperApplicationDelegate()

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(localeAppDelegate.attachBaseContext(base))
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        localeAppDelegate.onConfigurationChanged(this)
    } 
    
    override fun getApplicationContext(): Context =
    	LocaleHelper.onAttach(super.getApplicationContext())
}
  1. On your base activity class override methods below. For more details check: LocaleAwareCompatActivity
open class BaseActivity : AppCompatActivity() {  
    private val localeDelegate: LocaleHelperActivityDelegate = LocaleHelperActivityDelegateImpl()

    override fun getDelegate() = localeDelegate.getAppCompatDelegate(super.getDelegate())

    override fun attachBaseContext(newBase: Context) {
        super.attachBaseContext(localeDelegate.attachBaseContext(newBase))
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        localeDelegate.onCreate(this)
    }

    override fun onResume() {
        super.onResume()
        localeDelegate.onResumed(this)
    }

    override fun onPause() {
        super.onPause()
        localeDelegate.onPaused()
    }

    override fun createConfigurationContext(overrideConfiguration: Configuration): Context {
        val context = super.createConfigurationContext(overrideConfiguration)
        return LocaleHelper.onAttach(context)
    }

    override fun getApplicationContext(): Context =
        localeDelegate.getApplicationContext(super.getApplicationContext())

    open fun updateLocale(locale: Locale) {
        localeDelegate.setLocale(this, locale)
    } 
}

Usage

(Option 1)

If you're using the base classes, just call updateLocale(newLocale). It will then update the locale and restart the activity.

Example:

toTRButton.setOnClickListener { updateLocale(Locales.Turkish) }

In java.util.Locale class most of the common Locales and their variants are defined. However, it doesn't contain all the Locales so com.zeugmasolutions.Locales provides the missing ones for easy access.

(Option 2)

To change the locale you can call setLocale on the delegate

localeDelegate.setLocale(this, locale)

The delegate will set the new locale and recreate the activity.

Notes

  1. actionbar(toolbar) title should be set when onCreate is called.
override fun onCreate(savedInstanceState: Bundle?) {
	super.onCreate(savedInstanceState)
	setContentView(R.layout.activity_main) //sample

	setTitle(R.string.main_activity_title) //sample
}
  1. If your locale is Right-To-Left(RTL) don't forget to enable it in the AndroidManifest.xml
<application
	android:supportsRtl="true">
</application>
  1. Google introduced a new App Bundle format to split apk files in smaller sizes when they’re being installed on the client devices. However, this means that we cannot have dynamic language changes in our applications.

To prevent that split for language files we need to add extra lines in our build.gradle file inside the app folder like below.

android {
    //...
    //... removed for brevity
    bundle {
        language {
            enableSplit = false
        }
    }
}

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.