|
@@ -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 = {
|