Android application development, switch display language within the application
Android application development, switch display language within the application
Category:Android
Pub.date:2021-03-16
How to switch the display language within the app in Android app development.
For example, if you want to display English on the device even in a Japanese environment.
Android Studio Version 4.1.2
Kotlin 1.4.31
Part of the code
class MainActivity : AppCompatActivity() {
private var localeLanguage: String = "" // "" or "en" or "jp" or etc.
companion object {
internal const val SETTINGS: String = "settings"
internal const val LOCALE_LANGUAGE: String = "localeLanguage"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
//Save localeLanguage
//Save a string such as "en" or "ja" with some action
//You may restart Activity by executing recreate() at the timing of saving.
private fun saveLocaleLanguage() {
getSharedPreferences(SETTINGS, Context.MODE_PRIVATE).edit().apply {
putString(LOCALE_LANGUAGE, this@MainActivity.localeLanguage)
apply()
}
}
//Language setting
//Switch the language when the Activity starts.
//Prepare as many strings.xml as you need.
override fun attachBaseContext(base: Context) {
val pref = base.getSharedPreferences(SETTINGS, Context.MODE_PRIVATE)
this.localeLanguage = pref.getString(LOCALE_LANGUAGE, "") ?: ""
val loc: Locale? = if (this.localeLanguage != "") Locale(this.localeLanguage) else null
if (loc != null) {
val res = base.resources
val config = Configuration(res.configuration)
val localeList = LocaleList(loc)
LocaleList.setDefault(localeList)
config.setLocales(localeList)
super.attachBaseContext(base.createConfigurationContext(config))
} else {
super.attachBaseContext(base)
}
}
}
Prepare as many strings.xml as you need
An example of preparing all 11 languages with English as the default
main/res/values/strings.xml
main/res/values-de/strings.xml
main/res/values-es/strings.xml
main/res/values-fr/strings.xml
main/res/values-it/strings.xml
main/res/values-ja/strings.xml
main/res/values-nl/strings.xml
main/res/values-pl/strings.xml
main/res/values-pt/strings.xml
main/res/values-ru/strings.xml
main/res/values-zh/strings.xml
The arguments given to Locale() are as follows.
"en"
"de"
"es"
"fr"
"it"
"ja"
"nl"
"pl"
"pt"
"ru"
"zh"
Support for SDK Version 23 and below
//Language setting
override fun attachBaseContext(base: Context) {
val pref = base.getSharedPreferences(SETTINGS, Context.MODE_PRIVATE)
this.localeLanguage = pref.getString(LOCALE_LANGUAGE, "") ?: ""
val loc: Locale? = if (this.localeLanguage != "") Locale(this.localeLanguage) else null
if (loc != null) {
val res = base.resources
val config = Configuration(res.configuration)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //minSdkVersion 24
val localeList = LocaleList(loc)
LocaleList.setDefault(localeList)
config.setLocales(localeList)
} else { //minSdkVersion 17
config.setLocale(loc)
}
super.attachBaseContext(base.createConfigurationContext(config))
} else {
super.attachBaseContext(base)
}
}
CONTENTS
JavaScript
Unreal Engine
CakePHP4
CakePHP4
Flutter
Flutter
Other
JavaScript
JavaScript
CakePHP4
Web Server
Photoshop
Unreal Engine
CakePHP4
Web Browser
Web Server
iOS
Android
Web Browser
CakePHP4
Plesk
Illustrator
Plesk
Web Server
Web Server
CakePHP3
Web Browser
CakePHP3
JavaScript
JavaScript
CakePHP3
CakePHP3