Browse Source

update: comment组件加载语言包优先级

node 6 years ago
parent
commit
216e7c9b37
4 changed files with 111 additions and 15 deletions
  1. 1
    0
      package.json
  2. 68
    15
      src/App.js
  3. 21
    0
      src/lang/en-US.json
  4. 21
    0
      src/lang/zh-CN.json

+ 1
- 0
package.json View File

@@ -20,6 +20,7 @@
20 20
     "prop-types": "^15.6.2",
21 21
     "react": "^16.4.1",
22 22
     "react-dom": "^16.4.1",
23
+    "react-intl-universal": "^1.15.2",
23 24
     "react-scripts": "1.1.4",
24 25
     "shortid": "^2.2.11"
25 26
   },

+ 68
- 15
src/App.js View File

@@ -2,6 +2,7 @@ import React, { Component } from "react";
2 2
 import PropTypes from "prop-types";
3 3
 import { message } from "antd";
4 4
 import axios from "axios";
5
+import intl from "react-intl-universal";
5 6
 import { ERROR_DEFAULT, LIMIT } from "./constant";
6 7
 import { CommentContext } from "./Comment";
7 8
 import { isFunction } from "./helper";
@@ -13,6 +14,20 @@ import lang from "./lang";
13 14
 import "./App.css";
14 15
 // import styles from "./App.module.css";
15 16
 
17
+/**
18
+ * 当前支持的语言
19
+ */
20
+const SUPPORT_LOCALES = [
21
+  {
22
+    name: "English",
23
+    value: "en-US"
24
+  },
25
+  {
26
+    name: "简体中文",
27
+    value: "zh-CN"
28
+  }
29
+];
30
+
16 31
 class App extends Component {
17 32
   constructor(props) {
18 33
     super(props);
@@ -27,7 +42,8 @@ class App extends Component {
27 42
       page: 1,
28 43
       total: 0,
29 44
       // 是否没有更多评论了
30
-      isNoMoreComment: false
45
+      isNoMoreComment: false,
46
+      initDone: false
31 47
     };
32 48
     this.handleChangeLoading = this.handleChangeLoading.bind(this);
33 49
     this.sCreateComment = this.sCreateComment.bind(this);
@@ -52,7 +68,41 @@ class App extends Component {
52 68
     }
53 69
   }
54 70
 
55
-  componentDidMount() {}
71
+  componentDidMount() {
72
+    this.loadLocales();
73
+  }
74
+
75
+  /**
76
+   * 加载语言包
77
+   * 只能根据url或者传入的props来确定加载哪个语言包
78
+   * 优先级:传入的props > url
79
+   */
80
+  loadLocales() {
81
+    let { currentLocale } = this.props;
82
+    if (!currentLocale) {
83
+      currentLocale = intl.determineLocale({
84
+        urlLocaleKey: "lang"
85
+      });
86
+    }
87
+    console.log("currentLocale", currentLocale);
88
+    currentLocale = SUPPORT_LOCALES.find(item => item.value === currentLocale)
89
+      ? currentLocale
90
+      : "zh-CN";
91
+    this.axios
92
+      .get(`locales/${currentLocale}.json`)
93
+      .then(res => {
94
+        console.log("App locale data", res.data);
95
+        return intl.init({
96
+          currentLocale,
97
+          locales: {
98
+            [currentLocale]: res.data
99
+          }
100
+        });
101
+      })
102
+      .then(() => {
103
+        this.setState({ initDone: true });
104
+      });
105
+  }
56 106
 
57 107
   error(msg, info = {}) {
58 108
     if (this.props.showError) {
@@ -410,18 +460,20 @@ class App extends Component {
410 460
     };
411 461
 
412 462
     return (
413
-      <CommentContext.Provider value={value}>
414
-        <div className="comment">
415
-          {this.props.showEditor && (
416
-            <CommentInput content={this.props.children} />
417
-          )}
418
-          {this.props.showList && (
419
-            <div style={{ marginTop: 20 }}>
420
-              <CommentList />
421
-            </div>
422
-          )}
423
-        </div>
424
-      </CommentContext.Provider>
463
+      this.state.initDone && (
464
+        <CommentContext.Provider value={value}>
465
+          <div className="comment">
466
+            {this.props.showEditor && (
467
+              <CommentInput content={this.props.children} />
468
+            )}
469
+            {this.props.showList && (
470
+              <div style={{ marginTop: 20 }}>
471
+                <CommentList />
472
+              </div>
473
+            )}
474
+          </div>
475
+        </CommentContext.Provider>
476
+      )
425 477
     );
426 478
   }
427 479
 }
@@ -440,7 +492,8 @@ App.propTypes = {
440 492
   userId: PropTypes.number, // 用户id, comment内部不维护用户id, 调用组件时传递过来, 目前用于判断是否显示删除按钮
441 493
   pageType: PropTypes.string, // 分页类型
442 494
   page: PropTypes.number, // 页码
443
-  onPageChange: PropTypes.func // 页码变化回调
495
+  onPageChange: PropTypes.func, // 页码变化回调
496
+  currentLocale: PropTypes.string //  传入的语言包
444 497
 };
445 498
 
446 499
 App.defaultProps = {

+ 21
- 0
src/lang/en-US.json View File

@@ -0,0 +1,21 @@
1
+{
2
+  "editor.alreadyEntered": "已输入{count}/140字",
3
+  "editor.placeholder": "say...",
4
+  "editor.SubmitBtn": "Submit",
5
+  "editor.uploadTip": "上传图片",
6
+  "editor.uploadCount": "(您还能上传{count}张图片)",
7
+  "editor.uploadBtn": "upload",
8
+
9
+  "comment.totalComment": "共{total}条评论",
10
+  "comment.reply": "reply",
11
+  "comment.moreComment": "More",
12
+
13
+  "reply.totalReply": "{total}条回复",
14
+  "reply.moreReply": "More",
15
+  "reply.collapse": "收起回复",
16
+
17
+  "popConfirm.title": "Sure?",
18
+  "popConfirm.ok": "Ok",
19
+  "popConfirm.cancel": "Cancel",
20
+  "popConfirm.delete": "Delete"
21
+}

+ 21
- 0
src/lang/zh-CN.json View File

@@ -0,0 +1,21 @@
1
+{
2
+  "editor.alreadyEntered": "已输入{count}/140字",
3
+  "editor.placeholder": "说点什么吧...",
4
+  "editor.SubmitBtn": "发表",
5
+  "editor.uploadTip": "上传图片",
6
+  "editor.uploadCount": "(您还能上传{count}张图片)",
7
+  "editor.uploadBtn": "上传",
8
+
9
+  "comment.totalComment": "共{total}条评论",
10
+  "comment.reply": "回复",
11
+  "comment.moreComment": "查看更多评论",
12
+
13
+  "reply.totalReply": "{total}条回复",
14
+  "reply.moreReply": "查看更多回复",
15
+  "reply.collapse": "收起回复",
16
+
17
+  "popConfirm.title": "确定要删除吗",
18
+  "popConfirm.ok": "确定",
19
+  "popConfirm.cancel": "取消",
20
+  "popConfirm.delete": "删除"
21
+}