AndroidLocaleManger is a locale manager for Android:
- Change locale at runtime
- Supports multiple language
- Change locale according to system locale
- Easy to use
- Api 14+
Add JitPack to repositories in your project's root build.gradle
file:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add the dependency to your module's build.gradle
file:
dependencies {
...
implementation 'com.github.MostafaTaghipour:localemanager:1.0.0'
}
- Wrap the
Activity
Context:
override fun attachBaseContext(newBase: Context) {
super.attachBaseContext(LocaleManager.getInstance().wrapContext(newBase))
}
- Any time you need to change the locale of the application using the following code
LocaleManager.getInstance().setCurrentLocale(this /*context*/, Locale("fa" /* your desired language*/))
- Thats it, enjoy it
There is a BroadcastReceiver that fired when app locale did changed
private lateinit var localeReceiver: AppLocaleChangeReceiver
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//register BroadcastReceiver
val filter = IntentFilter()
filter.addAction(LocaleManager.APP_LOCALE_CHANGED_BROADCAST_ACTION)
this.localeReceiver = AppLocaleChangeReceiver()
this.localeReceiver.setListener(this)
registerReceiver(this.localeReceiver, filter)
}
override fun onAppLocaleChanged(newLocale: Locale) {
// app locale changed
}
override fun onDestroy() {
super.onDestroy()
//unregister BroadcastReceiver
unregisterReceiver(this.localeReceiver)
}
Mostafa Taghipour, mostafa@taghipour.me
AndroidLocaleManager is available under the MIT license. See the LICENSE file for more info.