Skip to content

Commit 7016149

Browse files
committed
Remove left / right swap in RTL
1 parent 76dc1ab commit 7016149

66 files changed

Lines changed: 310 additions & 1097 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/jest-preset/jest/mocks/NativeModules.js‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,9 @@ const NativeModules = {
219219
removeListeners: jest.fn() as JestMockFn<$FlowFixMe, $FlowFixMe>,
220220
},
221221
I18nManager: {
222-
allowRTL: jest.fn() as JestMockFn<$FlowFixMe, $FlowFixMe>,
223222
forceRTL: jest.fn() as JestMockFn<$FlowFixMe, $FlowFixMe>,
224-
swapLeftAndRightInRTL: jest.fn() as JestMockFn<$FlowFixMe, $FlowFixMe>,
225223
getConstants: (): $FlowFixMe => ({
226224
isRTL: false,
227-
doLeftAndRightSwapInRTL: true,
228225
}),
229226
},
230227
};

‎packages/react-native/Libraries/ReactNative/I18nManager.js‎

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,37 @@
88
* @format
99
*/
1010

11-
import type {I18nManagerConstants} from './NativeI18nManager';
12-
11+
import warnOnce from '../Utilities/warnOnce';
1312
import NativeI18nManager from './NativeI18nManager';
1413

14+
type I18nManagerConstants = {
15+
/**
16+
* @deprecated `left` and `right` style properties are no longer swapped in
17+
* RTL. Use `start` and `end` style properties, such as `marginStart`,
18+
* instead.
19+
*/
20+
doLeftAndRightSwapInRTL: boolean,
21+
isRTL: boolean,
22+
localeIdentifier?: ?string,
23+
};
24+
1525
const i18nConstants: I18nManagerConstants = getI18nManagerConstants();
1626

1727
function getI18nManagerConstants(): I18nManagerConstants {
1828
if (NativeI18nManager) {
1929
const {isRTL, doLeftAndRightSwapInRTL, localeIdentifier} =
2030
NativeI18nManager.getConstants();
21-
return {isRTL, doLeftAndRightSwapInRTL, localeIdentifier};
31+
return {
32+
isRTL,
33+
// Older native binaries still swap left and right styles in RTL
34+
doLeftAndRightSwapInRTL: doLeftAndRightSwapInRTL ?? false,
35+
localeIdentifier,
36+
};
2237
}
2338

2439
return {
2540
isRTL: false,
26-
doLeftAndRightSwapInRTL: true,
41+
doLeftAndRightSwapInRTL: false,
2742
};
2843
}
2944

@@ -38,15 +53,18 @@ export default {
3853
},
3954

