Skip to content

Commit 769e14c

Browse files
authored
docs(cndocs): 同步多篇 React Native 中文文档更新 (#1008)
* docs(cndocs): 同步翻译更新(高质量) * fix: resync translations when upstream docs are newer * chore: consolidate translation sync entrypoints * docs(cndocs): 补齐 AccessibilityInfo 同步内容 * docs(cndocs): 同步 DevSettings 等文档更新
1 parent eda9403 commit 769e14c

File tree

7 files changed

+226
-101
lines changed

7 files changed

+226
-101
lines changed

cndocs/_remove-global-cli.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
> 如果你之前全局安装过旧的`react-native-cli`命令行工具,请使用`npm uninstall -g react-native-cli`卸载掉它以避免一些冲突:
2-
>
3-
> ```shell
4-
> npm uninstall -g react-native-cli @react-native-community/cli
5-
> ```
1+
:::warning
2+
如果你之前全局安装过 `react-native-cli` 包,请将其移除,因为它可能会导致一些意外问题:
3+
4+
```shell
5+
npm uninstall -g react-native-cli @react-native-community/cli
6+
```
7+
8+
:::

cndocs/accessibilityinfo.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,26 @@ static isScreenReaderEnabled(): Promise<boolean>;
225225

226226
---
227227

228+
### `isHighTextContrastEnabled()` <div className="label android">Android</div>
229+
230+
```tsx
231+
static isHighTextContrastEnabled(): Promise<boolean>
232+
```
233+
234+
查询是否启用了高对比度文本。返回 Promise,启用为 `true`,否则为 `false`
235+
236+
---
237+
238+
### `isDarkerSystemColorsEnabled()` <div className="label ios">iOS</div>
239+
240+
```tsx
241+
static isDarkerSystemColorsEnabled(): Promise<boolean>
242+
```
243+
244+
查询是否启用了更深的系统颜色。返回 Promise,启用为 `true`,否则为 `false`
245+
246+
---
247+
228248
### `prefersCrossFadeTransitions()` <div className="label ios">iOS</div>
229249

230250
```tsx
@@ -237,6 +257,10 @@ static prefersCrossFadeTransitions(): Promise<boolean>;
237257

238258
### 🗑️ `setAccessibilityFocus()`
239259

260+
:::warning Deprecated
261+
建议改用 `sendAccessibilityEvent`,并将 `eventType` 设为 `focus`
262+
:::
263+
240264
```tsx
241265
static setAccessibilityFocus(reactTag: number);
242266
```
@@ -248,3 +272,22 @@ static setAccessibilityFocus(reactTag: number);
248272
:::note
249273
确保希望接收无障碍焦点的 `View` 设置了 `accessible={true}`
250274
:::
275+
276+
---
277+
278+
### `sendAccessibilityEvent()`
279+
280+
```tsx
281+
static sendAccessibilityEvent(host: HostInstance, eventType: AccessibilityEventTypes);
282+
```
283+
284+
以命令式方式在 React 组件上触发无障碍事件,例如为读屏器切换当前聚焦元素。
285+
286+
:::note
287+
确保希望接收无障碍焦点的 `View` 设置了 `accessible={true}`
288+
:::
289+
290+
| 名称 | 类型 | 说明 |
291+
| -------------------------------------------------------------- | ----------------------- | -------------------------------------------------------------------- |
292+
| host <div className="label basic required">Required</div> | HostInstance | 要向其发送事件的组件 ref。 |
293+
| eventType <div className="label basic required">Required</div> | AccessibilityEventTypes | `'click'`(仅 Android)、`'focus'``'viewHoverEnter'`(仅 Android)或 `'windowStateChange'`(仅 Android)之一 |

cndocs/devsettings.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,39 @@ title: DevSettings
1313

1414
### `addMenuItem()`
1515

16-
```jsx
17-
static addMenuItem(title, handler)
16+
```tsx
17+
static addMenuItem(title: string, handler: () => any);
1818
```
1919

20-
在开发者菜单中添加一个自定义的菜单项
20+
在开发者菜单中添加一个自定义的菜单项
2121

2222
**参数:**
2323

24-
| 名称 | 类型 |
25-
| -------------------------------------------------------------- | -------- |
26-
| title <div className="label basic required">必需</div> | string |
27-
| component <div className="label basic required">必需</div> | function |
24+
| 名称 | 类型 |
25+
| ------------------------------------------------------------ | -------- |
26+
| title <div className="label basic required">必需</div> | string |
27+
| handler <div className="label basic required">必需</div> | function |
2828

2929
**示例:**
3030

31-
```jsx
31+
```tsx
3232
DevSettings.addMenuItem('Show Secret Dev Screen', () => {
3333
Alert.alert('Showing secret dev screen!');
3434
});
3535
```
3636

37+
---
38+
3739
### `reload()`
3840

39-
```jsx
40-
static reload()
41+
```tsx
42+
static reload(reason?: string): void;
4143
```
4244

43-
重新加载引用(刷新)。可以直接调用也可以通过用户交互来触发。
45+
重新加载应用。可以直接调用,也可以通过用户交互来触发。
46+
47+
**示例:**
4448

45-
```jsx
49+
```tsx
4650
<Button title="Reload" onPress={() => DevSettings.reload()} />
4751
```

cndocs/dimensions.md

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,33 @@ id: dimensions
33
title: Dimensions
44
---
55

6-
import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import constants from '@site/core/TabsConstants';
6+
:::info
7+
对于 React 组件,[`useWindowDimensions`](usewindowdimensions) 是首选 API。与 `Dimensions` 不同,它会在窗口尺寸更新时同步更新。这种方式也更符合 React 的编程范式。
8+
:::
79

8-
本模块用于获取设备屏幕的宽高。
9-
10-
> 对于 React 函数组件,我们更推荐使用[`useWindowDimensions`](usewindowdimensions)这个 Hook API。和 `Dimensions` 不同,它会在屏幕尺寸变化时自动更新。
11-
12-
```jsx
13-
import { Dimensions } from 'react-native';
10+
```tsx
11+
import {Dimensions} from 'react-native';
1412
```
1513

16-
你可以用下面的方法来获取设备的宽高
14+
你可以使用下面的代码获取应用窗口的宽度和高度
1715

18-
```jsx
16+
```tsx
1917
const windowWidth = Dimensions.get('window').width;
2018
const windowHeight = Dimensions.get('window').height;
2119
```
2220

23-
> 注意:尽管尺寸信息立即就可用,但它可能会在将来被修改(譬如设备的方向改变),所以基于这些常量的渲染逻辑和样式应当每次 render 之后都调用此函数,而不是将对应的值保存下来。(举例来说,你可能需要使用内联的样式而不是在`StyleSheet`中保存相应的尺寸)。
21+
:::note
22+
尽管尺寸信息会立即可用,但它们可能发生变化(例如设备旋转、可折叠设备等),因此任何依赖这些常量的渲染逻辑或样式,都应尽量在每次渲染时调用此函数,而不是缓存其值(例如,使用内联样式,而不是把值写死在 `StyleSheet` 中)。
23+
:::
2424

25-
如果你要考虑可折叠的设备,或者其他屏幕尺寸可变的设备,可以参考下面例子中所使用的事件监听函数或是[`useWindowDimensions`](usewindowdimensions)
25+
如果你的目标设备是可折叠设备,或者设备的屏幕尺寸、应用窗口尺寸可能发生变化,可以像下面的示例一样使用 Dimensions 模块提供的事件监听器。
2626

2727
## 示例
2828

29-
```SnackPlayer name=Dimensions
29+
```SnackPlayer name=Dimensions%20Example
3030
import React, {useState, useEffect} from 'react';
31-
import {View, StyleSheet, Text, Dimensions} from 'react-native';
31+
import {StyleSheet, Text, Dimensions} from 'react-native';
32+
import {SafeAreaView, SafeAreaProvider} from 'react-native-safe-area-context';
3233
3334
const windowDimensions = Dimensions.get('window');
3435
const screenDimensions = Dimensions.get('screen');
@@ -50,20 +51,22 @@ const App = () => {
5051
});
5152
5253
return (
53-
<View style={styles.container}>
54-
<Text style={styles.header}>Window Dimensions</Text>
55-
{Object.entries(dimensions.window).map(([key, value]) => (
56-
<Text>
57-
{key} - {value}
58-
</Text>
59-
))}
60-
<Text style={styles.header}>Screen Dimensions</Text>
61-
{Object.entries(dimensions.screen).map(([key, value]) => (
62-
<Text>
63-
{key} - {value}
64-
</Text>
65-
))}
66-
</View>
54+
<SafeAreaProvider>
55+
<SafeAreaView style={styles.container}>
56+
<Text style={styles.header}>Window Dimensions</Text>
57+
{Object.entries(dimensions.window).map(([key, value]) => (
58+
<Text>
59+
{key} - {value}
60+
</Text>
61+
))}
62+
<Text style={styles.header}>Screen Dimensions</Text>
63+
{Object.entries(dimensions.screen).map(([key, value]) => (
64+
<Text>
65+
{key} - {value}
66+
</Text>
67+
))}
68+
</SafeAreaView>
69+
</SafeAreaProvider>
6770
);
6871
};
6972
@@ -82,9 +85,7 @@ const styles = StyleSheet.create({
8285
export default App;
8386
```
8487

85-
---
86-
87-
# 文档
88+
# 参考
8889

8990
## 方法
9091

cndocs/turbo-native-modules-ios.md

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,36 @@ import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import con
99

1010
## 准备你的 Xcode 项目
1111

12-
我们需要使用 Xcode 准备你的 iOS 项目。完成这 **6 步** 后,你将拥有一个实现 `NativeLocalStorageSpec` 接口的 `RCTNativeLocalStorage`
12+
我们需要使用 Xcode 准备你的 iOS 项目。完成这 **6 步** 后,你将拥有一个实现生成的 `NativeLocalStorageSpec` 接口的 `RCTNativeLocalStorage`
1313

14-
1. 打开 CocoPods 生成的 Xcode workspace 文件
14+
1. 打开 CocoaPods 生成的 Xcode Workspace
1515

1616
```bash
1717
cd ios
1818
open TurboModuleExample.xcworkspace
1919
```
2020

21-
<img class="half-size" alt="Open Xcode Workspace" src="/docs/assets/turbo-native-modules/xcode/1.webp" />
21+
<img className="half-size" alt="Open Xcode Workspace" src="/docs/assets/turbo-native-modules/xcode/1.webp" />
2222

2323
2. 右键点击应用,选择 <code>New Group</code>,将新组命名为 `NativeLocalStorage`
2424

25-
<img class="half-size" alt="Right click on app and select New Group" src="/docs/assets/turbo-native-modules/xcode/2.webp" />
25+
<img className="half-size" alt="Right click on app and select New Group" src="/docs/assets/turbo-native-modules/xcode/2.webp" />
2626

2727
3.`NativeLocalStorage` 组中,创建 <code>New</code>→<code>File from Template</code>。
2828

29-
<img class="half-size" alt="Create a new file using the Cocoa Touch Class template" src="/docs/assets/turbo-native-modules/xcode/3.webp" />
29+
<img className="half-size" alt="Create a new file using the Cocoa Touch Class template" src="/docs/assets/turbo-native-modules/xcode/3.webp" />
3030

3131
4. 使用 <code>Cocoa Touch Class</code> 模板。
3232

33-
<img class="half-size" alt="Use the Cocoa Touch Class template" src="/docs/assets/turbo-native-modules/xcode/4.webp" />
33+
<img className="half-size" alt="Use the Cocoa Touch Class template" src="/docs/assets/turbo-native-modules/xcode/4.webp" />
3434

35-
5. 将类命名为 <code>RCTNativeLocalStorage</code>,并选择 <code>Objective-C</code> 语言。
35+
5. 将 Cocoa Touch Class 命名为 <code>RCTNativeLocalStorage</code>,并选择 <code>Objective-C</code> 语言。
3636

37-
<img class="half-size" alt="Create an Objective-C RCTNativeLocalStorage class" src="/docs/assets/turbo-native-modules/xcode/5.webp" />
37+
<img className="half-size" alt="Create an Objective-C RCTNativeLocalStorage class" src="/docs/assets/turbo-native-modules/xcode/5.webp" />
3838

3939
6. 将 <code>RCTNativeLocalStorage.m</code> 重命名为 <code>RCTNativeLocalStorage.mm</code>,使其成为 Objective-C++ 文件。
4040

41-
<img class="half-size" alt="Convert to and Objective-C++ file" src="/docs/assets/turbo-native-modules/xcode/6.webp" />
41+
<img className="half-size" alt="Convert to and Objective-C++ file" src="/docs/assets/turbo-native-modules/xcode/6.webp" />
4242

4343
## 使用 NSUserDefaults 实现 localStorage
4444

@@ -78,8 +78,6 @@ static NSString *const RCTNativeLocalStorageKey = @"local-storage";
7878

7979
@implementation RCTNativeLocalStorage
8080

81-
RCT_EXPORT_MODULE(NativeLocalStorage)
82-
8381
- (id) init {
8482
if (self = [super init]) {
8583
_localStorage = [[NSUserDefaults alloc] initWithSuiteName:RCTNativeLocalStorageKey];
@@ -111,14 +109,57 @@ RCT_EXPORT_MODULE(NativeLocalStorage)
111109
}
112110
}
113111

112+
+ (NSString *)moduleName
113+
{
114+
return @"NativeLocalStorage";
115+
}
116+
114117
@end
115118
```
116119
117-
重要注意事项
120+
需要注意的要点
118121
119-
- `RCT_EXPORT_MODULE` 导出并注册模块,使用我们在 JavaScript 环境中访问它的标识符:`NativeLocalStorage`。更多详情请参阅 [docs](./legacy/native-modules-ios#module-name)。
120122
- 你可以使用 Xcode 跳转到 Codegen `@protocol NativeLocalStorageSpec`。你也可以使用 Xcode 为你生成 stub。
121123
124+
## 在应用中注册原生模块
125+
126+
最后一步是更新 `package.json`,告诉 React Native 如何将原生模块的 JS 规格与这些规格在原生代码中的具体实现关联起来。
127+
128+
按如下方式修改 `package.json`:
129+
130+
```json title="package.json"
131+
"start": "react-native start",
132+
"test": "jest"
133+
},
134+
"codegenConfig": {
135+
"name": "NativeLocalStorageSpec",
136+
"type": "modules",
137+
"jsSrcsDir": "specs",
138+
"android": {
139+
"javaPackageName": "com.sampleapp.specs"
140+
},
141+
// highlight-add-start
142+
"ios": {
143+
"modulesProvider": {
144+
"NativeLocalStorage": "RCTNativeLocalStorage"
145+
}
146+
}
147+
// highlight-add-end
148+
},
149+
150+
"dependencies": {
151+
```
152+
153+
此时,你需要重新安装 Pods,以确保再次运行 codegen 来生成新文件:
154+
155+
```bash
156+
# from the ios folder
157+
bundle exec pod install
158+
open SampleApp.xcworkspace
159+
```
160+
161+
如果你现在从 Xcode 构建应用,应该就可以成功构建。
162+
122163
## 在模拟器上构建并运行你的代码
123164

124165
<Tabs groupId="package-manager" queryString defaultValue={constants.defaultPackageManager} values={constants.packageManagers}>
@@ -135,6 +176,6 @@ yarn run ios
135176
</Tabs>
136177

137178
<video width="30%" height="30%" playsinline="true" autoplay="true" muted="true" loop="true">
138-
<source src="https://cdn.jsdelivr.net/gh/reactnativecn/react-native-website@gh-pages/docs/assets/turbo-native-modules/turbo-native-modules-ios.webm" type="video/webm" />
139-
<source src="https://cdn.jsdelivr.net/gh/reactnativecn/react-native-website@gh-pages/docs/assets/turbo-native-modules/turbo-native-modules-ios.mp4" type="video/mp4" />
179+
<source src="/docs/assets/turbo-native-modules/turbo-native-modules-ios.webm" type="video/webm" />
180+
<source src="/docs/assets/turbo-native-modules/turbo-native-modules-ios.mp4" type="video/mp4" />
140181
</video>

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"build": "yarn --cwd cnwebsite build",
1717
"build:fast": "yarn --cwd cnwebsite build:fast",
1818
"serve": "yarn --cwd cnwebsite serve",
19+
"sync-translations": "bash ./scripts/sync-translations.sh",
1920
"prepare": "husky",
2021
"lint": "eslint .",
2122
"lint:packages": "yarn workspaces foreach --from \"packages/*\" -p -R run lint",

0 commit comments

Comments
 (0)