Skip to content

$locale collected different from iOS #822

Closed

Description

@fphilipe

Version

3.71.2

Steps to Reproduce

We have an iOS and Android app that integrates the respective PostHog SDK.

Our app is available in English and German. In our Mobile Analytics dashboard on PostHog (created using the PostHog template) I see a breakdown of the languages. This breakdown contains many more languages (including country specifier such as ru-RU) than just the two the app runs in.

Digging deeper, I realized that the additional languages only come from Android. On iOS it indeed only reports just the language the app runs in, as can be seen in its source:

if #available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *) {
    if let languageCode = Locale.current.language.languageCode {
        properties["$locale"] = languageCode.identifier
    }
} else {
    if Locale.current.languageCode != nil {
        properties["$locale"] = Locale.current.languageCode
    }
}

On Android on the other hand you collect the language the device is set to:

dynamicContext["\$locale"] = "${Locale.getDefault().language}-${Locale.getDefault().country}"

Expected Result

On Android it should collect just the language that is also used for resource lookup. If my phone is set to de-CH and the app only supports English, I want to see en as the value for the $locale property.

This is how the chart looks filtered by iOS:

Image

Actual Result

This is the same chart filtered by Android:

Image

Activity

  1. dustinbyrne commented on Sep 29, 2026

    @dustinbyrne
    Contributor

    Thanks for reporting this. You’re right that Android and iOS behave differently here in two ways:

    • Android reads the default locale, which can differ from the language your app displays. iOS uses Locale.current, which accounts for the app’s supported languages and per-app language settings.
    • Android includes the country (de-CH), while iOS only sends the language (de). Our property definition describes $locale as the device locale, with examples like en-US and de-DE, so iOS dropping the country is also an inconsistency. That is separate from which language the app actually displays.

    We tested an English-only app with a resource context configured for de-CH on Android. Even Android’s app-context locale reports de-CH while the app displays English fallback strings, so it doesn’t look like switching to that API alone will fix this.

    For now, you can override $locale with the language your app uses through beforeSend. We tested this with Android SDK 3.71.2 and confirmed that the outgoing events contained the supplied en or de value instead of de-CH. Add it before setting up PostHog:

    config.addBeforeSend { event ->
        event.properties?.set("\$locale", currentAppLanguage())
        event
    }

    Replace currentAppLanguage() with your app’s language getter, returning "en" or "de" for the breakdown you described.

  2. fphilipe commented on Sep 30, 2026

    @fphilipe
    Author

    Thanks, that's what I'm doing. I rely on a trick to get the locale: I define a string resource language that gets the respective language code as the value in each values-$LANG/strings.xml file.

    I think this snippet should work as well:

    fun Context.currentLanguageCode(): String {
        val locale: Locale = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            resources.configuration.locales[0]
        } else {
            @Suppress("DEPRECATION")
            resources.configuration.locale
        }
        return locale.language   // e.g. "en", "de", "fr"
    }
  3. dustinbyrne commented on Oct 2, 2026

    @dustinbyrne
    Contributor

    I’ve opened a draft PR to explore this: #832.

    The proposed approach matches the user’s language preferences against the app’s declared supported locales. It uses Android’s LocaleConfig API on Android 13+, and reads the same packaged XML directly on Android 6–12. If nothing matches, it uses an explicitly declared default; if that information is unavailable, it keeps today’s behavior.

    This is just a proposal. It selects the best supported app language, but won’t necessarily match every displayed string because individual resources can fall back differently.

    Does your app declare a localeConfig, manually or through AGP? That would help us check whether this approach covers your setup.

  4. fphilipe commented on Oct 2, 2026

    @fphilipe
    Author

    Does your app declare a localeConfig, manually or through AGP? That would help us check whether this approach covers your setup.

    We declare it manually: android:localeConfig="@xml/locales_config" in the manifest. We don't use AGP generateLocaleConfig.

  5. marandaneto commented on Oct 5, 2026

    @marandaneto
    Member

    left a few comments in #832

  6. turnipdabeets commented on Oct 22, 2026

    @turnipdabeets
    Contributor

    @dustinbyrne @marandaneto I wonder if we should go with two properties:

    $locale: the primary device locale as a full tag (de-CH) on every platform.

    • Why: the taxonomy defines $locale as "the locale of the device" (en-US, de-DE), and every SDK can read it exactly.
    • Android: read the system locale instead of Locale.getDefault(), which can be a secondary language, a per-app choice or a value the app set itself. That's LocaleManager.systemLocales[0] on 13+, Resources.getSystem().configuration.locales[0] on 7–12, and configuration.locale on 6. Use toLanguageTag(), since the hand-built string drops the script (zh-Hans) and can produce en-.
    • iOS: switch from Locale.current.languageCode (the app's resolved language) to Locale.preferredLanguages.first. The original Objective-C SDK sent language-country, and v3 (chore: Amend the limitations with RN/Flutter not supported #75) dropped the country. @marandaneto, was that intentional?
    • Flutter gets it from the native SDKs. React Native already sends it through expo-localization / react-native-localize.

    $app_language: a new property for the language the app actually shows.

    • For a bilingual user (phone set to es, en, English-only app), the device locale says es while they read English happily. It's also what this issue asks for.
    • Detection:
    SDK How Accuracy
    iOS Bundle.main.preferredLocalizations.first High, including the per-app language choice
    Android 13+, per-app language picked LocaleManager.getApplicationLocales() High
    Android 13+, localeConfig declared LocaleConfig + LocaleList.getFirstMatch() Close guess; per-string fallback can differ
    Android 6–12 / no localeConfig Nothing reliable The app sets it
    Flutter Localizations.maybeLocaleOf(navigator.context) in PosthogObserver High, for apps using the observer (untested)
    React Native No standard i18n library The app sets it
    • Every SDK needs a way for the app to set it. Flutter and React Native apps usually translate in Dart/JS, so the native value would often be wrong there.
    • Why not feat(android): resolve locale against declared app languages #832: covering Android 6–12 needed an AndroidX Core bump, reflection, hand-parsed XML and a lookup per event, and still wasn't exact. Using only the 13+ public APIs keeps it small.

    What we keep: iOS's current value moves to $app_language, and Android's becomes an exact device locale. Nothing is lost.

    Both platforms' $locale would mean the same thing. The reporter declares localeConfig, so $language is filled automatically on iOS and Android 13+, and they set it on Android 6–12. Their chart (and maybe the Mobile Analytics template) would break down by $app_language.

    Breaking change: iOS $locale values change from de to de-CH, and iOS charts by $locale change meaning, so I'd ship it in v4 with a migration note. We can put both iOS and Android changes in the major release branches.

  7. posthog commented on Oct 23, 2026

    @posthog
    Contributor

    PostHog self-driving picked up this issue and researched it. Somebody may already be working on it, so ask before you start a fix.

    Report (team access only): https://us.posthog.com/project/2/inbox/reports/01a169ef-9ba3-7c2a-a426-2e2a9414f51a?link_source=github_comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions