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
This commit is contained in:
Mengjue Wang 2016-07-26 14:40:15 -07:00 committed by Facebook Github Bot 4
parent 54a4450309
commit 8c0a045c0d
2 changed files with 15 additions and 2 deletions

View File

@ -4,6 +4,7 @@ android_library(
name = 'i18nmanager', name = 'i18nmanager',
srcs = glob(['*.java']), srcs = glob(['*.java']),
deps = [ 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/infer-annotations:infer-annotations'),
react_native_dep('third-party/java/jsr-305:jsr-305'), react_native_dep('third-party/java/jsr-305:jsr-305'),
react_native_target('java/com/facebook/react/bridge:bridge'), react_native_target('java/com/facebook/react/bridge:bridge'),

View File

@ -12,6 +12,10 @@ package com.facebook.react.modules.i18nmanager;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor; import android.content.SharedPreferences.Editor;
import android.support.v4.text.TextUtilsCompat;
import android.support.v4.view.ViewCompat;
import java.util.Locale;
public class I18nUtil { public class I18nUtil {
private static I18nUtil sharedI18nUtilInstance = null; private static I18nUtil sharedI18nUtilInstance = null;
@ -32,10 +36,11 @@ public class I18nUtil {
return sharedI18nUtilInstance; 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. // the RN app will automatically have a RTL layout.
public boolean isRTL(Context context) { public boolean isRTL(Context context) {
return allowRTL(context); return allowRTL(context) &&
isDevicePreferredLanguageRTL();
} }
private boolean allowRTL(Context context) { private boolean allowRTL(Context context) {
@ -50,4 +55,11 @@ public class I18nUtil {
editor.putBoolean(KEY_FOR_PREFS, allowRTL); editor.putBoolean(KEY_FOR_PREFS, allowRTL);
editor.apply(); 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;
}
} }