* 时间节点翻译
()
| 213 | * 时间节点翻译 |
| 214 | */ |
| 215 | function timeElement() { |
| 216 | if (!window.RelativeTimeElement) { // 防止报错 |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | var RelativeTimeElement$getFormattedDate = RelativeTimeElement.prototype.getFormattedDate; |
| 221 | var TimeAgoElement$getFormattedDate = TimeAgoElement.prototype.getFormattedDate; |
| 222 | // var LocalTimeElement$getFormattedDate = LocalTimeElement.prototype.getFormattedDate; |
| 223 | |
| 224 | var RelativeTime = function (str, el) { // 相对时间解析 |
| 225 | if (/^on ([\w ]+)$/.test(str)) { |
| 226 | return '于 ' + el.title.replace(/ .+$/, ''); |
| 227 | } |
| 228 | |
| 229 | return str.replace(/just now|(an?|\d+) (second|minute|hour|day|month|year)s? ago/, function (m, d, t) { |
| 230 | if (m === 'just now') { |
| 231 | return '刚刚'; |
| 232 | } |
| 233 | |
| 234 | if (d[0] === 'a') { |
| 235 | d = '1'; |
| 236 | } // a, an 修改为 1 |
| 237 | |
| 238 | var dt = {second: '秒', minute: '分钟', hour: '小时', day: '天', month: '个月', year: '年'}; |
| 239 | |
| 240 | return d + ' ' + dt[t] + '之前'; |
| 241 | }); |
| 242 | }; |
| 243 | |
| 244 | RelativeTimeElement.prototype.getFormattedDate = function () { |
| 245 | var str = RelativeTimeElement$getFormattedDate.call(this); |
| 246 | return RelativeTime(str, this); |
| 247 | }; |
| 248 | |
| 249 | TimeAgoElement.prototype.getFormattedDate = function () { |
| 250 | var str = TimeAgoElement$getFormattedDate.call(this); |
| 251 | return RelativeTime(str, this); |
| 252 | }; |
| 253 | |
| 254 | LocalTimeElement.prototype.getFormattedDate = function () { |
| 255 | return this.title.replace(/ .+$/, ''); |
| 256 | }; |
| 257 | |
| 258 | // 遍历 time 元素进行翻译 |
| 259 | // 2016-04-16 github 改版,不再用 time 标签了。 |
| 260 | $('time, relative-time, time-ago, local-time').each(function (i, el) { |
| 261 | if (el.getFormattedDate) { // 跳过未注册的 time 元素 |
| 262 | el.textContent = el.getFormattedDate(); |
| 263 | } |
| 264 | }); |
| 265 | } |
| 266 | |
| 267 | |
| 268 | /** |