Localization Library
Header image Android library for in-app language changes support in your application
Feature
- In-app language changing
- Default language when first launch
- Work with string resource in XML and programmatically
- RTL language support
- Align on platform behavior
Demo
Try it at Google Play
Download
Since version 1.2.9 will move from JCenter to MavenCentral
Gradle
(Optional) You can exclude androidx.appcompat:appcompat
, if your project does not use AppCompat.
Usage
Custom application class which extends from LocalizationApplication
is require.
Either not, using LocalizationApplicationDelegate
with additional code as below
For the activities, extends from LocalizationActivity
.
Or using LocalizationActivityDelegate
with additional code
Then prepare your multilingual content in string resource.
Public method on LocalizationActivity
It have only 4 public methods.
setLanguage
Set the language that you need to change.
For example
getLanguage
Get current language as string locale.
And 2 optional override methods.
This override method will be useful when you need to know when language has changed.
When setLanguage
was called. Current active activity will be recreated to apply the new language.
Previous activities in back stack does not change to new language immediately. Until it back to active activity again.
Action Bar or Toolbar's title
You have to call setTitle(resId)
or getActionBar().setTitle(resId)
in onCreate(onSavedInstanceState: Bundle)
to apply the new language.
Handle state changes
Activity will be recreate when language has changed as common behavior for configuration changes in Android. Any Activities or Fragments which hold the data should handle the state changes.
Change the language in Fragment
Language in fragment will depends on activity. So no need for additional code in Fragment.
Service
For normally usage, just extends from LocalizationService
Or using LocalizationServiceDelegate
with additional code
BroadcastReceiver
BroadcastReceiver is abstract class. So we cannot create LocalizationBroadcastReceiver fot you.
In this case, you need to convert the context in onReceive(context: Context, intent: Intent)
to localized context with Context.toLocalizedContext()
before using.
Language resources optimization in Android App Bundle
Change the language by library can cause a crash to your app when you publishing your app with Android App Bundle with language resources optimization enabled.
To fix this, Using the Additional Languages API in Play Core library to download the additional language before.
For more information about Additional Language API : https://android-developers.googleblog.com/2019/03/the-latest-android-app-bundle-updates.html
If you don't want to implement this feature in your code, just ignore the language resources optimization by adding the Android App Bundle configuration in your app's build.gradle
ProGuard
Normally, there's no require the ProGuard rules for this library.
But if you want to exclude this library from obfuscate and shrinking. You also can add these code to proguard-rules.pro
-keep class com.akexorcist.localizationactivity.** { *; }
-dontwarn com.akexorcist.localizationactivity.**