4055
/**
41-
* Allows the app to opt in to RTL layout behavior. Should be called early
42-
* in app startup. Takes effect on the next app reload.
56+
* @deprecated Has no effect. Set `RCTSupportsRTL` to `true` in Info.plist on
57+
* iOS, or `android:supportsRtl="true"` in AndroidManifest.xml on Android,
58+
* instead.
4359
*/
44-
allowRTL: (shouldAllow: boolean) => {
45-
if (!NativeI18nManager) {
46-
return;
47-
}
48-
49-
NativeI18nManager.allowRTL(shouldAllow);
60+
allowRTL: (shouldAllow: boolean): void => {
61+
warnOnce(
62+
'I18nManager.allowRTL',
63+
'I18nManager.allowRTL() is deprecated and has no effect. Set ' +
64+
'`RCTSupportsRTL` to `true` in Info.plist on iOS, or ' +
65+
'`android:supportsRtl="true"` in AndroidManifest.xml on Android, ' +
66+
'instead.',
67+
);
5068
},
5169

5270
/**
@@ -62,21 +80,34 @@ export default {
6280
},
6381

6482
/**
65-
* Controls whether `left`/`right` style properties are automatically swapped
66-
* in RTL layouts. When enabled, `left` becomes `right` and vice versa.
83+
* @deprecated Has no effect. Use `start` and `end` style properties, such as
84+
* `marginStart`, instead.
6785
*/
68-
swapLeftAndRightInRTL: (flipStyles: boolean) => {
69-
if (!NativeI18nManager) {
70-
return;
71-
}
72-
73-
NativeI18nManager.swapLeftAndRightInRTL(flipStyles);
86+
swapLeftAndRightInRTL: (flipStyles: boolean): void => {
87+
warnOnce(
88+
'I18nManager.swapLeftAndRightInRTL',
89+
'I18nManager.swapLeftAndRightInRTL() is deprecated and has no effect. ' +
90+
'Use `start` and `end` style properties, such as `marginStart`, ' +
91+
'instead.',
92+
);
7493
},
7594

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

79-
/** Whether left and right style properties are swapped in RTL mode. */
80-
doLeftAndRightSwapInRTL:
81-
i18nConstants.doLeftAndRightSwapInRTL as I18nManagerConstants['doLeftAndRightSwapInRTL'],
98+
/**
99+
* @deprecated `left` and `right` style properties are no longer swapped in
100+
* RTL. Use `start` and `end` style properties, such as `marginStart`,
101+
* instead.
102+
*/
103+
// $FlowFixMe[unsafe-getters-setters]
104+
get doLeftAndRightSwapInRTL(): I18nManagerConstants['doLeftAndRightSwapInRTL'] {
105+
warnOnce(
106+
'I18nManager.doLeftAndRightSwapInRTL',
107+
'I18nManager.doLeftAndRightSwapInRTL is deprecated. `left` and `right` ' +
108+
'style properties are no longer swapped in RTL. Use `start` and ' +
109+
'`end` style properties, such as `marginStart`, instead.',
110+
);
111+
return i18nConstants.doLeftAndRightSwapInRTL;
112+
},
82113
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
'use strict';
12+
13+
import type {I18nManagerConstants} from '../NativeI18nManager';
14+
15+
const mockGetConstants: JestMockFn<[], I18nManagerConstants> = jest.fn();
16+
17+
jest.mock('../NativeI18nManager', () => ({
18+
__esModule: true,
19+
default: {
20+
getConstants: () => mockGetConstants(),
21+
forceRTL: jest.fn(),
22+
},
23+
}));
24+
25+
describe('I18nManager', () => {
26+
beforeEach(() => {
27+
jest.resetModules();
28+
});
29+
30+
it('does not swap left and right styles when native does not send the constant', () => {
31+
mockGetConstants.mockReturnValue({isRTL: true});
32+
const I18nManager = require('../I18nManager').default;
33+
34+
expect(I18nManager.getConstants().doLeftAndRightSwapInRTL).toBe(false);
35+
});
36+
37+
it('reads doLeftAndRightSwapInRTL from older native binaries', () => {
38+
mockGetConstants.mockReturnValue({
39+
isRTL: true,
40+
doLeftAndRightSwapInRTL: true,
41+
});
42+
const I18nManager = require('../I18nManager').default;
43+
44+
expect(I18nManager.getConstants().doLeftAndRightSwapInRTL).toBe(true);
45+
});
46+
});

‎packages/react-native/React/CoreModules/RCTI18nManager.mm‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,11 @@ + (BOOL)requiresMainQueueSetup
2626
return NO;
2727
}
2828

29-
- (void)allowRTL:(BOOL)value
30-
{
31-
[[RCTI18nUtil sharedInstance] allowRTL:value];
32-
}
33-
3429
- (void)forceRTL:(BOOL)value
3530
{
3631
[[RCTI18nUtil sharedInstance] forceRTL:value];
3732
}
3833
39-
- (void)swapLeftAndRightInRTL:(BOOL)value
40-
{
41-
[[RCTI18nUtil sharedInstance] swapLeftAndRightInRTL:value];
42-
}
43-
4434
- (NSDictionary *)constantsToExport
4535
{
4636
return [self getConstants];
@@ -50,7 +40,6 @@ - (NSDictionary *)getConstants
5040
{
5141
return @{
5242
@"isRTL" : @([[RCTI18nUtil sharedInstance] isRTL]),
53-
@"doLeftAndRightSwapInRTL" : @([[RCTI18nUtil sharedInstance] doLeftAndRightSwapInRTL])
5443
};
5544
}
5645

‎packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollContentViewComponentView.mm‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#import "RCTScrollContentViewComponentView.h"
99

10-
#import <React/RCTI18nUtil.h>
1110
#import <React/RCTUtils.h>
1211
#import <react/featureflags/ReactNativeFeatureFlags.h>
1312
#import <react/renderer/components/scrollview/ScrollContentViewComponentDescriptor.h>
@@ -53,11 +52,9 @@ - (void)_updateStateIfNecessary
5352
return;
5453
}
5554

56-
RCTI18nUtil *i18nUtil = [RCTI18nUtil sharedInstance];
5755
ScrollContentViewState newData;
5856
newData.safeAreaPadding = RCTRoundedEdgeInsets(_safeAreaPadding);
5957
newData.contentInsetAdjustment = RCTRoundedEdgeInsets(_contentInsetAdjustment);
60-
newData.swapLeftAndRightInRTL = i18nUtil.isRTL && i18nUtil.doLeftAndRightSwapInRTL;
6158
if (newData == _lastStateData) {
6259
return;
6360
}

‎packages/react-native/React/Fabric/Surface/RCTFabricSurface.mm‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,6 @@ - (void)_updateLayoutContext
194194
auto layoutContext = _surfaceHandler->getLayoutContext();
195195
196196
layoutContext.pointScaleFactor = RCTScreenScale();
197-
layoutContext.swapLeftAndRightInRTL =
198-
[[RCTI18nUtil sharedInstance] isRTL] && [[RCTI18nUtil sharedInstance] doLeftAndRightSwapInRTL];
199197
layoutContext.fontSizeMultiplier = RCTFontSizeMultiplier();
200198
201199
_surfaceHandler->constraintLayout(layoutConstraints, layoutContext);

‎packages/react-native/React/Modules/RCTI18nUtil.h‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@
1818
+ (instancetype)sharedInstance;
1919

2020
- (BOOL)isRTL;
21-
- (BOOL)isRTLAllowed;
22-
- (void)allowRTL:(BOOL)value;
2321
- (BOOL)isRTLForced;
2422
- (void)forceRTL:(BOOL)value;
25-
- (BOOL)doLeftAndRightSwapInRTL;
26-
- (void)swapLeftAndRightInRTL:(BOOL)value;
2723

2824
@end

‎packages/react-native/React/Modules/RCTI18nUtil.m‎

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,57 +9,50 @@
99

1010
#import <UIKit/UIKit.h>
1111

12-
@implementation RCTI18nUtil
12+
@implementation RCTI18nUtil {
13+
BOOL _isRTLSupported;
14+
}
1315

1416
+ (instancetype)sharedInstance
1517
{
1618
static RCTI18nUtil *sharedInstance;
1719
static dispatch_once_t onceToken;
1820
dispatch_once(&onceToken, ^{
1921
sharedInstance = [self new];
20-
[sharedInstance swapLeftAndRightInRTL:true];
2122
});
2223

2324
return sharedInstance;
2425
}
2526

27+
- (instancetype)init
28+
{
29+
if (self = [super init]) {
30+
_isRTLSupported = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"RCTSupportsRTL"] boolValue];
31+
}
32+
return self;
33+
}
34+
2635
/**
2736
* Check if the app is currently running on an RTL locale.
28-
* This only happens when the app:
37+
* Always false unless `RCTSupportsRTL` is true in Info.plist.
38+
* Otherwise, this only happens when the app:
2939
* - is forcing RTL layout, regardless of the active language (for development purpose)
30-
* - allows RTL layout when using RTL locale
40+
* - or is using an RTL locale
3141
*/
3242
- (BOOL)isRTL
3343
{
44+
if (!_isRTLSupported) {
45+
return NO;
46+
}
3447
if ([self isRTLForced]) {
3548
return YES;
3649
}
37-
if ([self isRTLAllowed] && [self isApplicationPreferredLanguageRTL]) {
50+
if ([self isApplicationPreferredLanguageRTL]) {
3851
return YES;
3952
}
4053
return NO;
4154
}
4255
43-
/**
44-
* Should be used very early during app start up
45-
* Before the bridge is initialized
46-
* @return whether the app allows RTL layout, default is true
47-
*/
48-
- (BOOL)isRTLAllowed
49-
{
50-
NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:@"RCTI18nUtil_allowRTL"];
51-
if (value == nil) {
52-
return YES;
53-
}
54-
return [value boolValue];
55-
}
56-
57-
- (void)allowRTL:(BOOL)rtlStatus
58-
{
59-
[[NSUserDefaults standardUserDefaults] setBool:rtlStatus forKey:@"RCTI18nUtil_allowRTL"];
60-
[[NSUserDefaults standardUserDefaults] synchronize];
61-
}
62-
6356
/**
6457
* Could be used to test RTL layout with English
6558
* Used for development and testing purpose
@@ -76,17 +69,6 @@ - (void)forceRTL:(BOOL)rtlStatus
7669
[[NSUserDefaults standardUserDefaults] synchronize];
7770
}
7871
79-
- (BOOL)doLeftAndRightSwapInRTL
80-
{
81-
return [[NSUserDefaults standardUserDefaults] boolForKey:@"RCTI18nUtil_makeRTLFlipLeftAndRightStyles"];
82-
}
83-
84-
- (void)swapLeftAndRightInRTL:(BOOL)value
85-
{
86-
[[NSUserDefaults standardUserDefaults] setBool:value forKey:@"RCTI18nUtil_makeRTLFlipLeftAndRightStyles"];
87-
[[NSUserDefaults standardUserDefaults] synchronize];
88-
}
89-
9072
// Check if the current device language is RTL
9173
- (BOOL)isDevicePreferredLanguageRTL
9274
{

0 commit comments

Comments
 (0)