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

Android Oreo本地化失效求助:如何设置与获取默认Locale?

Fixing Localization Issues on Android Oreo: How to Set & Get Default Locale

Hey there! I’ve run into this exact localization problem on Android Oreo (API 26) before—Google changed how Locale management works starting with this version, which is why your strings are stuck on the device’s default language. Let me walk you through the correct way to handle setting and retrieving Locales for your app.

First: Understanding the Oreo Locale Change

Before Oreo, you could directly modify the Configuration object to change the app’s Locale, but starting with API 26, Google introduced LocaleManager to manage system and app-level Locales. Directly modifying Configuration no longer persists across app restarts, and the system may override your changes.


How to Get the Default Locale

The method depends on whether you want the system default Locale or your app’s current Locale:

1. Get System Default Locale

  • For API ≥26 (Oreo+):
    val systemLocale = LocaleManager.getDefault().configuration.locales[0]
    
  • For API <26:
    val systemLocale = Resources.getSystem().configuration.locale
    // Or simply Locale.getDefault() (works across versions but may reflect app-level changes)
    

2. Get Your App’s Current Locale

This is what you’ll need to check if your app’s localization is applied correctly:

  • For API ≥33 (Tiramisu+):
    val appLocale = LocaleManager.getApplicationLocaleManager(context)
        .applicationLocales[0]
    
  • For API 26–32 (Oreo to S):
    val appLocale = AppCompatDelegate.getApplicationLocales()[0] ?: Locale.getDefault()
    
  • For API <26:
    val appLocale = context.resources.configuration.locale
    

How to Set the App’s Default Locale (Fix Your Localization Issue)

To make your app use a specific Locale (and keep it applied across restarts), follow these steps based on your target API level:

If you’re using the AppCompat library (which you should be for broader compatibility), this method works for API 26+:

fun setAppLocale(context: Context, locale: Locale) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        // API 33+ use LocaleManager directly
        LocaleManager.getApplicationLocaleManager(context)
            .setApplicationLocales(LocaleList.forLanguageTags(locale.toLanguageTag()))
    } else {
        // API 26–32 use AppCompatDelegate
        AppCompatDelegate.setApplicationLocales(LocaleListCompat.create(locale))
    }
    // Recreate the current Activity to apply changes immediately
    (context as? Activity)?.recreate()
}

For API <26 (Legacy Devices)

If you still support devices below Oreo, add this fallback:

fun setAppLocale(context: Context, locale: Locale) {
    when {
        Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> {
            LocaleManager.getApplicationLocaleManager(context)
                .setApplicationLocales(LocaleList.forLanguageTags(locale.toLanguageTag()))
        }
        Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
            AppCompatDelegate.setApplicationLocales(LocaleListCompat.create(locale))
        }
        else -> {
            val config = context.resources.configuration
            config.locale = locale
            context.resources.updateConfiguration(config, context.resources.displayMetrics)
            Locale.setDefault(locale)
        }
    }
    (context as? Activity)?.recreate()
}

Key Notes to Fix Your Localization Problem:

  • Place this code in your Application class: Call setAppLocale() in onCreate() to ensure the Locale is set before any Activities load.
  • Check your resource directories: Make sure your localized strings are in the correct folders, e.g., values-zh-rCN for Simplified Chinese, values-en-rUS for American English.
  • Restart the app if needed: If you change the Locale after the app has launched, you’ll need to recreate Activities or prompt the user to restart the app for all strings to update.

Why Your Strings Were Stuck on Default Language

On Oreo+, directly modifying Configuration doesn’t persist, and the system will reset to the device’s default Locale when the app restarts. Using the LocaleManager or AppCompatDelegate methods ensures your app’s Locale preference is saved and applied consistently.

内容的提问来源于stack exchange,提问作者Rajalakshmi Arumugam

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:28:05