Browse Source

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

node 6 years ago
parent
commit
c7c08d8558
3 changed files with 68 additions and 13 deletions
  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 View File

10
 import CommentList from "./components/CommentList";
10
 import CommentList from "./components/CommentList";
11
 import Editor from "./components/Editor";
11
 import Editor from "./components/Editor";
12
 import RenderText from "./components/RenderText";
12
 import RenderText from "./components/RenderText";
13
-import lang from "./lang";
13
+// import lang from "./lang";
14
 import USdata from "./lang/en-US.js";
14
 import USdata from "./lang/en-US.js";
15
 import CNdata from "./lang/zh-CN.js";
15
 import CNdata from "./lang/zh-CN.js";
16
 import "./App.css";
16
 import "./App.css";
35
   "en-US": USdata
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
 class App extends Component {
65
 class App extends Component {
39
   constructor(props) {
66
   constructor(props) {
40
     super(props);
67
     super(props);
119
   }
146
   }
120
 
147
 
121
   errorHandler(error) {
148
   errorHandler(error) {
149
+    const { locale } = this.state;
150
+    const localResponse = LOCALES_RESPONSE[locale];
122
     if (error.response && error.response.data && error.response.data.msg) {
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
         response: error.response
153
         response: error.response
125
       });
154
       });
126
       return;
155
       return;
127
     }
156
     }
128
-    this.error(lang[error.message] || ERROR_DEFAULT, {
157
+    this.error(localResponse[error.response.data.msg] || ERROR_DEFAULT, {
129
       response: error.response
158
       response: error.response
130
     });
159
     });
131
   }
160
   }
174
             total
203
             total
175
           });
204
           });
176
         } else {
205
         } else {
177
-          message.info("没有更多评论了");
206
+          message.info(intl.get("message.noMoreComment"));
178
           this.setState({
207
           this.setState({
179
             isNoMoreComment: true
208
             isNoMoreComment: true
180
           });
209
           });
196
       .get(`${API}/replies?comment_id=${commentId}&page=${page}&limit=${LIMIT}`)
225
       .get(`${API}/replies?comment_id=${commentId}&page=${page}&limit=${LIMIT}`)
197
       .then(response => {
226
       .then(response => {
198
         if (!response.data.list) {
227
         if (!response.data.list) {
199
-          message.info("没有更多数据了!");
228
+          message.info(intl.get("message.noMoreData"));
200
         }
229
         }
201
         const list = this.state.list.map(item => {
230
         const list = this.state.list.map(item => {
202
           if (item.id === commentId) {
231
           if (item.id === commentId) {
233
    * @param {object} {content} comment content
262
    * @param {object} {content} comment content
234
    */
263
    */
235
   sCreateComment({ content } = {}, cb) {
264
   sCreateComment({ content } = {}, cb) {
236
-    if (!content) return this.error("评论内容不能为空 ");
265
+    if (!content) return this.error(intl.get("message.notNull"));
237
     this.handleChangeLoading("sCreateComment", true);
266
     this.handleChangeLoading("sCreateComment", true);
238
     const { API, type, businessId } = this.props;
267
     const { API, type, businessId } = this.props;
239
     this.axios(`${API}/comments`, {
268
     this.axios(`${API}/comments`, {
247
     })
276
     })
248
       .then(response => {
277
       .then(response => {
249
         if (this.props.showAlertComment) {
278
         if (this.props.showAlertComment) {
250
-          message.success("评论成功!");
279
+          message.success(intl.get("message.success"));
251
         }
280
         }
252
         if (isFunction(cb)) cb();
281
         if (isFunction(cb)) cb();
253
         // 将数据写入到 list 中
282
         // 将数据写入到 list 中
293
    * @param {object} data { comment_id, content, [reply_id] }
322
    * @param {object} data { comment_id, content, [reply_id] }
294
    */
323
    */
295
   sCreateReply(data, cb) {
324
   sCreateReply(data, cb) {
296
-    if (!data.content) return this.error("回复内容不能为空 ");
325
+    if (!data.content) return this.error(intl.get("message.replyNoNull"));
297
     this.handleChangeLoading("sCreateReply", true);
326
     this.handleChangeLoading("sCreateReply", true);
298
     const { API } = this.props;
327
     const { API } = this.props;
299
     this.axios(`${API}/replies`, {
328
     this.axios(`${API}/replies`, {
303
     })
332
     })
304
       .then(response => {
333
       .then(response => {
305
         if (this.props.showAlertReply) {
334
         if (this.props.showAlertReply) {
306
-          message.success("回复成功!");
335
+          message.success(intl.get("message.replySuccess"));
307
         }
336
         }
308
         if (isFunction(cb)) cb();
337
         if (isFunction(cb)) cb();
309
         // 将数据写入到 list 中
338
         // 将数据写入到 list 中
371
     })
400
     })
372
       .then(response => {
401
       .then(response => {
373
         if (this.props.showAlertFavor) {
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
         // 更新 list 中的该项数据的 favored
409
         // 更新 list 中的该项数据的 favored
377
         const list = this.state.list.map(item => {
410
         const list = this.state.list.map(item => {
406
       withCredentials: true
439
       withCredentials: true
407
     })
440
     })
408
       .then(response => {
441
       .then(response => {
409
-        message.success(favored ? "取消点赞成功!" : "点赞成功!");
442
+        message.success(
443
+          favored
444
+            ? intl.get("message.cancelLickSuccess")
445
+            : intl.get("message.likeSuccess")
446
+        );
410
         // 更新 list 中的该项数据的 favored
447
         // 更新 list 中的该项数据的 favored
411
         const list = this.state.list.map(item => {
448
         const list = this.state.list.map(item => {
412
           if (item.id === commentId) {
449
           if (item.id === commentId) {

+ 10
- 1
src/lang/en-US.js View File

23
   "popConfirm.title": "Sure?",
23
   "popConfirm.title": "Sure?",
24
   "popConfirm.ok": "Ok",
24
   "popConfirm.ok": "Ok",
25
   "popConfirm.cancel": "Cancel",
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
 export default USdata;
38
 export default USdata;

+ 10
- 1
src/lang/zh-CN.js View File

22
   "popConfirm.title": "确定要删除吗",
22
   "popConfirm.title": "确定要删除吗",
23
   "popConfirm.ok": "确定",
23
   "popConfirm.ok": "确定",
24
   "popConfirm.cancel": "取消",
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
 export default CNdata;
37
 export default CNdata;