Android Library for easy changing locale programmatically
- Changes language in the runtime
- Persists the changes in SharedPreferences automatically
- Detects Right-To-Left (RTL) languages and updates layout direction
-
Add the JitPack repository to your build file
allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
-
Add the dependency
implementation 'com.github.ramt57:EasyLocaleApp:v1.0.3'
(Option 1) Using base classes
- Extend your app class
class MyApplication : EasyLocaleApplication() {
}
- Extend your base activity class
open class BaseActivity : EasyLocaleAppCompatActivity() {
}
It's Done
(Option 2) Using delegates
If you don't want to extend from base classes.
- On your custom Application class override
onAttach
andonConfiguration
change methods.
class MyApp : Application() {
private val localeAppDelegate = EasyLocaleApplicationDelegates()
override fun attachBaseContext(base: Context) {
super.attachBaseContext(localeAppDelegate.attachBaseContext(base))
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
localeAppDelegate.onConfigurationChanged(this)
}
}
- On your base activity class override
onAttach
open class BaseActivity : AppCompatActivity() {
private val localeDelegate = EasyLocaleActivityDelegate()
override fun attachBaseContext(newBase: Context) {
super.attachBaseContext(localeDelegate.attachBaseContext(newBase))
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
localeDelegate.initialise(this,this)
}
}
(Usage 1)
If you're using the base classes, just call setLocale(newLocale)
. It will then update the locale and restart the activity.
Example:
btn_locale.setOnClickListener { setLocale(Locales("ar") }
(Usage 2)
To change the locale you can call setLocale
on the delegate
localeDelegate.setLocale(this, newLocale)
The delegate will set the new locale and recreate the activity.
(Usage 3)
To get notified of Locale
changes implement EasyLocaleChangeListner
localeDelegate.setLocaleChangeListner(object:EasyLocaleChangeListner{
override fun onLocaleChanged(mOldLocale:Locale,mNewLocale:Locale){
...
}
})
- 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
}
- If your locale is Right-To-Left(RTL) don't forget to enable it in the
AndroidManifest.xml
<application
android:supportsRtl="true">
</application>
- 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
}
}
}
or support bundle for only those locale that your app support
defaultConfig {
...
resConfigs "en", "de"
}