From 8c0a045c0d514ddc1696c7751c6665e81da4aa4b Mon Sep 17 00:00:00 2001 From: Mengjue Wang Date: Tue, 26 Jul 2016 14:40:15 -0700 Subject: [PATCH] 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 --- .../com/facebook/react/modules/i18nmanager/BUCK | 1 + .../react/modules/i18nmanager/I18nUtil.java | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/BUCK b/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/BUCK index ea33bd1ee..deece94dc 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/BUCK @@ -4,6 +4,7 @@ android_library( name = 'i18nmanager', srcs = glob(['*.java']), deps = [ + react_native_dep('third-party/android/support/v4:lib-support-v4'), react_native_dep('third-party/java/infer-annotations:infer-annotations'), react_native_dep('third-party/java/jsr-305:jsr-305'), react_native_target('java/com/facebook/react/bridge:bridge'), diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nUtil.java b/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nUtil.java index e0199b265..dd42db2fb 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nUtil.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nUtil.java @@ -12,6 +12,10 @@ package com.facebook.react.modules.i18nmanager; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; +import android.support.v4.text.TextUtilsCompat; +import android.support.v4.view.ViewCompat; + +import java.util.Locale; public class I18nUtil { private static I18nUtil sharedI18nUtilInstance = null; @@ -32,10 +36,11 @@ public class I18nUtil { return sharedI18nUtilInstance; } - // If set allowRTL on the JS side, + // If the current device language is RTL and RTL is allowed for the app, // the RN app will automatically have a RTL layout. public boolean isRTL(Context context) { - return allowRTL(context); + return allowRTL(context) && + isDevicePreferredLanguageRTL(); } private boolean allowRTL(Context context) { @@ -50,4 +55,11 @@ public class I18nUtil { editor.putBoolean(KEY_FOR_PREFS, allowRTL); editor.apply(); } + + // Check if the current device language is RTL + private boolean isDevicePreferredLanguageRTL() { + final int directionality = + TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()); + return directionality == ViewCompat.LAYOUT_DIRECTION_RTL; + } }