Kaynağa Gözat

add: 完成后端返回数据的国际化

node 6 yıl önce
ebeveyn
işleme
c7c08d8558
3 değiştirilmiş dosya ile 68 ekleme ve 13 silme
  1. 48
    11
      src/App.js
  2. 10
    1
      src/lang/en-US.js
  3. 10
    1
      src/lang/zh-CN.js

+ 48
- 11
src/App.js Dosyayı Görüntüle

@@ -10,7 +10,7 @@ import CommentInput from "./components/CommentInput";
10 10
 import CommentList from "./components/CommentList";
11 11
 import Editor from "./components/Editor";
12 12
 import RenderText from "./components/RenderText";
13
-import lang from "./lang";
13
+// import lang from "./lang";
14 14
 import USdata from "./lang/en-US.js";
15 15
 import CNdata from "./lang/zh-CN.js";
16 16
 import "./App.css";
@@ -35,6 +35,33 @@ const LOCALES = {
35 35
   "en-US": USdata
36 36
 };
37 37
 
38
+const LOCALES_RESPONSE = {
39
+  "zh-CN": {
40
+    "not found": "没有数据",
41
+    "auth failed": "请先登录",
42
+    "create comment failed": "创建评论失败",
43
+    "comment favor failed": "评论点赞失败",
44
+    "delete comment favor failed": "评论取消点赞失败",
45
+    "get comments failed": "获取评论列表失败",
46
+    "create reply failed": "创建回复失败",
47
+    "reply favor failed": "回复点赞失败",
48
+    "delete reply favor failed": "删除回复点赞失败",
49
+    "get replies failed": "获取回复列表失败"
50
+  },
51
+  "en-US": {
52
+    "not found": "no data",
53
+    "auth failed": "please log in first",
54
+    "create comment failed": "Failed to create comment",
55
+    "comment favor failed": "Comment likes failure",
56
+    "delete comment favor failed": "评论取消点赞失败",
57
+    "get comments failed": "Comment cancels praise failure",
58
+    "create reply failed": "Create reply failed",
59
+    "reply favor failed": "Reply to praise failed",
60
+    "delete reply favor failed": "Delete reply clicks failed",
61
+    "get replies failed": "Failed to get reply list"
62
+  }
63
+};
64
+
38 65
 class App extends Component {
39 66
   constructor(props) {
40 67
     super(props);
@@ -119,13 +146,15 @@ class App extends Component {
119 146
   }
120 147
 
121 148
   errorHandler(error) {
149
+    const { locale } = this.state;
150
+    const localResponse = LOCALES_RESPONSE[locale];
122 151
     if (error.response && error.response.data && error.response.data.msg) {
123
-      this.error(lang[error.response.data.msg] || ERROR_DEFAULT, {
152
+      this.error(localResponse[error.response.data.msg] || ERROR_DEFAULT, {
124 153
         response: error.response
125 154
       });
126 155
       return;
127 156
     }
128
-    this.error(lang[error.message] || ERROR_DEFAULT, {
157
+    this.error(localResponse[error.response.data.msg] || ERROR_DEFAULT, {
129 158
       response: error.response
130 159
     });
131 160
   }
@@ -174,7 +203,7 @@ class App extends Component {
174 203
             total
175 204
           });
176 205
         } else {
177
-          message.info("没有更多评论了");
206
+          message.info(intl.get("message.noMoreComment"));
178 207
           this.setState({
179 208
             isNoMoreComment: true
180 209
           });
@@ -196,7 +225,7 @@ class App extends Component {
196 225
       .get(`${API}/replies?comment_id=${commentId}&page=${page}&limit=${LIMIT}`)
197 226
       .then(response => {
198 227
         if (!response.data.list) {
199
-          message.info("没有更多数据了!");
228
+          message.info(intl.get("message.noMoreData"));
200 229
         }
201 230
         const list = this.state.list.map(item => {
202 231
           if (item.id === commentId) {
@@ -233,7 +262,7 @@ class App extends Component {
233 262
    * @param {object} {content} comment content
234 263
    */
235 264
   sCreateComment({ content } = {}, cb) {
236
-    if (!content) return this.error("评论内容不能为空 ");
265
+    if (!content) return this.error(intl.get("message.notNull"));
237 266
     this.handleChangeLoading("sCreateComment", true);
238 267
     const { API, type, businessId } = this.props;
239 268
     this.axios(`${API}/comments`, {
@@ -247,7 +276,7 @@ class App extends Component {
247 276
     })
248 277
       .then(response => {
249 278
         if (this.props.showAlertComment) {
250
-          message.success("评论成功!");
279
+          message.success(intl.get("message.success"));
251 280
         }
252 281
         if (isFunction(cb)) cb();
253 282
         // 将数据写入到 list 中
@@ -293,7 +322,7 @@ class App extends Component {
293 322
    * @param {object} data { comment_id, content, [reply_id] }
294 323
    */
295 324
   sCreateReply(data, cb) {
296
-    if (!data.content) return this.error("回复内容不能为空 ");
325
+    if (!data.content) return this.error(intl.get("message.replyNoNull"));
297 326
     this.handleChangeLoading("sCreateReply", true);
298 327
     const { API } = this.props;
299 328
     this.axios(`${API}/replies`, {
@@ -303,7 +332,7 @@ class App extends Component {
303 332
     })
304 333
       .then(response => {
305 334
         if (this.props.showAlertReply) {
306
-          message.success("回复成功!");
335
+          message.success(intl.get("message.replySuccess"));
307 336
         }
308 337
         if (isFunction(cb)) cb();
309 338
         // 将数据写入到 list 中
@@ -371,7 +400,11 @@ class App extends Component {
371 400
     })
372 401
       .then(response => {
373 402
         if (this.props.showAlertFavor) {
374
-          message.success(favored ? "取消点赞成功!" : "点赞成功!");
403
+          message.success(
404
+            favored
405
+              ? intl.get("message.cancelLickSuccess")
406
+              : intl.get("message.likeSuccess")
407
+          );
375 408
         }
376 409
         // 更新 list 中的该项数据的 favored
377 410
         const list = this.state.list.map(item => {
@@ -406,7 +439,11 @@ class App extends Component {
406 439
       withCredentials: true
407 440
     })
408 441
       .then(response => {
409
-        message.success(favored ? "取消点赞成功!" : "点赞成功!");
442
+        message.success(
443
+          favored
444
+            ? intl.get("message.cancelLickSuccess")
445
+            : intl.get("message.likeSuccess")
446
+        );
410 447
         // 更新 list 中的该项数据的 favored
411 448
         const list = this.state.list.map(item => {
412 449
           if (item.id === commentId) {

+ 10
- 1
src/lang/en-US.js Dosyayı Görüntüle

@@ -23,7 +23,16 @@ const USdata = {
23 23
   "popConfirm.title": "Sure?",
24 24
   "popConfirm.ok": "Ok",
25 25
   "popConfirm.cancel": "Cancel",
26
-  "popConfirm.delete": "Delete"
26
+  "popConfirm.delete": "Delete",
27
+
28
+  "message.noMoreComment": "no more comment",
29
+  "message.noMoreData": "no more data",
30
+  "message.notNull": "comment not null",
31
+  "message.success": "comment success",
32
+  "message.replyNoNull": "reply not null",
33
+  "message.replySuccess": "reply success",
34
+  "message.cancelLickSuccess": "cancel like success",
35
+  "message.likeSuccess": "like success"
27 36
 };
28 37
 
29 38
 export default USdata;

+ 10
- 1
src/lang/zh-CN.js Dosyayı Görüntüle

@@ -22,7 +22,16 @@ const CNdata = {
22 22
   "popConfirm.title": "确定要删除吗",
23 23
   "popConfirm.ok": "确定",
24 24
   "popConfirm.cancel": "取消",
25
-  "popConfirm.delete": "删除"
25
+  "popConfirm.delete": "删除",
26
+
27
+  "message.noMoreComment": "没有更多评论了",
28
+  "message.noMoreData": "没有更多数据了!",
29
+  "message.notNull": "评论内容不能为空",
30
+  "message.success": "评论成功!",
31
+  "message.replyNoNull": "回复内容不能为空",
32
+  "message.replySuccess": "回复成功!",
33
+  "message.cancelLickSuccess": "取消点赞成功!",
34
+  "message.likeSuccess": "点赞成功!"
26 35
 };
27 36
 
28 37
 export default CNdata;