Skip to content

Commit 2ff5c7a

Browse files
scarlacSeph Soliman
andauthored
Add I18nManager docs (facebook#4626)
Added to sidebar as well. Related to facebook/react-native#51661 and facebook/react-native#51648 Co-authored-by: Seph Soliman <[email protected]>
1 parent 1ffaf35 commit 2ff5c7a

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed

docs/i18nmanager.md

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
---
2+
id: i18nmanager
3+
title: I18nManager
4+
---
5+
6+
# I18nManager
7+
8+
The `I18nManager` module provides utilities for managing Right-to-Left (RTL) layout support for languages like Arabic, Hebrew, and others. It provides methods to control RTL behavior and check the current layout direction.
9+
10+
## Examples
11+
12+
### Change positions and animations based on RTL
13+
14+
If you absolutely position elements to align with other flexbox elements, they may not align in RTL languages. Using `isRTL` can be used to adjust alignment or animations.
15+
16+
```SnackPlayer name=I18nManager%20Change%20Absolute%20Positions%20And%20Animations
17+
import React from 'react';
18+
import {I18nManager, Text, View} from 'react-native';
19+
import {SafeAreaView, SafeAreaProvider} from 'react-native-safe-area-context';
20+
21+
const App = () => {
22+
// Change to `true` to see the effect in a non-RTL language
23+
const isRTL = I18nManager.isRTL;
24+
return (
25+
<SafeAreaProvider>
26+
<SafeAreaView>
27+
<View style={{ position: 'absolute', left: isRTL ? undefined : 0, right: isRTL ? 0 : undefined }}>
28+
{isRTL ? (
29+
<Text>Back &gt;</Text>
30+
) : (
31+
<Text>&lt; Back</Text>
32+
)}
33+
</View>
34+
</SafeAreaView>
35+
</SafeAreaProvider>
36+
);
37+
};
38+
39+
export default App;
40+
```
41+
42+
### During Development
43+
44+
```SnackPlayer name=I18nManager%20During%20Development
45+
import React, {useState} from 'react';
46+
import {Alert, I18nManager, StyleSheet, Switch, Text, View} from 'react-native';
47+
import {SafeAreaView, SafeAreaProvider} from 'react-native-safe-area-context';
48+
49+
const App = () => {
50+
const [rtl, setRTL] = useState(I18nManager.isRTL);
51+
return (
52+
<SafeAreaProvider>
53+
<SafeAreaView>
54+
<View style={styles.container}>
55+
<View style={styles.forceRtl}>
56+
<Text>Force RTL in Development:</Text>
57+
<Switch value={rtl} onValueChange={(value) => {
58+
setRTL(value);
59+
I18nManager.forceRTL(value);
60+
Alert.alert(
61+
'Reload this page',
62+
'Please reload this page to change the UI direction! ' +
63+
'All examples in this app will be affected. ' +
64+
'Check them out to see what they look like in RTL layout.',
65+
);
66+
}} />
67+
</View>
68+
</View>
69+
</SafeAreaView>
70+
</SafeAreaProvider>
71+
);
72+
};
73+
74+
const styles = StyleSheet.create({
75+
container: {
76+
padding: 20,
77+
},
78+
forceRtl: {
79+
flexDirection: 'row',
80+
justifyContent: 'space-between',
81+
alignItems: 'center',
82+
},
83+
});
84+
85+
export default App;
86+
```
87+
88+
# Reference
89+
90+
## Properties
91+
92+
### `isRTL`
93+
94+
```typescript
95+
static isRTL: boolean;
96+
```
97+
98+
A boolean value indicating whether the app is currently in RTL layout mode.
99+
100+
The value of `isRTL` is determined by the following logic:
101+
102+
- If `forceRTL` is `true`, `isRTL` returns `true`
103+
- If `allowRTL` is `false`, `isRTL` returns `false`
104+
- Otherwise, `isRTL` will be `true` given the following:
105+
- **iOS:**
106+
- The user-preferred language on the device is an RTL language
107+
- The application-defined localizations include the user-chosen language (as defined in the Xcode project file (`knownRegions = (...)`)
108+
- **Android:**
109+
- The user-preferred language on the device is an RTL language
110+
- The application's `AndroidManifest.xml` defines `android:supportsRTL="true"` on the `<application>` element
111+
112+
### `doLeftAndRightSwapInRTL`
113+
114+
```typescript
115+
static doLeftAndRightSwapInRTL: boolean;
116+
```
117+
118+
A boolean value indicating whether left and right style properties should be automatically swapped when in RTL mode. When enabled, left becomes right and right becomes left in RTL layouts.
119+
120+
## Methods
121+
122+
### `allowRTL()`
123+
124+
```typescript
125+
static allowRTL: (allowRTL: boolean) => void;
126+
```
127+
128+
Enables or disables RTL layout support for the application.
129+
130+
**Parameters:**
131+
132+
- `allowRTL` (boolean): Whether to allow RTL layout
133+
134+
**Important Notes:**
135+
136+
- Changes take effect on the next application start, not immediately
137+
- This setting is persisted across app restarts
138+
139+
### `forceRTL()`
140+
141+
```typescript
142+
static forceRTL: (forced: boolean) => void;
143+
```
144+
145+
Forces the app to use RTL layout regardless of the device language settings. This is primarily useful for testing RTL layouts during development.
146+
147+
Avoid forcing RTL in production apps as it requires a full app restart to take effect, which makes for a poor user-experience.
148+
149+
**Parameters:**
150+
151+
- `forced` (boolean): Whether to force RTL layout
152+
153+
**Important Notes:**
154+
155+
- Changes take full effect on the next application start, not immediately
156+
- The setting is persisted across app restarts
157+
- Only meant for development and testing. In production, you should either disallow RTL fully or handle it appropriately (see `isRTL`)
158+
159+
### `swapLeftAndRightInRTL()`
160+
161+
```typescript
162+
static swapLeftAndRightInRTL: (swapLeftAndRight: boolean) => void;
163+
```
164+
165+
Swap left and right style properties in RTL mode. When enabled, left becomes right and right becomes left in RTL layouts. Does not affect the value of `isRTL`.

website/sidebars.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ export default {
204204
'devsettings',
205205
'dimensions',
206206
'easing',
207+
'i18nmanager',
207208
'interactionmanager',
208209
'keyboard',
209210
'layoutanimation',

0 commit comments

Comments
 (0)