Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions packages/jest-preset/jest/mocks/NativeModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,9 @@ const NativeModules = {
removeListeners: jest.fn() as JestMockFn<$FlowFixMe, $FlowFixMe>,
},
I18nManager: {
allowRTL: jest.fn() as JestMockFn<$FlowFixMe, $FlowFixMe>,
forceRTL: jest.fn() as JestMockFn<$FlowFixMe, $FlowFixMe>,
swapLeftAndRightInRTL: jest.fn() as JestMockFn<$FlowFixMe, $FlowFixMe>,
getConstants: (): $FlowFixMe => ({
isRTL: false,
doLeftAndRightSwapInRTL: true,
}),
},
};
Expand Down
7 changes: 1 addition & 6 deletions packages/react-native/Libraries/Modal/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import * as React from 'react';
const ScrollView = require('../Components/ScrollView/ScrollView').default;
const View = require('../Components/View/View').default;
const AppContainer = require('../ReactNative/AppContainer').default;
const I18nManager = require('../ReactNative/I18nManager').default;
const {RootTagContext} = require('../ReactNative/RootTag');
const StyleSheet = require('../StyleSheet/StyleSheet').default;

Expand Down Expand Up @@ -367,17 +366,13 @@ class Modal extends React.Component<ModalProps, ModalState> {
}
}

