如何切换应用全局语言并重启后保留所选语言设置?
Hey there! Let's work through your language switching and persistence needs—this is a common requirement, and I’ve got you covered with step-by-step solutions tailored to your use case.
First, ensure your language resources are structured correctly (this example uses Android conventions, but core logic applies to other platforms too):
- English: Store all strings in
values/strings.xml - Hindi:
values-hi/strings.xml - Marathi:
values-mr/strings.xml
Next, create a helper method to update the app's locale and apply changes immediately:
fun setAppLanguage(context: Context, languageCode: String) { val targetLocale = Locale(languageCode) Locale.setDefault(targetLocale) // Update app configuration to use the new locale val config = context.resources.configuration config.setLocale(targetLocale) context.resources.updateConfiguration(config, context.resources.displayMetrics) // Restart the current activity to refresh all UI elements with the new language (context as Activity).run { finish() startActivity(intent) } }
Hook this method up to your language selection buttons in the language picker page:
btnEnglish.setOnClickListener { setAppLanguage(this, "en") } btnHindi.setOnClickListener { setAppLanguage(this, "hi") } btnMarathi.setOnClickListener { setAppLanguage(this, "mr") }
To retain the user’s language choice after closing the app, you’ll need to store the selected language code locally. We’ll use SharedPreferences (Android) here—similar tools like UserDefaults (iOS) or SharedPreferences (Flutter) work for other platforms.
First, add a method to save the selected language:
fun saveSelectedLanguage(context: Context, languageCode: String) { val prefs = context.getSharedPreferences("AppPreferences", Context.MODE_PRIVATE) prefs.edit().putString("selected_language", languageCode).apply() }
Update your button click handlers to save the language before switching:
btnEnglish.setOnClickListener { saveSelectedLanguage(this, "en") setAppLanguage(this, "en") } // Repeat the same pattern for Hindi and Marathi buttons
Finally, load the saved language when the app starts. The best place to do this is in your custom Application class (so it applies to all activities):
class MyApplication : Application() { override fun onCreate() { super.onCreate() // Load saved language, default to English if no preference exists val savedLanguage = getSavedLanguage(this) setAppLanguage(this, savedLanguage) } private fun getSavedLanguage(context: Context): String { val prefs = context.getSharedPreferences("AppPreferences", Context.MODE_PRIVATE) return prefs.getString("selected_language", "en") ?: "en" } }
Quick Tips
- Avoid hardcoded text: Make sure every UI element’s text comes from your language resource files—this ensures the language switch affects the entire app.
- Cross-platform adjustments: If you’re using Flutter, use the
flutter_localizationspackage and set thelocaleproperty of yourMaterialAppbased on the saved value. For iOS, useUserDefaultsto store the language code and update the app’s locale on launch. - Test thoroughly: Verify that Hindi and Marathi characters render correctly across different devices and screen sizes.
内容的提问来源于stack exchange,提问作者Nikhil Shende

