Skip to content

Commit e46189e

Browse files
zoontekmeta-codesync[bot]
authored andcommitted
fix(android): use the active locale to detect RTL direction (#57635)
Summary: On Android, RTL detection read the wrong locale. `isDevicePreferredLanguageRTL` use `Locale.getAvailableLocales()[0]`, which returns an arbitrary entry from the unordered list of **all runtime locales** (so, all locales that the current Android version supports), not the user's current system / app language. So layout direction was resolved from whatever locale happened to be first in that list, unrelated to the device (or per-app) language. This fixes it to read the active locale from `context.resources.configuration.locales[0]`, the resolved preferred locale for the app. See the screenshots below: `Locale.getAvailableLocales()[0]` returns all locales supported by Android, so the first one is **always** `af`, and `isRTL` is always `false`: <img width="1862" height="383" alt="Locale getAvailableLocales" src="https://github.com/user-attachments/assets/7340959f-67bd-4537-a074-849a76f345f8" /> --- vs `context.resources.configuration.locales[0]` for the same device language (I chose `ar-JO` in app settings), `isRTL` is `true`: <img width="1258" height="396" alt="app locales" src="https://github.com/user-attachments/assets/55159b65-a90c-40b7-9941-e5339c73d44c" /> ## Changelog: [ANDROID] [FIXED] - Detect RTL from the active locale instead of an arbitrary installed one Pull Request resolved: #57635 Test Plan: Switch the device (or per-app) language to an RTL language (e.g. Arabic) and confirm the app lays out RTL. **`Locale.getAvailableLocales()[0]` (before, arbitrary):** <img width="238" height="467" alt="android - before" src="https://github.com/user-attachments/assets/3f69e933-22b1-4d31-824a-b5e96057a76d" /> --- **`context.resources.configuration.locales[0]` (after, active locale):** <img width="238" height="467" alt="android - after" src="https://github.com/user-attachments/assets/9c86ff1a-b0ab-489f-a84e-76e3b7507da8" /> Reviewed By: christophpurrer Differential Revision: D113244807 Pulled By: Abbondanzo fbshipit-source-id: d1c884dcdbf5d1d0882fb12bc63644fc41559635
1 parent e0d8e63 commit e46189e

1 file changed

Lines changed: 7 additions & 9 deletions

File tree

  • packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nUtil.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import android.content.Context
1111
import android.content.pm.ApplicationInfo
1212
import android.view.View
1313
import androidx.core.text.TextUtilsCompat
14-
import java.util.Locale
1514

1615
public class I18nUtil private constructor() {
1716
/**
@@ -21,7 +20,7 @@ public class I18nUtil private constructor() {
2120
*/
2221
public fun isRTL(context: Context): Boolean =
2322
applicationHasRtlSupport(context) &&
24-
(isRTLForced(context) || (isRTLAllowed(context) && isDevicePreferredLanguageRTL))
23+
(isRTLForced(context) || (isRTLAllowed(context) && isDevicePreferredLanguageRTL(context)))
2524

2625
/**
2726
* Android relies on the presence of `android:supportsRtl="true"` being set in order to resolve
@@ -60,13 +59,12 @@ public class I18nUtil private constructor() {
6059
setPref(context, KEY_FOR_PREFS_FORCERTL, forceRTL)
6160
}
6261

63-
private val isDevicePreferredLanguageRTL: Boolean
64-
// Check if the current device language is RTL
65-
get() {
66-
val directionality =
67-
TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getAvailableLocales()[0])
68-
return directionality == View.LAYOUT_DIRECTION_RTL
69-
}
62+
// Check if the current device language is RTL
63+
private fun isDevicePreferredLanguageRTL(context: Context): Boolean {
64+
val directionality =
65+
TextUtilsCompat.getLayoutDirectionFromLocale(context.resources.configuration.locales[0])
66+
return directionality == View.LAYOUT_DIRECTION_RTL
67+
}
7068

7169
private fun isPrefSet(context: Context, key: String, defaultValue: Boolean): Boolean =
7270
context

0 commit comments

Comments
 (0)