Skip to content

Commit 0271a23

Browse files
committed
feat: add localization
1 parent 0a991ef commit 0271a23

13 files changed

Lines changed: 178 additions & 26 deletions

File tree

App.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { DefaultTheme, Provider as PaperProvider } from "react-native-paper";
55
import useCachedResources from "./hooks/useCachedResources";
66
import useColorScheme from "./hooks/useColorScheme";
77
import Navigation from "./navigation";
8-
98
const theme = {
109
...DefaultTheme,
1110
colors: {

lang/en-us.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"general": {
3+
"start": "START",
4+
5+
"ip": "IP",
6+
"lossRate": "Loss Rate",
7+
"respondTime": "Respond Time",
8+
"downloadSpeed": "Download Speed"
9+
},
10+
"testRun": {
11+
"title": "Test Run",
12+
"testRespond": "TEST RESPOND",
13+
"testDownload": "TEST DOWNLOAD",
14+
"ipCount": "ip count",
15+
"coCurrentCount": "coCurrent count",
16+
"testUrl": "test url"
17+
},
18+
"testStatistics": {
19+
"title": "Test Statistics",
20+
"totalRespond": "Total Respond",
21+
"respondSuccessRate": "Respond Suc Rate",
22+
"meanRespondTime": "Mean Respond Time",
23+
"totalDownload": "Total Download",
24+
"downloadSuccessRate": "Download Suc Rate",
25+
"meanDownload": "Mean Download",
26+
"showAllColumns": "Show All Columns",
27+
"hideSomeColumns": "Hide Some Columns"
28+
},
29+
"testConfig": {
30+
"title": "Test Config"
31+
}
32+
}

lang/zh-cn.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"general": {
3+
"start": "开始",
4+
5+
"ip": "IP",
6+
"lossRate": "丢包率",
7+
"respondTime": "响应时长",
8+
"downloadSpeed": "下载速度"
9+
},
10+
"testRun": {
11+
"title": "运行测试",
12+
"testRespond": "测试响应",
13+
"testDownload": "测试下载",
14+
"ipCount": "IP 数量",
15+
"coCurrentCount": "并发数",
16+
"testUrl": "测试地址"
17+
},
18+
"testStatistics": {
19+
"title": "测试统计",
20+
"totalRespond": "响应次数",
21+
"respondSuccessRate": "响应成功率",
22+
"meanRespondTime": "平均响应时长",
23+
"totalDownload": "下载次数",
24+
"downloadSuccessRate": "下载成功率",
25+
"meanDownload": "平均下载速度",
26+
"showAllColumns": "展示所有列",
27+
"hideSomeColumns": "隐藏某些列"
28+
},
29+
"testConfig": {
30+
"title": "测试配置"
31+
}
32+
}

localize/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import I18n from "i18n-js";
2+
import * as RNLocalize from "react-native-localize";
3+
import cn from "../lang/zh-cn.json";
4+
import en from "../lang/en-us.json";
5+
6+
// 获取手机本地国际化信息
7+
const locales = RNLocalize.getLocales();
8+
const systemLanguage = locales[0]?.languageCode; // 用户系统偏好语言
9+
console.log(locales, 12, systemLanguage);
10+
11+
// 如果获取到了即使用,否则启用默认语言
12+
if (systemLanguage) {
13+
I18n.locale = systemLanguage;
14+
} else {
15+
I18n.locale = "en"; // 用户既没有设置,也没有获取到系统语言,默认加载英语语言资源
16+
}
17+
18+
I18n.fallbacks = true;
19+
20+
// 加载语言包
21+
I18n.translations = {
22+
zh: cn,
23+
cn,
24+
en,
25+
};
26+
27+
export { I18n };

navigation/index.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
} from "../types";
2424
import LinkingConfiguration from "./LinkingConfiguration";
2525
import TestStatisticsScreen from "@/screens/TestStatisticsScreen";
26+
import { I18n } from "@/localize";
2627