const side = I18nManager.getConstants().isRTL ? 'right' : 'left';
const styles = StyleSheet.create({
modal: {
position: 'absolute',
},
/* $FlowFixMe[incompatible-type] Natural Inference rollout. See
* https://fburl.com/workplace/6291gfvu */
container: {
// $FlowFixMe[incompatible-type]
[side]: 0,
top: 0,
start: 0,
flex: 1,
backgroundColor: 'white',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import {createRef} from 'react';
import {Modal} from 'react-native';

const DEFAULT_MODAL_CHILD_VIEW = (
<rn-view backgroundColor="rgba(255, 255, 255, 1)" flex="1" left="0" top="0" />
<rn-view
backgroundColor="rgba(255, 255, 255, 1)"
flex="1"
start="0"
top="0"
/>
);

describe('<Modal>', () => {
Expand Down
77 changes: 54 additions & 23 deletions packages/react-native/Libraries/ReactNative/I18nManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,37 @@
* @format
*/

import type {I18nManagerConstants} from './NativeI18nManager';

import warnOnce from '../Utilities/warnOnce';
import NativeI18nManager from './NativeI18nManager';

type I18nManagerConstants = {
/**
* @deprecated `left` and `right` style properties are no longer swapped in
* RTL. Use `start` and `end` style properties, such as `marginStart`,
* instead.
*/
doLeftAndRightSwapInRTL: boolean,
isRTL: boolean,
localeIdentifier?: ?string,
};

const i18nConstants: I18nManagerConstants = getI18nManagerConstants();

function getI18nManagerConstants(): I18nManagerConstants {
if (NativeI18nManager) {
const {isRTL, doLeftAndRightSwapInRTL, localeIdentifier} =
NativeI18nManager.getConstants();
return {isRTL, doLeftAndRightSwapInRTL, localeIdentifier};
return {
isRTL,
// Older native binaries still swap left and right styles in RTL
doLeftAndRightSwapInRTL: doLeftAndRightSwapInRTL ?? false,
localeIdentifier,
};
}

return {
isRTL: false,
doLeftAndRightSwapInRTL: true,
doLeftAndRightSwapInRTL: false,
};
}

Expand All @@ -38,15 +53,18 @@ export default {
},

/**
* Allows the app to opt in to RTL layout behavior. Should be called early
* in app startup. Takes effect on the next app reload.
* @deprecated Has no effect. Set `RCTSupportsRTL` to `true` in Info.plist on
* iOS, or `android:supportsRtl="true"` in AndroidManifest.xml on Android,
* instead.
*/
allowRTL: (shouldAllow: boolean) => {
if (!NativeI18nManager) {
return;
}

NativeI18nManager.allowRTL(shouldAllow);
allowRTL: (shouldAllow: boolean): void => {
warnOnce(
'I18nManager.allowRTL',
'I18nManager.allowRTL() is deprecated and has no effect. Set ' +
'`RCTSupportsRTL` to `true` in Info.plist on iOS, or ' +
'`android:supportsRtl="true"` in AndroidManifest.xml on Android, ' +
'instead.',
);
},

/**
Expand All @@ -62,21 +80,34 @@ export default {
},

/**
* Controls whether `left`/`right` style properties are automatically swapped
* in RTL layouts. When enabled, `left` becomes `right` and vice versa.
* @deprecated Has no effect. Use `start` and `end` style properties, such as
* `marginStart`, instead.
*/
swapLeftAndRightInRTL: (flipStyles: boolean) => {
if (!NativeI18nManager) {
return;
}

NativeI18nManager.swapLeftAndRightInRTL(flipStyles);
swapLeftAndRightInRTL: (flipStyles: boolean): void => {
warnOnce(
'I18nManager.swapLeftAndRightInRTL',
'I18nManager.swapLeftAndRightInRTL() is deprecated and has no effect. ' +
'Use `start` and `end` style properties, such as `marginStart`, ' +
'instead.',
);
},

/** Whether the current layout direction is Right-to-Left. */
isRTL: i18nConstants.isRTL as I18nManagerConstants['isRTL'],

/** Whether left and right style properties are swapped in RTL mode. */
doLeftAndRightSwapInRTL:
i18nConstants.doLeftAndRightSwapInRTL as I18nManagerConstants['doLeftAndRightSwapInRTL'],
/**
* @deprecated `left` and `right` style properties are no longer swapped in
* RTL. Use `start` and `end` style properties, such as `marginStart`,
* instead.
*/
// $FlowFixMe[unsafe-getters-setters]
get doLeftAndRightSwapInRTL(): I18nManagerConstants['doLeftAndRightSwapInRTL'] {
warnOnce(
'I18nManager.doLeftAndRightSwapInRTL',
'I18nManager.doLeftAndRightSwapInRTL is deprecated. `left` and `right` ' +
'style properties are no longer swapped in RTL. Use `start` and ' +
'`end` style properties, such as `marginStart`, instead.',
);
return i18nConstants.doLeftAndRightSwapInRTL;
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/

'use strict';

import type {I18nManagerConstants} from '../NativeI18nManager';

const mockGetConstants: JestMockFn<[], I18nManagerConstants> = jest.fn();

jest.mock('../NativeI18nManager', () => ({
__esModule: true,
default: {
getConstants: () => mockGetConstants(),
forceRTL: jest.fn(),
},
}));

describe('I18nManager', () => {
beforeEach(() => {
jest.resetModules();
});

it('does not swap left and right styles when native does not send the constant', () => {
mockGetConstants.mockReturnValue({isRTL: true});
const I18nManager = require('../I18nManager').default;

expect(I18nManager.getConstants().doLeftAndRightSwapInRTL).toBe(false);
});

it('reads doLeftAndRightSwapInRTL from older native binaries', () => {
mockGetConstants.mockReturnValue({
isRTL: true,
doLeftAndRightSwapInRTL: true,
});
const I18nManager = require('../I18nManager').default;

expect(I18nManager.getConstants().doLeftAndRightSwapInRTL).toBe(true);
});
});
11 changes: 0 additions & 11 deletions packages/react-native/React/CoreModules/RCTI18nManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,11 @@ + (BOOL)requiresMainQueueSetup
return NO;
}

- (void)allowRTL:(BOOL)value
{
[[RCTI18nUtil sharedInstance] allowRTL:value];
}

- (void)forceRTL:(BOOL)value
{
[[RCTI18nUtil sharedInstance] forceRTL:value];
}

- (void)swapLeftAndRightInRTL:(BOOL)value
{
[[RCTI18nUtil sharedInstance] swapLeftAndRightInRTL:value];
}

- (NSDictionary *)constantsToExport
{
return [self getConstants];
Expand All @@ -50,7 +40,6 @@ - (NSDictionary *)getConstants
{
return @{
@"isRTL" : @([[RCTI18nUtil sharedInstance] isRTL]),
@"doLeftAndRightSwapInRTL" : @([[RCTI18nUtil sharedInstance] doLeftAndRightSwapInRTL])
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#import "RCTScrollContentViewComponentView.h"

#import <React/RCTI18nUtil.h>
#import <React/RCTUtils.h>
#import <react/featureflags/ReactNativeFeatureFlags.h>
#import <react/renderer/components/scrollview/ScrollContentViewComponentDescriptor.h>
Expand Down Expand Up @@ -53,11 +52,9 @@ - (void)_updateStateIfNecessary
return;
}

RCTI18nUtil *i18nUtil = [RCTI18nUtil sharedInstance];
ScrollContentViewState newData;
newData.safeAreaPadding = RCTRoundedEdgeInsets(_safeAreaPadding);
newData.contentInsetAdjustment = RCTRoundedEdgeInsets(_contentInsetAdjustment);
newData.swapLeftAndRightInRTL = i18nUtil.isRTL && i18nUtil.doLeftAndRightSwapInRTL;
if (newData == _lastStateData) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,6 @@ - (void)_updateLayoutContext
auto layoutContext = _surfaceHandler->getLayoutContext();

layoutContext.pointScaleFactor = RCTScreenScale();
layoutContext.swapLeftAndRightInRTL =
[[RCTI18nUtil sharedInstance] isRTL] && [[RCTI18nUtil sharedInstance] doLeftAndRightSwapInRTL];
layoutContext.fontSizeMultiplier = RCTFontSizeMultiplier();

_surfaceHandler->constraintLayout(layoutConstraints, layoutContext);
Expand Down
4 changes: 0 additions & 4 deletions packages/react-native/React/Modules/RCTI18nUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
+ (instancetype)sharedInstance;

- (BOOL)isRTL;
- (BOOL)isRTLAllowed;
- (void)allowRTL:(BOOL)value;
- (BOOL)isRTLForced;
- (void)forceRTL:(BOOL)value;
- (BOOL)doLeftAndRightSwapInRTL;
- (void)swapLeftAndRightInRTL:(BOOL)value;

@end
54 changes: 18 additions & 36 deletions packages/react-native/React/Modules/RCTI18nUtil.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,50 @@

#import <UIKit/UIKit.h>

@implementation RCTI18nUtil
@implementation RCTI18nUtil {
BOOL _isRTLSupported;
}

+ (instancetype)sharedInstance
{
static RCTI18nUtil *sharedInstance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [self new];
[sharedInstance swapLeftAndRightInRTL:true];
});

return sharedInstance;
}

- (instancetype)init
{
if (self = [super init]) {
_isRTLSupported = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"RCTSupportsRTL"] boolValue];
}
return self;
}

/**
* Check if the app is currently running on an RTL locale.
* This only happens when the app:
* Always false unless `RCTSupportsRTL` is true in Info.plist.
* Otherwise, this only happens when the app:
* - is forcing RTL layout, regardless of the active language (for development purpose)
* - allows RTL layout when using RTL locale
* - or is using an RTL locale
*/
- (BOOL)isRTL
{
if (!_isRTLSupported) {
return NO;
}
if ([self isRTLForced]) {
return YES;
}
if ([self isRTLAllowed] && [self isApplicationPreferredLanguageRTL]) {
if ([self isApplicationPreferredLanguageRTL]) {
return YES;
}
return NO;
}

/**
* Should be used very early during app start up
* Before the bridge is initialized
* @return whether the app allows RTL layout, default is true
*/
- (BOOL)isRTLAllowed
{
NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:@"RCTI18nUtil_allowRTL"];
if (value == nil) {
return YES;
}
return [value boolValue];
}

- (void)allowRTL:(BOOL)rtlStatus
{
[[NSUserDefaults standardUserDefaults] setBool:rtlStatus forKey:@"RCTI18nUtil_allowRTL"];
[[NSUserDefaults standardUserDefaults] synchronize];
}

/**
* Could be used to test RTL layout with English
* Used for development and testing purpose
Expand All @@ -76,17 +69,6 @@ - (void)forceRTL:(BOOL)rtlStatus
[[NSUserDefaults standardUserDefaults] synchronize];
}

- (BOOL)doLeftAndRightSwapInRTL
{
return [[NSUserDefaults standardUserDefaults] boolForKey:@"RCTI18nUtil_makeRTLFlipLeftAndRightStyles"];
}

- (void)swapLeftAndRightInRTL:(BOOL)value
{
[[NSUserDefaults standardUserDefaults] setBool:value forKey:@"RCTI18nUtil_makeRTLFlipLeftAndRightStyles"];
[[NSUserDefaults standardUserDefaults] synchronize];
}

// Check if the current device language is RTL
- (BOOL)isDevicePreferredLanguageRTL
{
Expand Down
Loading
Loading