Skip to content

Commit 2e52f77

Browse files
committed
Apply I18nManager.forceRTL() at runtime
1 parent 7016149 commit 2e52f77

23 files changed

Lines changed: 159 additions & 63 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ const NativeModules = {
219219
removeListeners: jest.fn() as JestMockFn<$FlowFixMe, $FlowFixMe>,
220220
},
221221
I18nManager: {
222-
forceRTL: jest.fn() as JestMockFn<$FlowFixMe, $FlowFixMe>,
222+
forceRTL: jest.fn(() => false) as JestMockFn<$FlowFixMe, $FlowFixMe>,
223223
getConstants: (): $FlowFixMe => ({
224224
isRTL: false,
225225
}),

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type I18nManagerConstants = {
2222
localeIdentifier?: ?string,
2323
};
2424

25-
const i18nConstants: I18nManagerConstants = getI18nManagerConstants();
25+
let i18nConstants: I18nManagerConstants = getI18nManagerConstants();
2626

2727
function getI18nManagerConstants(): I18nManagerConstants {
2828
if (NativeI18nManager) {
@@ -69,14 +69,18 @@ export default {
6969

7070
/**
7171
* Forces the app into RTL layout mode, regardless of the device locale.
72-
* Takes effect on the next app reload.
7372
*/
74-
forceRTL: (shouldForce: boolean) => {
73+
forceRTL: (shouldForce: boolean): void => {
7574
if (!NativeI18nManager) {
7675
return;
7776
}
7877

79-
NativeI18nManager.forceRTL(shouldForce);
78+
const isRTL = NativeI18nManager.forceRTL(shouldForce);
79+
80+
// Older native binaries return nothing, and apply the change on the next reload
81+
if (typeof isRTL === 'boolean') {
82+
i18nConstants = {...i18nConstants, isRTL};
83+
}
8084
},
8185

8286
/**
@@ -93,7 +97,10 @@ export default {
9397
},
9498

9599
/** Whether the current layout direction is Right-to-Left. */
96-
isRTL: i18nConstants.isRTL as I18nManagerConstants['isRTL'],
100+
// $FlowFixMe[unsafe-getters-setters]
101+
get isRTL(): I18nManagerConstants['isRTL'] {
102+
return i18nConstants.isRTL;
103+
},
97104

98105
/**
99106
* @deprecated `left` and `right` style properties are no longer swapped in

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,21 @@
1313
import type {I18nManagerConstants} from '../NativeI18nManager';
1414

1515
const mockGetConstants: JestMockFn<[], I18nManagerConstants> = jest.fn();
16+
const mockForceRTL: JestMockFn<[boolean], ?boolean> = jest.fn();
1617

1718
jest.mock('../NativeI18nManager', () => ({
1819
__esModule: true,
1920
default: {
2021
getConstants: () => mockGetConstants(),
21-
forceRTL: jest.fn(),
22+
forceRTL: (forceRTL: boolean) => mockForceRTL(forceRTL),
2223
},
2324
}));
2425

2526
describe('I18nManager', () => {
2627
beforeEach(() => {
2728
jest.resetModules();
29+
mockGetConstants.mockReset();
30+
mockForceRTL.mockReset();
2831
});
2932

3033
it('does not swap left and right styles when native does not send the constant', () => {
@@ -43,4 +46,31 @@ describe('I18nManager', () => {
4346

4447
expect(I18nManager.getConstants().doLeftAndRightSwapInRTL).toBe(true);
4548
});
49+
50+
it('updates isRTL when forceRTL returns a boolean', () => {
51+
mockGetConstants.mockReturnValue({isRTL: false});
52+
mockForceRTL.mockImplementation(forceRTL => forceRTL);
53+
const I18nManager = require('../I18nManager').default;
54+
55+
I18nManager.forceRTL(true);
56+
57+
expect(mockForceRTL).toHaveBeenCalledWith(true);
58+
expect(mockGetConstants).toHaveBeenCalledTimes(1);
59+
expect(I18nManager.isRTL).toBe(true);
60+
expect(I18nManager.getConstants()).toEqual({
61+
doLeftAndRightSwapInRTL: false,
62+
isRTL: true,
63+
});
64+
});
65+
66+
it('keeps isRTL when forceRTL returns nothing on older native binaries', () => {
67+
mockGetConstants.mockReturnValue({isRTL: false});
68+
mockForceRTL.mockReturnValue(undefined);
69+
const I18nManager = require('../I18nManager').default;
70+
71+
I18nManager.forceRTL(true);
72+
73+
expect(mockForceRTL).toHaveBeenCalledWith(true);
74+
expect(I18nManager.isRTL).toBe(false);
75+
});
4676
});

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#import <FBReactNativeSpec/FBReactNativeSpec.h>
99

1010
#import <React/RCTI18nUtil.h>
11+
#import <React/RCTSurfacePresenterStub.h>
1112
#import "RCTI18nManager.h"
1213

1314
#import "CoreModulesPlugins.h"
@@ -17,7 +18,9 @@
1718
@interface RCTI18nManager () <NativeI18nManagerSpec>
1819
@end
1920

20-
@implementation RCTI18nManager
21+
@implementation RCTI18nManager {
22+
__weak id<RCTSurfacePresenterStub> _surfacePresenter;
23+
}
2124

2225
RCT_EXPORT_MODULE()
2326

@@ -26,9 +29,26 @@ + (BOOL)requiresMainQueueSetup
2629
return NO;
2730
}
2831

29-
- (void)forceRTL:(BOOL)value
32+
- (void)setSurfacePresenter:(id<RCTSurfacePresenterStub>)surfacePresenter
33+
{
34+
_surfacePresenter = surfacePresenter;
35+
}
36+
37+
- (NSNumber *)forceRTL:(BOOL)value
3038
{
39+
BOOL wasRTL = [[RCTI18nUtil sharedInstance] isRTL];
3140
[[RCTI18nUtil sharedInstance] forceRTL:value];
41+
BOOL isRTL = [[RCTI18nUtil sharedInstance] isRTL];
42+
43+
if (isRTL != wasRTL) {
44+
// Returns before the relayout, so JS reads the new value before any layout event
45+
id<RCTSurfacePresenterStub> surfacePresenter = _surfacePresenter;
46+
dispatch_async(dispatch_get_main_queue(), ^{
47+
[surfacePresenter updateLayoutDirectionOfSurfaces];
48+
});
49+
}
50+
51+
return @(isRTL);
3252
}
3353
3454
- (NSDictionary *)constantsToExport

‎packages/react-native/React/Fabric/RCTSurfacePresenter.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ NS_ASSUME_NONNULL_BEGIN
6565

6666
- (nullable RCTFabricSurface *)surfaceForRootTag:(ReactTag)rootTag;
6767

68+
/*
69+
* Lays out all surfaces again with the current layout direction. Must be called on the main thread.
70+
*/
71+
- (void)updateLayoutDirectionOfSurfaces;
72+
6873
- (void)synchronouslyUpdateViewOnUIThread:(NSNumber *)reactTag props:(NSDictionary *)props;
6974
- (void)schedulerDidSynchronouslyUpdateViewOnUIThread:(ReactTag)tag props:(folly::dynamic)props;
7075

‎packages/react-native/React/Fabric/RCTSurfacePresenter.mm‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ - (RCTFabricSurface *)surfaceForRootTag:(ReactTag)rootTag
168168
return [_surfaceRegistry surfaceForRootTag:rootTag];
169169
}
170170
171+
- (void)updateLayoutDirectionOfSurfaces
172+
{
173+
RCTAssertMainQueue();
174+
[_surfaceRegistry enumerateWithBlock:^(NSEnumerator<RCTFabricSurface *> *enumerator) {
175+
for (RCTFabricSurface *surface in enumerator) {
176+
[surface updateLayoutContext];
177+
}
178+
}];
179+
}
180+
171181
- (id<RCTSurfaceProtocol>)createFabricSurfaceForModuleName:(NSString *)moduleName
172182
initialProperties:(NSDictionary *)initialProperties
173183
{

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ NS_ASSUME_NONNULL_BEGIN
115115

116116
- (const facebook::react::SurfaceHandler &)surfaceHandler;
117117

118+
/**
119+
* Lays out the Surface again with the current layout direction, screen scale
120+
* and font size multiplier. Must be called on the main thread.
121+
*/
122+
- (void)updateLayoutContext;
123+
118124
@end
119125

120126
NS_ASSUME_NONNULL_END

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ - (instancetype)initWithSurfacePresenter:(RCTSurfacePresenter *)surfacePresenter
6363

6464
[self setMinimumSize:CGSizeZero maximumSize:RCTViewportSize()];
6565

66-
[self _updateLayoutContext];
66+
[self updateLayoutContext];
6767

6868
[[NSNotificationCenter defaultCenter] addObserver:self
6969
selector:@selector(handleContentSizeCategoryDidChangeNotification:)
@@ -157,7 +157,7 @@ - (RCTSurfaceView *)view
157157
158158
if (!_view) {
159159
_view = [[RCTSurfaceView alloc] initWithSurface:self];
160-
[self _updateLayoutContext];
160+
[self updateLayoutContext];
161161
#if !TARGET_OS_TV
162162
_touchHandler = [RCTSurfaceTouchHandler new];
163163
[_touchHandler attachToView:_view];
@@ -186,7 +186,7 @@ - (void)_propagateStageChange
186186
}
187187
}
188188
189-
- (void)_updateLayoutContext
189+
- (void)updateLayoutContext
190190
{
191191
auto layoutConstraints = _surfaceHandler->getLayoutConstraints();
192192
layoutConstraints.layoutDirection = RCTLayoutDirection([[RCTI18nUtil sharedInstance] isRTL]);
@@ -288,7 +288,7 @@ - (BOOL)synchronouslyWaitFor:(NSTimeInterval)timeout
288288
289289
- (void)handleContentSizeCategoryDidChangeNotification:(NSNotification *)notification
290290
{
291-
[self _updateLayoutContext];
291+
[self updateLayoutContext];
292292
}
293293
294294
#pragma mark - Private

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
@implementation RCTI18nUtil {
1313
BOOL _isRTLSupported;
14+
BOOL _isRTLForced;
1415
}
1516

1617
+ (instancetype)sharedInstance
@@ -28,6 +29,7 @@ - (instancetype)init
2829
{
2930
if (self = [super init]) {
3031
_isRTLSupported = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"RCTSupportsRTL"] boolValue];
32+
_isRTLForced = NO;
3133
}
3234
return self;
3335
}
@@ -59,14 +61,12 @@ - (BOOL)isRTL
5961
*/
6062
- (BOOL)isRTLForced
6163
{
62-
BOOL rtlStatus = [[NSUserDefaults standardUserDefaults] boolForKey:@"RCTI18nUtil_forceRTL"];
63-
return rtlStatus;
64+
return _isRTLForced;
6465
}
6566
6667
- (void)forceRTL:(BOOL)rtlStatus
6768
{
68-
[[NSUserDefaults standardUserDefaults] setBool:rtlStatus forKey:@"RCTI18nUtil_forceRTL"];
69-
[[NSUserDefaults standardUserDefaults] synchronize];
69+
_isRTLForced = rtlStatus;
7070
}
7171
7272
// Check if the current device language is RTL

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ NS_ASSUME_NONNULL_BEGIN
3535
- (void)synchronouslyUpdateViewOnUIThread:(NSNumber *)reactTag props:(NSDictionary *)props;
3636
- (void)addObserver:(id<RCTSurfacePresenterObserver>)observer;
3737
- (void)removeObserver:(id<RCTSurfacePresenterObserver>)observer;
38+
- (void)updateLayoutDirectionOfSurfaces;
3839

3940
@end
4041

0 commit comments

Comments
 (0)