Skip to content

Commit

Permalink
Make I18nManagerModule TurboModule-compatible
Browse files Browse the repository at this point in the history
Summary:
This NativeModule will now be type-safe, and TurboModule-compatible.

Changelog: [Internal]

Reviewed By: fkgozali

Differential Revision: D26956332

fbshipit-source-id: 6651a003c70819934869dd6a8c664ef984d0efcb
  • Loading branch information
RSNara authored and facebook-github-bot committed Mar 10, 2021
1 parent b15f8a3 commit 23d9bf1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
19 changes: 13 additions & 6 deletions Libraries/ReactNative/I18nManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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|} => {
Expand Down
1 change: 1 addition & 0 deletions Libraries/ReactNative/NativeI18nManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface Spec extends TurboModule {
+getConstants: () => {|
isRTL: boolean,
doLeftAndRightSwapInRTL: boolean,
localeIdentifier: ?string,
|};
allowRTL: (allowRTL: boolean) => void;
forceRTL: (forceRTL: boolean) => void;
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@
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;
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);
}

Expand All @@ -34,8 +34,8 @@ public String getName() {
}

@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();
Expand All @@ -46,18 +46,18 @@ public Map<String, Object> getConstants() {
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);
}
}

0 comments on commit 23d9bf1

Please sign in to comment.