Skip to content

Commit

Permalink
add spec for I18nManager (#24908)
Browse files Browse the repository at this point in the history
Summary:
Part of #24875.

## Changelog

[General] [Added] - add TM spec for I18nManager
Pull Request resolved: #24908

Reviewed By: fkgozali

Differential Revision: D15395163

Pulled By: RSNara

fbshipit-source-id: 8fd3a5a8ce5d0f74132efff4fae7224eab03405b
  • Loading branch information
ericlewis authored and facebook-github-bot committed Jun 1, 2019
1 parent 5e6cebe commit 18feded
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 19 deletions.
6 changes: 4 additions & 2 deletions Libraries/Inspector/resolveBoxStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ function resolveBoxStyle(

const styleForEnd = style[prefix + 'End'];
if (styleForEnd != null) {
if (I18nManager.isRTL && I18nManager.doLeftAndRightSwapInRTL) {
const constants = I18nManager.getConstants();
if (constants.isRTL && constants.doLeftAndRightSwapInRTL) {
result.left = styleForEnd;
} else {
result.right = styleForEnd;
Expand All @@ -78,7 +79,8 @@ function resolveBoxStyle(
}
const styleForStart = style[prefix + 'Start'];
if (styleForStart != null) {
if (I18nManager.isRTL && I18nManager.doLeftAndRightSwapInRTL) {
const constants = I18nManager.getConstants();
if (constants.isRTL && constants.doLeftAndRightSwapInRTL) {
result.right = styleForStart;
} else {
result.left = styleForStart;
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Modal/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ class Modal extends React.Component<Props> {
}
}

const side = I18nManager.isRTL ? 'right' : 'left';
const side = I18nManager.getConstants().isRTL ? 'right' : 'left';
const styles = StyleSheet.create({
modal: {
position: 'absolute',
Expand Down
37 changes: 21 additions & 16 deletions Libraries/ReactNative/I18nManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,26 @@
*/
'use strict';

type I18nManagerStatus = {
isRTL: boolean,
doLeftAndRightSwapInRTL: boolean,
allowRTL: (allowRTL: boolean) => {},
forceRTL: (forceRTL: boolean) => {},
swapLeftAndRightInRTL: (flipStyles: boolean) => {},
};
import NativeI18nManager from './NativeI18nManager';

const I18nManager: I18nManagerStatus = require('../BatchedBridge/NativeModules')
.I18nManager || {
isRTL: false,
doLeftAndRightSwapInRTL: true,
allowRTL: () => {},
forceRTL: () => {},
swapLeftAndRightInRTL: () => {},
};
module.exports = {
getConstants: () => {
return NativeI18nManager.getConstants();
},

allowRTL: (shouldAllow: boolean) => {
NativeI18nManager.allowRTL(shouldAllow);
},

module.exports = I18nManager;
forceRTL: (shouldForce: boolean) => {
NativeI18nManager.forceRTL(shouldForce);
},

swapLeftAndRightInRTL: (flipStyles: boolean) => {
NativeI18nManager.swapLeftAndRightInRTL(flipStyles);
},

isRTL: NativeI18nManager.getConstants().isRTL,
doLeftAndRightSwapInRTL: NativeI18nManager.getConstants()
.doLeftAndRightSwapInRTL,
};
26 changes: 26 additions & 0 deletions Libraries/ReactNative/NativeI18nManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/

'use strict';

import type {TurboModule} from '../TurboModule/RCTExport';
import * as TurboModuleRegistry from '../TurboModule/TurboModuleRegistry';

export interface Spec extends TurboModule {
+getConstants: () => {|
isRTL: boolean,
doLeftAndRightSwapInRTL: boolean,
|};
allowRTL: (allowRTL: boolean) => void;
forceRTL: (forceRTL: boolean) => void;
swapLeftAndRightInRTL: (flipStyles: boolean) => void;
}

export default TurboModuleRegistry.getEnforcing<Spec>('I18nManager');
9 changes: 9 additions & 0 deletions jest/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,15 @@ jest
addListener: jest.fn(),
removeListeners: jest.fn(),
},
I18nManager: {
allowRTL: jest.fn(),
forceRTL: jest.fn(),
swapLeftAndRightInRTL: jest.fn(),
getConstants: () => ({
isRTL: false,
doLeftAndRightSwapInRTL: true,
}),
},
}))
.mock('../Libraries/ReactNative/requireNativeComponent', () => {
const React = require('react');
Expand Down

0 comments on commit 18feded

Please sign in to comment.