Skip to content

Commit 9de0b79

Browse files
MengjueWFacebook Github Bot 6
authored andcommitted
Provide forceRTL for LTR language to test
Summary: Provide forceRTL function for developer to test RTL layout in LTR language bundle in I18nUtil and expose it in I18nManager. Rename allowRTL and setAllowRTL functions Differential Revision: D3673601 fbshipit-source-id: 98f9c99e8a6948336fb918c24566dc9e5c0a3a3e
1 parent f571f01 commit 9de0b79

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nManagerModule.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,15 @@ public Map<String, Object> getConstants() {
4646

4747
@ReactMethod
4848
public void allowRTL(boolean value) {
49-
sharedI18nUtilInstance.setAllowRTL(
49+
sharedI18nUtilInstance.allowRTL(
50+
getReactApplicationContext(),
51+
value
52+
);
53+
}
54+
55+
@ReactMethod
56+
public void forceRTL(boolean value) {
57+
sharedI18nUtilInstance.forceRTL(
5058
getReactApplicationContext(),
5159
value
5260
);

ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nUtil.java

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)