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
This commit is contained in:
Ramanpreet Nara 2021-03-10 14:52:40 -08:00 коммит произвёл Facebook GitHub Bot
Родитель b15f8a30e7
Коммит 23d9bf1a24
4 изменённых файлов: 28 добавлений и 19 удалений

Просмотреть файл

@ -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|} => {

Просмотреть файл

@ -15,6 +15,7 @@ export interface Spec extends TurboModule {
+getConstants: () => {|
isRTL: boolean,
doLeftAndRightSwapInRTL: boolean,
localeIdentifier: ?string,
|};
allowRTL: (allowRTL: boolean) => void;
forceRTL: (forceRTL: boolean) => void;

Просмотреть файл

@ -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"),
],
)

Просмотреть файл

@ -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<String, Object> getConstants() {
final Context context = getContext();
public Map<String, Object> getTypedExportedConstants() {
final Context context = getReactApplicationContext();
final Locale locale = context.getResources().getConfiguration().locale;
final Map<String, Object> 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);
}
}