diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/BUCK b/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/BUCK new file mode 100644 index 000000000..ea33bd1ee --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/BUCK @@ -0,0 +1,19 @@ +include_defs('//ReactAndroid/DEFS') + +android_library( + name = 'i18nmanager', + srcs = glob(['*.java']), + deps = [ + 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'), + react_native_target('java/com/facebook/react/common:common'), + ], + visibility = [ + 'PUBLIC', + ], +) + +project_config( + src_target = ':i18nmanager', +) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nManagerModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nManagerModule.java new file mode 100644 index 000000000..49f30ddbd --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nManagerModule.java @@ -0,0 +1,54 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +package com.facebook.react.modules.i18nmanager; + +import com.facebook.react.bridge.NativeModule; +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.bridge.ReactContext; +import com.facebook.react.bridge.ReactContextBaseJavaModule; +import com.facebook.react.bridge.ReactMethod; +import com.facebook.react.common.MapBuilder; + +import java.util.Map; + + +/** + * {@link NativeModule} that allows JS to set allowRTL and get isRTL status. + */ +public class I18nManagerModule extends ReactContextBaseJavaModule { + + private final I18nUtil sharedI18nUtilInstance = I18nUtil.getInstance(); + + public I18nManagerModule(ReactApplicationContext reactContext) { + super(reactContext); + } + + @Override + public String getName() { + return "I18nManager"; + } + + @Override + public Map getConstants() { + final Map constants = MapBuilder.newHashMap(); + constants.put("isRTL", sharedI18nUtilInstance.isRTL( + getReactApplicationContext() + )); + return constants; + } + + @ReactMethod + public void allowRTL(boolean value) { + sharedI18nUtilInstance.setAllowRTL( + getReactApplicationContext(), + value + ); + } +} 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 new file mode 100644 index 000000000..e0199b265 --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nUtil.java @@ -0,0 +1,53 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +package com.facebook.react.modules.i18nmanager; + +import android.content.Context; +import android.content.SharedPreferences; +import android.content.SharedPreferences.Editor; + +public class I18nUtil { + private static I18nUtil sharedI18nUtilInstance = null; + + private static final String MY_PREFS_NAME = + "com.facebook.react.modules.i18nmanager.I18nUtil"; + private static final String KEY_FOR_PREFS = + "RCTI18nUtil_allowRTL"; + + private I18nUtil() { + // Exists only to defeat instantiation. + } + + public static I18nUtil getInstance() { + if(sharedI18nUtilInstance == null) { + sharedI18nUtilInstance = new I18nUtil(); + } + return sharedI18nUtilInstance; + } + + // If set allowRTL on the JS side, + // the RN app will automatically have a RTL layout. + public boolean isRTL(Context context) { + return allowRTL(context); + } + + private boolean allowRTL(Context context) { + SharedPreferences prefs = + context.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE); + return prefs.getBoolean(KEY_FOR_PREFS, false); + } + + public void setAllowRTL(Context context, boolean allowRTL) { + SharedPreferences.Editor editor = + context.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE).edit(); + editor.putBoolean(KEY_FOR_PREFS, allowRTL); + editor.apply(); + } +}