Bläddra i källkod

fix: 完成dayjs国际化

node 5 år sedan
förälder
incheckning
0833e15db1
2 ändrade filer med 14 tillägg och 4 borttagningar
  1. 3
    2
      src/App.js
  2. 11
    2
      src/components/ContentItem/index.js

+ 3
- 2
src/App.js Visa fil

50
       total: 0,
50
       total: 0,
51
       // 是否没有更多评论了
51
       // 是否没有更多评论了
52
       isNoMoreComment: false,
52
       isNoMoreComment: false,
53
-      initDone: false
53
+      initDone: false,
54
+      locale: "zh-CN"
54
     };
55
     };
55
     this.handleChangeLoading = this.handleChangeLoading.bind(this);
56
     this.handleChangeLoading = this.handleChangeLoading.bind(this);
56
     this.sCreateComment = this.sCreateComment.bind(this);
57
     this.sCreateComment = this.sCreateComment.bind(this);
104
         }
105
         }
105
       })
106
       })
106
       .then(() => {
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 Visa fil

17
 // dayjs.locale("zh-cn");
17
 // dayjs.locale("zh-cn");
18
 dayjs.extend(relativeTime);
18
 dayjs.extend(relativeTime);
19
 
19
 
20
+const LOCALES = {
21
+  "zh-CN": "zh-cn"
22
+};
23
+
20
 class CommentItem extends Component {
24
 class CommentItem extends Component {
21
   constructor(props) {
25
   constructor(props) {
22
     super(props);
26
     super(props);
80
       app
84
       app
81
     } = this.props;
85
     } = this.props;
82
 
86
 
87
+    const { locale } = this.props.app;
88
+    console.log("locale is", locale);
83
     const { showInput } = this.state;
89
     const { showInput } = this.state;
84
 
90
 
85
     let newContent = content.content;
91
     let newContent = content.content;
125
                   "YYYY-MM-DD HH:mm:ss"
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
               </Tooltip>
139
               </Tooltip>
131
             </span>
140
             </span>
132
           </div>
141
           </div>