Skip to content

Commit eaa9d7f

Browse files
Shreesh Ayachitmeta-codesync[bot]
authored andcommitted
Use ConfigurationCompat for device locale lookup (#58723)
Summary: Pull Request resolved: #58723 `I18nUtil` directly called `Configuration.getLocales()`, which is unavailable before API 24. Read the preferred locale through `ConfigurationCompat` instead and fall back to the process default when the locale list is empty. Add an API-23 Robolectric regression test that exercises RTL detection on an Arabic locale. Changelog: [Android][Fixed] - Prevent a crash when resolving RTL layout direction on Android 6 Reviewed By: cortinico Differential Revision: D122147089 fbshipit-source-id: 461c8606f9abc45aa995c6e13254a2ad3f925c66
1 parent ca0ae36 commit eaa9d7f

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

  • packages/react-native/ReactAndroid/src

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ package com.facebook.react.modules.i18nmanager
1010
import android.content.Context
1111
import android.content.pm.ApplicationInfo
1212
import android.view.View
13+
import androidx.core.os.ConfigurationCompat
1314
import androidx.core.text.TextUtilsCompat
15+
import java.util.Locale
1416

1517
public class I18nUtil private constructor() {
1618
/**
@@ -61,8 +63,9 @@ public class I18nUtil private constructor() {
6163

6264
// Check if the current device language is RTL
6365
private fun isDevicePreferredLanguageRTL(context: Context): Boolean {
64-
val directionality =
65-
TextUtilsCompat.getLayoutDirectionFromLocale(context.resources.configuration.locales[0])
66+
val locale =
67+
ConfigurationCompat.getLocales(context.resources.configuration)[0] ?: Locale.getDefault()
68+
val directionality = TextUtilsCompat.getLayoutDirectionFromLocale(locale)
6669
return directionality == View.LAYOUT_DIRECTION_RTL
6770
}
6871

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.modules.i18nmanager
9+
10+
import android.content.Context
11+
import android.content.pm.ApplicationInfo
12+
import org.assertj.core.api.Assertions.assertThat
13+
import org.junit.Before
14+
import org.junit.Test
15+
import org.junit.runner.RunWith
16+
import org.robolectric.RobolectricTestRunner
17+
import org.robolectric.RuntimeEnvironment
18+
import org.robolectric.annotation.Config
19+
20+
@RunWith(RobolectricTestRunner::class)
21+
internal class I18nUtilTest {
22+
private lateinit var context: Context
23+
24+
@Before
25+
fun setUp() {
26+
context = RuntimeEnvironment.getApplication()
27+
context.applicationInfo.flags =
28+
context.applicationInfo.flags or ApplicationInfo.FLAG_SUPPORTS_RTL
29+
I18nUtil.instance.allowRTL(context, true)
30+
I18nUtil.instance.forceRTL(context, false)
31+
}
32+
33+
@Test
34+
@Config(sdk = [23], qualifiers = "ar")
35+
fun isRTL_onApi23_usesPreferredLocale() {
36+
assertThat(I18nUtil.instance.isRTL(context)).isTrue()
37+
}
38+
}

0 commit comments

Comments
 (0)