Issue: getLocales() returns locales alphabetically instead of user preference order

I’m using @amazon-devices/react-native-localize and noticed something unexpected.

According to the documentation, getLocales() should return the user’s preferred locales in priority order. However, in my case, it seems to return the list sorted alphabetically.

For example, my device is set to en-US as the primary language, but the output from getLocales() looks like this:

[
  { "countryCode": "", "isRTL": false, "languageCode": "af", "languageTag": "af", "scriptCode": "" },
  { "countryCode": "NA", "isRTL": false, "languageCode": "af", "languageTag": "af-NA", "scriptCode": "" },
  ...
]

I was expecting en-US to appear first since it’s the device’s preferred locale.

Has anyone else faced this issue? Is this a bug or am I missing something? Any workaround would be appreciated.

Hi @Saras_verma,

Welcome to Amazon Developer Community!!

Thank you for your question about the getLocales() API behavior. I tried to reproduce the issue you reported and was able to observe the same behavior where getLocales() returns locales in alphabetical order instead of user preference order.

Our team is currently investigating this issue. While we work on a resolution, I would like to suggest a workaround that may help. You can try using JavaScript’s Intl API to detect and correct the locale ordering:

// Get raw locales from the API
const rawLocales = getLocales();

// Pull the real system locale using Intl API
const realPrimary = Intl.DateTimeFormat().resolvedOptions().locale;

// Find and move the real locale to first position
const primaryIndex = rawLocales.findIndex(l =>
  l.languageTag.toLowerCase() === realPrimary.toLowerCase()
);

let correctedLocales = [...rawLocales];
if (primaryIndex > -1) {
  const [primary] = correctedLocales.splice(primaryIndex, 1);
  correctedLocales.unshift(primary);
}

This workaround should work across different language configurations (English, Spanish, Hindi, etc.) and restore the expected behavior where locales are returned “in order” as specified.

Could you please try this workaround and let us know if it works for you?

Warm regards,
Aishwarya

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.