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.
implementation 'com.zeugmasolutions.localehelper:locale-helper-android:1.0.2'
- Changes language on-the-fly
- Persists the changes in
Preferences
automatically - Detects changes when activity loads from backstack
- Detects Right-To-Left (RTL) languages and updates layout direction
- Small footprint (~3KB, ~50 methods), easy to use
(Option 1) Using base classes
- Extend your app class
class App : LocaleAwareApp() {
}
- 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.
- On your custom Application class override
onAttach
andonConfiguration
change methods.
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)
}
}
- On your base activity class override
onAttach
and add a helper method
open class BaseActivity : AppCompatActivity() {
private val localeDelegate = LocaleHelperActivityDelegateImpl()
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()
}
open fun updateLocale(locale: Locale) {
localeDelegate.setLocale(this, locale)
}
}
(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 directly call setLocale
LocaleHelper.setLocale(locale:Locale)
Then you have to handle activity recreation by yourself calling activity.recreate()
- 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
}
}
}