You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何切换应用全局语言并重启后保留所选语言设置?

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.

1. Switching the Entire App to the Selected Language

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")
}
2. Persisting the Selected Language Across App Restarts

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_localizations package and set the locale property of your MaterialApp based on the saved value. For iOS, use UserDefaults to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:18:57