运行时切换XML字符串语言:点击按钮切换印地语失效
Hey there! Let's walk through why your Hindi language switch isn't working and how to fix both the button-triggered switch and XML string localization.
1. First, Double-Check Your Hindi Resource Setup
The most common issue here is missing or incorrectly named resource files. Make sure you have:
- A
values-hifolder inside yourres/directory - A
strings.xmlfile insidevalues-hiwith all your translated Hindi strings. Example:
<!-- res/values-hi/strings.xml --> <resources> <string name="app_name">मेरा ऐप</string> <string name="switch_hindi">हिंदी में बदलें</string> <!-- Add translations for every string you use in your app --> </resources>
2. Implement the Correct Locale Switch Logic
If your current code isn't switching locales properly, try this robust method. It updates the app configuration and ensures the changes take effect immediately:
Kotlin Example:
// Add this method to your Activity or a dedicated LocaleHelper class fun switchToHindi() { val hindiLocale = Locale("hi") Locale.setDefault(hindiLocale) val config = resources.configuration.apply { setLocale(hindiLocale) setLayoutDirection(hindiLocale) // Important for right-to-left formatting if needed } // Update the app's resources with the new locale resources.updateConfiguration(config, resources.displayMetrics) // Recreate the activity to apply all UI changes recreate() } // Attach this to your button click listener binding.btnSwitchHindi.setOnClickListener { switchToHindi() }
For Android API 24+ (Alternative Approach):
If targeting newer versions, use createConfigurationContext to avoid configuration leaks:
fun switchToHindi() { val hindiLocale = Locale("hi") val config = resources.configuration.apply { setLocale(hindiLocale) } val context = createConfigurationContext(config) resources.updateConfiguration(config, resources.displayMetrics) // Recreate activity to apply changes recreate() }
3. Handling XML String Localization
How you handle XML strings depends on where they're stored:
Case 1: XML is a String Resource
If your XML content is stored as a string in strings.xml, just fetch it after switching locales—Android will automatically pull the Hindi version from values-hi/strings.xml:
// After switching locale, get the localized XML string val localizedXmlContent = getString(R.string.your_xml_string_key) // Parse or display this XML as needed
Case 2: XML is a Raw Resource
If your XML is in the raw/ folder, create a raw-hi folder and place the Hindi version of the XML there. Then load it using:
val inputStream = resources.openRawResource(R.raw.your_xml_file) // Use an XML parser (like XmlPullParser) to process the input stream
Case 3: Dynamic XML Content
If generating XML on the fly, insert localized strings using getString(R.string.your_key) after the locale switch to ensure Hindi text is used.
Troubleshooting Quick Checks
- Verify Locale: Add a log to confirm the locale changed:
Log.d("LocaleCheck", Locale.getDefault().language)should printhiafter switching. - Missing Translations: Any strings not present in
values-hi/strings.xmlwill fall back to the default language—double-check your translations. - Manifest Declaration: For API 33+, declare supported locales in your manifest to ensure the system recognizes Hindi:
<!-- AndroidManifest.xml --> <application ... android:localeConfig="@xml/locales_config"> </application> <!-- res/xml/locales_config.xml --> <locale-config xmlns:android="http://schemas.android.com/apk/res/android"> <locale android:name="en"/> <locale android:name="hi"/> </locale-config>
Give these steps a try—they should resolve both the button switch issue and XML string localization!
内容的提问来源于stack exchange,提问作者Manik

