From 23d9bf1a24f80003a8a3c0b82e9b5691e4e6544e Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Wed, 10 Mar 2021 14:52:40 -0800 Subject: [PATCH] Make I18nManagerModule TurboModule-compatible Summary: This NativeModule will now be type-safe, and TurboModule-compatible. Changelog: [Internal] Reviewed By: fkgozali Differential Revision: D26956332 fbshipit-source-id: 6651a003c70819934869dd6a8c664ef984d0efcb --- Libraries/ReactNative/I18nManager.js | 19 ++++++++++----- Libraries/ReactNative/NativeI18nManager.js | 1 + .../facebook/react/modules/i18nmanager/BUCK | 3 ++- .../i18nmanager/I18nManagerModule.java | 24 +++++++++---------- 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/Libraries/ReactNative/I18nManager.js b/Libraries/ReactNative/I18nManager.js index 7c3dcd6231..9fa21e78da 100644 --- a/Libraries/ReactNative/I18nManager.js +++ b/Libraries/ReactNative/I18nManager.js @@ -13,12 +13,19 @@ import NativeI18nManager from './NativeI18nManager'; const i18nConstants: {| doLeftAndRightSwapInRTL: boolean, isRTL: boolean, -|} = NativeI18nManager - ? NativeI18nManager.getConstants() - : { - isRTL: false, - doLeftAndRightSwapInRTL: true, - }; +|} = getI18nManagerConstants(); + +function getI18nManagerConstants() { + if (NativeI18nManager) { + const {isRTL, doLeftAndRightSwapInRTL} = NativeI18nManager.getConstants(); + return {isRTL, doLeftAndRightSwapInRTL}; + } + + return { + isRTL: false, + doLeftAndRightSwapInRTL: true, + }; +} module.exports = { getConstants: (): {|doLeftAndRightSwapInRTL: boolean, isRTL: boolean|} => { diff --git a/Libraries/ReactNative/NativeI18nManager.js b/Libraries/ReactNative/NativeI18nManager.js index 608eb679e7..b9cb0efebc 100644 --- a/Libraries/ReactNative/NativeI18nManager.js +++ b/Libraries/ReactNative/NativeI18nManager.js @@ -15,6 +15,7 @@ export interface Spec extends TurboModule { +getConstants: () => {| isRTL: boolean, doLeftAndRightSwapInRTL: boolean, + localeIdentifier: ?string, |}; allowRTL: (allowRTL: boolean) => void; forceRTL: (forceRTL: boolean) => void; 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 1b479ecd3e..2a7e403ccd 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/BUCK @@ -1,4 +1,4 @@ -load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_target", "rn_android_library") +load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_root_target", "react_native_target", "rn_android_library") rn_android_library( name = "i18nmanager", @@ -22,5 +22,6 @@ rn_android_library( react_native_target("java/com/facebook/react/bridge:bridge"), react_native_target("java/com/facebook/react/common:common"), react_native_target("java/com/facebook/react/module/annotations:annotations"), + react_native_root_target("Libraries:FBReactNativeSpec"), ], ) 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 index 9567c145b2..767900d3d0 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nManagerModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nManagerModule.java @@ -8,9 +8,9 @@ package com.facebook.react.modules.i18nmanager; import android.content.Context; -import com.facebook.react.bridge.ContextBaseJavaModule; +import com.facebook.fbreact.specs.NativeI18nManagerSpec; import com.facebook.react.bridge.NativeModule; -import com.facebook.react.bridge.ReactMethod; +import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.common.MapBuilder; import com.facebook.react.module.annotations.ReactModule; import java.util.Locale; @@ -18,13 +18,13 @@ import java.util.Map; /** {@link NativeModule} that allows JS to set allowRTL and get isRTL status. */ @ReactModule(name = I18nManagerModule.NAME) -public class I18nManagerModule extends ContextBaseJavaModule { +public class I18nManagerModule extends NativeI18nManagerSpec { public static final String NAME = "I18nManager"; private final I18nUtil sharedI18nUtilInstance = I18nUtil.getInstance(); - public I18nManagerModule(Context context) { + public I18nManagerModule(ReactApplicationContext context) { super(context); } @@ -34,8 +34,8 @@ public class I18nManagerModule extends ContextBaseJavaModule { } @Override - public Map getConstants() { - final Context context = getContext(); + public Map getTypedExportedConstants() { + final Context context = getReactApplicationContext(); final Locale locale = context.getResources().getConfiguration().locale; final Map constants = MapBuilder.newHashMap(); @@ -46,18 +46,18 @@ public class I18nManagerModule extends ContextBaseJavaModule { return constants; } - @ReactMethod + @Override public void allowRTL(boolean value) { - sharedI18nUtilInstance.allowRTL(getContext(), value); + sharedI18nUtilInstance.allowRTL(getReactApplicationContext(), value); } - @ReactMethod + @Override public void forceRTL(boolean value) { - sharedI18nUtilInstance.forceRTL(getContext(), value); + sharedI18nUtilInstance.forceRTL(getReactApplicationContext(), value); } - @ReactMethod + @Override public void swapLeftAndRightInRTL(boolean value) { - sharedI18nUtilInstance.swapLeftAndRightInRTL(getContext(), value); + sharedI18nUtilInstance.swapLeftAndRightInRTL(getReactApplicationContext(), value); } }