Skip to content

Commit 8c0a045

Browse files
MengjueWFacebook Github Bot 4
authored andcommitted
Connect the OS setting with isRTL
Summary: Provide two function in I18nUtil to get the device and app current using language. And Set isRTL with both allowRTL and current app using language. Reviewed By: fkgozali Differential Revision: D3603412 fbshipit-source-id: 6bb280d56af9a12901148df9fab368366073680f
1 parent 54a4450 commit 8c0a045

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ android_library(
44
name = 'i18nmanager',
55
srcs = glob(['*.java']),
66
deps = [
7+
react_native_dep('third-party/android/support/v4:lib-support-v4'),
78
react_native_dep('third-party/java/infer-annotations:infer-annotations'),
89
react_native_dep('third-party/java/jsr-305:jsr-305'),
910
react_native_target('java/com/facebook/react/bridge:bridge'),

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
import android.content.Context;
1313
import android.content.SharedPreferences;
1414
import android.content.SharedPreferences.Editor;
15+
import android.support.v4.text.TextUtilsCompat;
16+
import android.support.v4.view.ViewCompat;
17+
18+
import java.util.Locale;
1519

1620
public class I18nUtil {
1721
private static I18nUtil sharedI18nUtilInstance = null;
@@ -32,10 +36,11 @@ public static I18nUtil getInstance() {
3236
return sharedI18nUtilInstance;
3337
}
3438

35-
// If set allowRTL on the JS side,
39+
// If the current device language is RTL and RTL is allowed for the app,
3640
// the RN app will automatically have a RTL layout.
3741
public boolean isRTL(Context context) {
38-
return allowRTL(context);
42+
return allowRTL(context) &&
43+
isDevicePreferredLanguageRTL();
3944
}
4045

4146
private boolean allowRTL(Context context) {
@@ -50,4 +55,11 @@ public void setAllowRTL(Context context, boolean allowRTL) {
5055
editor.putBoolean(KEY_FOR_PREFS, allowRTL);
5156
editor.apply();
5257
}
58+
59+
// Check if the current device language is RTL
60+
private boolean isDevicePreferredLanguageRTL() {
61+
final int directionality =
62+
TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault());
63+
return directionality == ViewCompat.LAYOUT_DIRECTION_RTL;
64+
}
5365
}

0 commit comments

Comments
 (0)