@@ -22,8 +22,10 @@ public class I18nUtil {
2222
2323 private static final String MY_PREFS_NAME =
2424 "com.facebook.react.modules.i18nmanager.I18nUtil" ;
25- private static final String KEY_FOR_PREFS =
25+ private static final String KEY_FOR_PREFS_ALLOWRTL =
2626 "RCTI18nUtil_allowRTL" ;
27+ private static final String KEY_FOR_PREFS_FORCERTL =
28+ "RCTI18nUtil_forceRTL" ;
2729
2830 private I18nUtil () {
2931 // Exists only to defeat instantiation.
@@ -36,23 +38,51 @@ public static I18nUtil getInstance() {
3638 return sharedI18nUtilInstance ;
3739 }
3840
39- // If the current device language is RTL and RTL is allowed for the app,
40- // the RN app will automatically have a RTL layout.
41+ /**
42+ * Check if the device is currently running on an RTL locale.
43+ * This only happens when the app:
44+ * - is forcing RTL layout, regardless of the active language (for development purpose)
45+ * - allows RTL layout when using RTL locale
46+ */
4147 public boolean isRTL (Context context ) {
42- return allowRTL (context ) &&
48+ if (isRTLForced (context )) {
49+ return true ;
50+ }
51+ return isRTLAllowed (context ) &&
4352 isDevicePreferredLanguageRTL ();
4453 }
4554
46- private boolean allowRTL (Context context ) {
55+ /**
56+ * Should be used very early during app start up
57+ * Before the bridge is initialized
58+ */
59+ private boolean isRTLAllowed (Context context ) {
60+ SharedPreferences prefs =
61+ context .getSharedPreferences (MY_PREFS_NAME , Context .MODE_PRIVATE );
62+ return prefs .getBoolean (KEY_FOR_PREFS_ALLOWRTL , false );
63+ }
64+
65+ public void allowRTL (Context context , boolean allowRTL ) {
66+ SharedPreferences .Editor editor =
67+ context .getSharedPreferences (MY_PREFS_NAME , Context .MODE_PRIVATE ).edit ();
68+ editor .putBoolean (KEY_FOR_PREFS_ALLOWRTL , allowRTL );
69+ editor .apply ();
70+ }
71+
72+ /**
73+ * Could be used to test RTL layout with English
74+ * Used for development and testing purpose
75+ */
76+ private boolean isRTLForced (Context context ) {
4777 SharedPreferences prefs =
4878 context .getSharedPreferences (MY_PREFS_NAME , Context .MODE_PRIVATE );
49- return prefs .getBoolean (KEY_FOR_PREFS , false );
79+ return prefs .getBoolean (KEY_FOR_PREFS_FORCERTL , false );
5080 }
5181
52- public void setAllowRTL (Context context , boolean allowRTL ) {
82+ public void forceRTL (Context context , boolean allowRTL ) {
5383 SharedPreferences .Editor editor =
5484 context .getSharedPreferences (MY_PREFS_NAME , Context .MODE_PRIVATE ).edit ();
55- editor .putBoolean (KEY_FOR_PREFS , allowRTL );
85+ editor .putBoolean (KEY_FOR_PREFS_FORCERTL , allowRTL );
5686 editor .apply ();
5787 }
5888
0 commit comments