Skip to content

Commit de8a7e1

Browse files
committed
feat: i18n use timeAgo
1 parent a973ecc commit de8a7e1

File tree

3 files changed

+100
-4
lines changed

3 files changed

+100
-4
lines changed

src/components/DataTable.vue

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
<td
5757
class="whitespace-nowrap text-ellipsis break-all overflow-hidden px-3 py-2 text-xs text-$ud-text-secondary"
5858
>
59-
{{ formatTimeAgo(new Date(item.code_updated_at)) }}
59+
{{ formatTimeAgoWithI18n(new Date(item.code_updated_at)) }}
6060
</td>
6161
<td
6262
class="relative whitespace-nowrap py-2 pl-3 pr-4 text-right text-xs font-medium"
@@ -119,7 +119,7 @@
119119
</template>
120120

121121
<script setup lang="ts">
122-
import { formatTimeAgo } from '@vueuse/core';
122+
import { formatTimeAgo, UseTimeAgoUnitNamesDefault } from '@vueuse/core';
123123
import { GreasyforkScript } from '../composables/data';
124124
125125
const props = defineProps<{ data: GreasyforkScript[] }>();
@@ -135,4 +135,42 @@
135135
};
136136
137137
const { t } = useI18n();
138+
139+
const formatTimeAgoWithI18n = (from: Date) => {
140+
return formatTimeAgo<UseTimeAgoUnitNamesDefault>(from, {
141+
messages: {
142+
justNow: t('time-ago.just-now'),
143+
past: (n) => (n.match(/\d/) ? t('time-ago.past', { n }) : n),
144+
future: (n) => (n.match(/\d/) ? t('time-ago.future', { n }) : n),
145+
month: (n, past) =>
146+
n === 1
147+
? past
148+
? t('time-ago.month.past')
149+
: t('time-ago.month.future')
150+
: t('time-ago.month.n', { n }),
151+
year: (n, past) =>
152+
n === 1
153+
? past
154+
? t('time-ago.year.past')
155+
: t('time-ago.year.future')
156+
: t('time-ago.year.n', { n }),
157+
day: (n, past) =>
158+
n === 1
159+
? past
160+
? t('time-ago.day.past')
161+
: t('time-ago.day.future')
162+
: t('time-ago.day.n', { n }),
163+
week: (n, past) =>
164+
n === 1
165+
? past
166+
? t('time-ago.week.past')
167+
: t('time-ago.week.future')
168+
: t('time-ago.week.n', { n }),
169+
hour: (n) => t('time-ago.hour', { n }),
170+
minute: (n) => t('time-ago.minute', { n }),
171+
second: (n) => t('time-ago.second', { n }),
172+
invalid: 'invalid'
173+
}
174+
});
175+
};
138176
</script>

src/locales/cn.json

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,34 @@
1717
"language": "语言",
1818
"language-desc": "切换用户语言设置",
1919
"enable": "本页启用",
20-
"enable-desc": "控制本页面是否启用功能(会话)"
20+
"enable-desc": "控制本页面是否启用功能(会话)",
21+
"time-ago": {
22+
"just-now": "就在刚刚",
23+
"past": "{n}前",
24+
"future": "{n}后",
25+
"month": {
26+
"past": "上个月",
27+
"future": "下个月",
28+
"n": "{n}个月"
29+
},
30+
"year": {
31+
"past": "去年",
32+
"future": "明年",
33+
"n": "{n}年"
34+
},
35+
"day": {
36+
"past": "昨天",
37+
"future": "明天",
38+
"n": "{n}天"
39+
},
40+
"week": {
41+
"past": "上周",
42+
"future": "下周",
43+
"n": "{n}周"
44+
},
45+
"hour": "{n}小时",
46+
"minute": "{n}分钟",
47+
"second": "{n}秒",
48+
"invalid": "无效"
49+
}
2150
}

src/locales/en.json

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,34 @@
1717
"language": "Language",
1818
"language-desc": "Switch language",
1919
"enable": "Enable on this page",
20-
"enable-desc": "To enable this plugin on this page or not (Session)"
20+
"enable-desc": "To enable this plugin on this page or not (Session)",
21+
"time-ago": {
22+
"just-now": "just now",
23+
"past": "{n} ago",
24+
"future": "in {n}",
25+
"month": {
26+
"past": "last month",
27+
"future": "next month",
28+
"n": "{n} month | {n} months"
29+
},
30+
"year": {
31+
"past": "last year",
32+
"future": "next year",
33+
"n": "{n} year | {n} years"
34+
},
35+
"day": {
36+
"past": "yesterday",
37+
"future": "tomorrow",
38+
"n": "{n} day | {n} days"
39+
},
40+
"week": {
41+
"past": "last week",
42+
"future": "next week",
43+
"n": "{n} week | {n} weeks"
44+
},
45+
"hour": "{n} hour |{n} hours",
46+
"minute": "{n} minute |{n} minutes",
47+
"second": "{n} second |{n} seconds",
48+
"invalid": ""
49+
}
2150
}

0 commit comments

Comments
 (0)