Commit 8cebd97f7376a3f0235a1315870345ff9dc22140
1 parent
9c67b23f
make Time Component to internationalize
Showing
5 changed files
with
40 additions
and
11 deletions
Show diff stats
src/components/time/time.js
| ... | ... | @@ -47,7 +47,7 @@ const getDate = (timeStamp, startType) => { |
| 47 | 47 | * @param {String|Number} timeStamp 时间戳 |
| 48 | 48 | * @returns {String} 相对时间字符串 |
| 49 | 49 | */ |
| 50 | -export const getRelativeTime = timeStamp => { | |
| 50 | +export const getRelativeTime = (timeStamp, locale) => { | |
| 51 | 51 | // 判断当前传入的时间戳是秒格式还是毫秒 |
| 52 | 52 | // const IS_MILLISECOND = true; |
| 53 | 53 | // 如果是毫秒格式则转为秒格式 |
| ... | ... | @@ -65,23 +65,23 @@ export const getRelativeTime = timeStamp => { |
| 65 | 65 | // 如果IS_EARLY为false则差值取反 |
| 66 | 66 | if (!IS_EARLY) diff = -diff; |
| 67 | 67 | let resStr = ''; |
| 68 | - const dirStr = IS_EARLY ? '前' : '后'; | |
| 68 | + let dirStr = IS_EARLY ? (locale('i.time.before') || '前') : (locale('i.time.after') || '后'); | |
| 69 | 69 | |
| 70 | - if (diff < 1000) resStr = '刚刚'; | |
| 70 | + if (diff < 1000) resStr = locale('i.time.just') || '刚刚'; | |
| 71 | 71 | // 少于等于59秒 |
| 72 | - else if (diff < 60000) resStr = parseInt(diff / 1000) + '秒' + dirStr; | |
| 72 | + else if (diff < 60000) resStr = parseInt(diff / 1000) + (locale('i.time.seconds') || '秒') + dirStr; | |
| 73 | 73 | // 多于59秒,少于等于59分钟59秒 |
| 74 | - else if (diff >= 60000 && diff < 3600000) resStr = Math.floor(diff / 60000) + '分钟' + dirStr; | |
| 74 | + else if (diff >= 60000 && diff < 3600000) resStr = Math.floor(diff / 60000) + (locale('i.time.minutes') || '分钟') + dirStr; | |
| 75 | 75 | // 多于59分钟59秒,少于等于23小时59分钟59秒 |
| 76 | - else if (diff >= 3600000 && diff < 86400000) resStr = Math.floor(diff / 3600000) + '小时' + dirStr; | |
| 76 | + else if (diff >= 3600000 && diff < 86400000) resStr = Math.floor(diff / 3600000) + (locale('i.time.hours') || '小时') + dirStr; | |
| 77 | 77 | // 多于23小时59分钟59秒,少于等于29天59分钟59秒 |
| 78 | - else if (diff >= 86400000 && diff < 2623860000) resStr = Math.floor(diff / 86400000) + '天' + dirStr; | |
| 78 | + else if (diff >= 86400000 && diff < 2623860000) resStr = Math.floor(diff / 86400000) + (locale('i.time.days') || '天') + dirStr; | |
| 79 | 79 | // 多于29天59分钟59秒,少于364天23小时59分钟59秒,且传入的时间戳早于当前 |
| 80 | 80 | else if (diff >= 2623860000 && diff <= 31567860000 && IS_EARLY) resStr = getDate(timeStamp); |
| 81 | 81 | else resStr = getDate(timeStamp, 'year'); |
| 82 | 82 | return resStr; |
| 83 | 83 | }; |
| 84 | 84 | |
| 85 | -export default function (timestamp) { | |
| 86 | - return getRelativeTime(timestamp); | |
| 87 | -} | |
| 88 | 85 | \ No newline at end of file |
| 86 | +export default function (timestamp, locale) { | |
| 87 | + return getRelativeTime(timestamp, locale); | |
| 88 | +} | ... | ... |
src/components/time/time.vue
| ... | ... | @@ -5,12 +5,14 @@ |
| 5 | 5 | import Vue from 'vue'; |
| 6 | 6 | const isServer = Vue.prototype.$isServer; |
| 7 | 7 | import { oneOf } from '../../utils/assist'; |
| 8 | + import Locale from '../../mixins/locale'; | |
| 8 | 9 | import Time from './time'; |
| 9 | 10 | |
| 10 | 11 | const prefixCls = 'ivu-time'; |
| 11 | 12 | |
| 12 | 13 | export default { |
| 13 | 14 | name: 'Time', |
| 15 | + mixins: [Locale], | |
| 14 | 16 | props: { |
| 15 | 17 | time: { |
| 16 | 18 | type: [Number, Date, String], |
| ... | ... | @@ -65,7 +67,7 @@ |
| 65 | 67 | } |
| 66 | 68 | |
| 67 | 69 | if (this.type === 'relative') { |
| 68 | - this.date = Time(time); | |
| 70 | + this.date = Time(time, this.t); | |
| 69 | 71 | } else { |
| 70 | 72 | const date = new Date(this.time); |
| 71 | 73 | const year = date.getFullYear(); | ... | ... |
src/locale/lang/en-US.js
| ... | ... | @@ -94,6 +94,15 @@ const lang = { |
| 94 | 94 | star: 'Star', |
| 95 | 95 | stars: 'Stars' |
| 96 | 96 | }, |
| 97 | + time: { | |
| 98 | + before: ' ago', | |
| 99 | + after: ' after', | |
| 100 | + just: 'just now', | |
| 101 | + seconds: ' seconds', | |
| 102 | + minutes: ' minutes', | |
| 103 | + hours: ' hours', | |
| 104 | + days: ' days' | |
| 105 | + }, | |
| 97 | 106 | tree: { |
| 98 | 107 | emptyText: 'No Data' |
| 99 | 108 | } | ... | ... |
src/locale/lang/ja-JP.js
src/locale/lang/zh-CN.js