Browse Source

fix: 完成dayjs国际化

node 6 years ago
parent
commit
0833e15db1
2 changed files with 14 additions and 4 deletions
  1. 3
    2
      src/App.js
  2. 11
    2
      src/components/ContentItem/index.js

+ 3
- 2
src/App.js View File

@@ -50,7 +50,8 @@ class App extends Component {
50 50
       total: 0,
51 51
       // 是否没有更多评论了
52 52
       isNoMoreComment: false,
53
-      initDone: false
53
+      initDone: false,
54
+      locale: "zh-CN"
54 55
     };
55 56
     this.handleChangeLoading = this.handleChangeLoading.bind(this);
56 57
     this.sCreateComment = this.sCreateComment.bind(this);
@@ -104,7 +105,7 @@ class App extends Component {
104 105
         }
105 106
       })
106 107
       .then(() => {
107
-        this.setState({ initDone: true });
108
+        this.setState({ initDone: true, locale: currentLocale });
108 109
       });
109 110
   }
110 111
 

+ 11
- 2
src/components/ContentItem/index.js View File

@@ -17,6 +17,10 @@ import ImagePreviewer from "../ImagePreviewer/ImagePreviewer";
17 17
 // dayjs.locale("zh-cn");
18 18
 dayjs.extend(relativeTime);
19 19
 
20
+const LOCALES = {
21
+  "zh-CN": "zh-cn"
22
+};
23
+
20 24
 class CommentItem extends Component {
21 25
   constructor(props) {
22 26
     super(props);
@@ -80,6 +84,8 @@ class CommentItem extends Component {
80 84
       app
81 85
     } = this.props;
82 86
 
87
+    const { locale } = this.props.app;
88
+    console.log("locale is", locale);
83 89
     const { showInput } = this.state;
84 90
 
85 91
     let newContent = content.content;
@@ -125,8 +131,11 @@ class CommentItem extends Component {
125 131
                   "YYYY-MM-DD HH:mm:ss"
126 132
                 )}
127 133
               >
128
-                {dayjs(content.created * 1000).fromNow()}
129
-                {/* {dayjs(content.created * 1000).locale('zh-cn').fromNow()} */}
134
+                {LOCALES[locale]
135
+                  ? dayjs(content.created * 1000)
136
+                      .locale(LOCALES[locale])
137
+                      .fromNow()
138
+                  : dayjs(content.created * 1000).fromNow()}
130 139
               </Tooltip>
131 140
             </span>
132 141
           </div>