2728
export default function Navigation({
2829
colorScheme,
@@ -85,7 +86,7 @@ function BottomTabNavigator() {
8586
name="TestRun"
8687
component={TestRunScreen}
8788
options={({ navigation }: RootTabScreenProps<"TestRun">) => ({
88-
title: "Test Run",
89+
title: I18n.t("testRun.title"),
8990
tabBarIcon: ({ color }) => <TabBarIcon name="code" color={color} />,
9091
headerRight: () => (
9192
<Pressable
@@ -108,7 +109,7 @@ function BottomTabNavigator() {
108109
name="TestStatistics"
109110
component={TestStatisticsScreen}
110111
options={({ navigation }: RootTabScreenProps<"TestStatistics">) => ({
111-
title: "Test Statistics",
112+
title: I18n.t("testStatistics.title"),
112113
tabBarIcon: ({ color }) => <TabBarIcon name="code" color={color} />,
113114
headerRight: () => (
114115
<Pressable
@@ -131,7 +132,7 @@ function BottomTabNavigator() {
131132
name="TestConfig"
132133
component={ConfigScreen}
133134
options={{
134-
title: "Test Config",
135+
title: I18n.t("testConfig.title"),
135136
tabBarIcon: ({ color }) => <TabBarIcon name="code" color={color} />,
136137
}}
137138
/>

package-lock.json

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@
2929
"expo-splash-screen": "~0.14.0",
3030
"expo-status-bar": "~1.2.0",
3131
"expo-web-browser": "~10.1.0",
32+
"i18n-js": "^3.9.2",
3233
"lodash-es": "^4.17.21",
3334
"mobx": "^6.5.0",
3435
"mobx-react": "^7.3.0",
3536
"react": "17.0.1",
3637
"react-dom": "17.0.1",
3738
"react-native": "0.64.3",
39+
"react-native-localize": "^2.2.1",
3840
"react-native-paper": "^4.12.1",
3941
"react-native-safe-area-context": "3.3.2",
4042
"react-native-screens": "~3.10.1",
@@ -44,6 +46,7 @@
4446
},
4547
"devDependencies": {
4648
"@babel/core": "^7.12.9",
49+
"@types/i18n-js": "^3.8.2",
4750
"@types/lodash-es": "^4.17.6",
4851
"@types/react": "~17.0.21",
4952
"@types/react-native": "~0.64.12",

screens/TestConfigScreen.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { StyleSheet } from "react-native";
22

33
import { Text, View } from "../components/Themed";
4+
import { I18n } from "@/localize";
45

56
export default function TestConfigScreen() {
67
return (
78
<View style={styles.container}>
8-
<Text style={styles.title}>Test Config</Text>
9+
<Text style={styles.title}>{I18n.t("testConfig.title")}</Text>
910
<View
1011
style={styles.separator}
1112
lightColor="#eee"

screens/TestRunScreen/components/TestPage.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { initialTestPageTableHeaderCols, MyTableHeaderColumn } from "../model";
1212
import { useTestRunningStatus } from "../hooks/useTestRunningStatus";
1313
import { miniStyle } from "@/theme";
1414
import { Button } from "react-native-paper";
15-
15+
import { I18n } from "@/localize";
1616
export default function TestPage({ path }: { path: string }) {
1717
const { testIpCount, setTestIpCount, getIpList } = useTestIpCount();
1818
const [testIpCoCurrentCount, setTestIpCoCurrentCount] = useState<string>("5");
@@ -93,7 +93,7 @@ export default function TestPage({ path }: { path: string }) {
9393
contentStyle={{ ...styles.paperBtnContent }}
9494
labelStyle={{ ...miniStyle.textStyle }}
9595
>
96-
TEST RESPOND
96+
{I18n.t("testRun.testRespond")}
9797
</Button>
9898
<View style={{ marginRight: 5 }}></View>
9999
<Button
@@ -108,7 +108,7 @@ export default function TestPage({ path }: { path: string }) {
108108
contentStyle={{ ...styles.paperBtnContent }}
109109
labelStyle={{ ...miniStyle.textStyle }}
110110
>
111-
TEST DOWNLOAD
111+
{I18n.t("testRun.testDownload")}
112112
</Button>
113113
<View style={{ marginRight: 5 }}></View>
114114

@@ -118,18 +118,18 @@ export default function TestPage({ path }: { path: string }) {
118118
contentStyle={{ ...styles.paperBtnContent }}
119119
labelStyle={{ ...miniStyle.textStyle }}
120120
>
121-
START
121+
{I18n.t("general.start")}
122122
</Button>
123123
</View>
124124
<View style={styles.toolbar}>
125-
<Text>ip count</Text>
125+
<Text> {I18n.t("testRun.ipCount")}</Text>
126126
<TextInput
127127
style={styles.input}
128128
onChangeText={onTestIpCountChange}
129129
value={testIpCount}
130130
keyboardType="numeric"
131131
/>
132-
<Text>coCurrent count</Text>
132+
<Text> {I18n.t("testRun.coCurrentCount")}</Text>
133133
<TextInput
134134
style={styles.input}
135135
onChangeText={(val) => setTestIpCoCurrentCount(() => val)}
@@ -138,7 +138,7 @@ export default function TestPage({ path }: { path: string }) {
138138
/>
139139
</View>
140140
<View style={styles.toolbar}>
141-
<Text>test url</Text>
141+
<Text>{I18n.t("testRun.testUrl")}</Text>
142142
<TextInput
143143
style={{ ...styles.input, flex: 1 }}
144144
onChangeText={(val) => setTestUrl(() => val)}

screens/TestRunScreen/model.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { I18n } from '@/localize';
12
import { RequestStatus } from "@/typings/index";
23
import { TableHeaderColumn } from "@/components/Table/TableHeader";
34
export type CfIpResponse = {
@@ -14,10 +15,10 @@ export type MyTableHeaderColumn = TableHeaderColumn<
1415
>;
1516

1617
export const initialTestPageTableHeaderCols: MyTableHeaderColumn[] = [
17-
{ id: "ip", label: "IP", width: 60, sort: "default" },
18+
{ id: "ip", label: I18n.t("general.ip"), width: 60, sort: "default" },
1819
{
1920
id: "packetLossRate",
20-
label: "Loss Rate",
21+
label: I18n.t("general.lossRate"),
2122
width: 40,
2223
sort: "default",
2324
// @ts-ignore
@@ -30,7 +31,7 @@ export const initialTestPageTableHeaderCols: MyTableHeaderColumn[] = [
3031
},
3132
{
3233
id: "respondTime",
33-
label: "Respond Time(ms)",
34+
label: I18n.t("general.respondTime"),
3435
width: 40,
3536
sort: "default",
3637
// @ts-ignore
@@ -54,7 +55,7 @@ export const initialTestPageTableHeaderCols: MyTableHeaderColumn[] = [
5455
},
5556
{
5657
id: "downloadSpeed",
57-
label: "Download Speed(MB/S)",
58+
label: I18n.t("general.downloadSpeed"),
5859
width: 40,
5960
sort: "default",
6061
// @ts-ignore

0 commit comments

Comments
 (0)