As any user, you may want to view your app in a language that is different from what the device is currently using. An app that can change its language on the fly is an easy choice for those who want to use it and for someone who wants to distribute it globally to anyone, regardless of the device language setting. This article is devoted to an Android library that can be used to implement in-app language changes support.
The Android library that provides a simple way for your app to change its language at runtime. You can change the language in your app without restarting it (even if the device changes its locale). In-app language changes support in your app: ➤ It is locale independent ➤ It has a simple API ➤ It has a compact size ➤ It is simple and easy to use ➤ It can save and load language translations, and supports static, dynamic and bundled translations ➤ It can specify the device’s locale at runtime ➤ It can change the language of your app in the same activity ➤ It can change the language of your app in
Location Library
Android library with a header image to support language changes in your application.
- To change the language of an application
- Default language on first start-up
- Working with string sources in XML and programmatically
- Support of RTL language
- Harmonisation of behaviour on the platform
Try it on Google Play
Starting with version 1.2.9, there will be a transition from JCenter to MavenCentral.
// build.gradle (project)
allprojects {
repositories {
mavenCentral()
/* . */
}
}
Gradle
Implementation of ‘com.akexorcist:localization:1.2.10’.
(Optional) You can exclude androidx.appcompat:appcompat if your project does not use AppCompat.
implementation (‘com.akexorcist:localization:1.2.10’) {
exclude group : ‘androidx.core’, module : ‘core’
}
A custom application class is needed, which is an extension of LocalizationApplication.
Class MainApplication : LocalizationApplication() {
/* … */
override fun getDefaultLanguage() = Locale.ENGLISH
}
Or not, by using the LocalizationApplicationDelegate with additional code as shown below
Class MainApplication : Application() {
private val localizationDelegate = LocalizationApplicationDelegate()
override fun attachBaseContext(base : Context) {
localizationDelegate.setDefaultLanguage(base, Locale.ENGLISH)
super.attachBaseContext(localizationDelegate.attachBaseContext(base))
}
override fun onConfigurationChanged(newConfig : Configuration) {
super.onConfigurationChanged(newConfig)
localizationDelegate.onConfigurationChanged(this)
}
override fun getApplicationContext() : Context {
return localizationDelegate.getApplicationContext(super.getApplicationContext())
}
override fun getResources() : Resources {
return localizationDelegate.getResources(baseContext, super.getResources())
}
}
For the activity, expanded from LocalizationActivity.
Class MainActivity : LocalizationActivity() {
/* … */
}
Or with LocalizationActivityDelegate with additional code
Open the class CustomActivity: Activity(), OnLocalChangedListener {
private trap localizationDelegate = LocalizationActivityDelegate(this)
public override fun onCreate(savedInstanceState : Bundle ?) {
localizationDelegate.addOnLocaleChangedListener(this)
localizationDelegate.onCreate()
super.onCreate(savedInstanceState)
}
public override fun onResume() {
super.onResume()
localizationDelegate.onResume(this)
}
override fun attachBaseContext(newBase : Context) {
applyOverrideConfiguration(localizationDelegate.updateConfigurationLocal(newBase))
super.attachBaseContext(newBase)
}
override fun getApplicationContext() : Context {
return localizationDelegate.getApplicationContext(super.getApplicationContext())
}
override fun getResources() : Resources {
return localizationDelegate.getResources(super.getResources())
}
fun setLanguage(language : String ?) {
localizationDelegate.setLanguage(this, language !!)
}
fun setLanguage(locale : Locale ?) {
localizationDelegate.setLanguage(this, locale !!)
}
val currentLanguage : localization
get() = localizationDelegate.getLanguage(this)
// Just the change locale event method override
override fun onBeforeLocaleChanged() {}
override fun onAfterLocaleChanged() {}
}
Then prepare the multilingual content in a string resource.
Public method on Locality Activity
It has only 4 public methods.
fun setLanguage(language : String)
fun setLanguage(language : String, country : Strinng)
fun setLanguage(locale : Locale)
fun getCurrentLanguage() : The line is
setLanguage Sets the language to be changed.
For example
setLanguage(th) // Language : Thailand
setLanguage(th, TH) // Language : Thailand, Country : Thai
setLanguage(Locale(th, TH)) // Language : Thailand, Country : Thai
setLanguage(s) // Language : English
setLanguage(s), GB // Language : English, Country : UK
setLanguage(s), US) // Language : English, Country : United States
setLanguage(Locale(s, US)) // Language : English, Country : United States
setLanguage(Locale.KOREA) // Language : Korean, Land: Korea
setLanguage(Locale.KOREAN) // Language : Korean
setLanguage(Locale.CANADA_ENCH) // Language : French, Land: Canada
getLanguage Gets the current language as a local string.
And two additional breaking methods.
fun onBeforeLocaleChanged()
fun onAfterLocaleChanged()
This overloaded method is useful when you need to know when the language has changed.
If setLanguage is called. Current active activities are recreated to apply the new language.
Previous actions in the backstack are not immediately transferred to the new language. Until it becomes active again.
You must call setTitle(resId) or getActionBar().setTitle(resId) in onCreate(onSavedInstanceState : Bundle) to apply the new language.
Class MainActivity : LocalizationActivity() {
override fun onCreate(savedInstanceState : Bundle ?) {
/* … */
setTitle(R.string.main_activity_title)
}
}
Status change processing
The activity is recreated when the language is changed, which is the normal behavior when changing the configuration in Android. All actions or fragments that store data must process state changes.
Language change in fragment
The language of the fragment depends on the activity. Therefore, no additional code is needed in the fragment.
Service
For normal use it is sufficient to extend the LocalizationService
SimpleService class: LocalizationService() {
/* … */
}
Or you can use the LocalizationServiceDelegate with additional code
abstract class CustomService : Service() {
private val localizationDelegate : LocalizationServiceDelegate by lazy {
LocalizationServiceDelegate(this)
}
override fun getBaseContext() : Context {
return localizationDelegate.getBaseContext(super.getBaseContext())
}
override fun getApplicationContext() : Context {
return localizationDelegate.getApplicationContext(super.getApplicationContext())
}
override fun getResources() : Resources {
return localizationDelegate.getResources(super.getResources())
}
}
Broadcast receiver
BroadcastReceiver is an abstract class. Therefore we cannot make a LocalizationBroadcastReceiver for you.
In this case, you must convert the context in onReceive(context:Context, intent:Intent) to a localized context using Context.toLocalizedContext() before using it.
SimpleBroadcastReceiver class: BroadcastReceiver() {
override fun onReceive(context : Context, intent : Intent) {
val localizedContext = context.toLocalizedContext()
/* . */
}
}
Changing the language via the library may cause the application to fail if released with the Android App Bundle with language source optimization enabled.
To solve this problem, use the Additional Languages API in the Play Core library to load an additional language earlier.
Learn more about the 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, simply ignore the language source optimization by adding the Android App Bundle configuration to your application’s build.gradle file.
android {
/* … */
bundle {
language {
enableSplit = false
}
}
}
Normally, ProGuard rules are not needed for this library.
But if you want to exclude this library from obfuscation and reduction. You can also paste this code into the proguard-rules.pro file
-backup class com.akexorcist.localizationactivity.** { * ; }
-dontwarn com.akexorcist.localizationactivity.**
GitHub
https://github.com/akexorcist/Localization
This source has been very much helpful in doing our research. Read more about multi language library android github and let us know what you think.
Frequently Asked Questions
How do I change the default language in Android Apps?
The Android’s operating system was developed by Google, which is the main reason for its success. It has a lot of apps that can be used for a variety of purposes and all these apps are available to the users in many different languages. The only thing that the users have to do in order to use apps in different languages is to select a particular language as the default language for the app. Most of the applications on your Android device are written in the default language that you set during the initial language configuration of the device. However, you can always change this language to another language of your choice, using the settings menu. Let’s see how this can be done. We can change the default language of the device by changing the locale. However, for this, we first need to know the locale supported by our application, the locale that is supported (if any) by the device, and a few other attributes that we need to take care before actually making the change. We can do this by using the android.provider.Locale class.
How can I add locale language in Android?
Also called “localization”, adding locale language in Android is easy to do by yourself. It’s a good idea if you are about to release your app or game to an international market. In this article, you’ll find some useful methods and tools to start your localization project. Android devices are popular in various countries, and some apps support multiple languages. So, how can we add a locale language in Android? The first step is to add the locale language into AndroidManifest.xml. Android locales are defined by the android:locale attribute on the There are two ways to get your Android multilingual. The first one is to buy a phone that supports multilingual, such as the Samsung Galaxy S9, which is available in three languages, English, Korean, and Chinese. The second way is to install a language pack yourself, which is what we will be covering in this tutorial. (website name) will be using a Samsung Galaxy S10 as an example, but if you have an older phone, the process will be mostly the same. We’ve all been there: you get a new Android phone and you can barely use it, because you can’t read any of the interface text. If you’re lucky, you can change the language settings and get back to normal—but if you’re not, you have to hope for a future update or root your phone just to switch languages. We’ve got some good news for you: there is a hidden way to change the language of every application on your phone, no rooting required. All you need is ADB, Android’s developer tools. multi language library android githubandroid multi language app example githubhow to create multi language app in androidandroid change language without restarting activitychange app language programmatically in androidhow to change whole app language in android programmatically,People also search for,Feedback,Privacy settings,How Search works,change app language programmatically in android,how to change whole app language in android programmatically,programmatically change language in android application github,multi language library android github,programmatically change language in android application github java,android multi language app example github,how to create multi language app in android,android change language without restarting activityRelated Tags: