Skip to content

Commit 2048bd0

Browse files
committed
dayjsのフォーマットをi18nと連動するようにする
1 parent 5528457 commit 2048bd0

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

nuxt-i18n.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@ const options = {
1515
code: 'ja',
1616
displayName: '日本語',
1717
file: 'ja.json',
18+
format: 'ja',
1819
},
1920
{
2021
code: 'en',
2122
displayName: 'English',
2223
file: 'en.json',
24+
format: 'en',
2325
},
2426
{
2527
code: 'zh-goyu',
2628
displayName: '台灣華語',
2729
file: 'zh_TW.json',
30+
format: 'zh-tw',
2831
},
2932
],
3033
}

nuxt.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export default {
128128
locales: ['ja', 'en', 'zh-tw'],
129129
defaultLocale: 'ja',
130130
defaultTimeZone: 'Asia/Tokyo',
131-
plugins: ['utc', 'timezone', 'minMax'],
131+
plugins: ['utc', 'timezone', 'minMax', 'localizedFormat'],
132132
},
133133
/*
134134
** Axios module configuration

src/components/AppLanguageSelector.vue

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,28 @@
1515
<script lang="ts">
1616
import Vue from 'vue'
1717
18-
type LocaleListItem = { text: string; value: string }
18+
type LocaleListItem = { text: string; value: string; format: string }
1919
type LocalData = {
2020
locales: LocaleListItem[]
2121
}
2222
2323
export default Vue.extend({
2424
data(): LocalData {
2525
if (!this.$root.$i18n.locales)
26-
return { locales: [{ text: 'N/A', value: '' }] }
26+
return { locales: [{ text: 'N/A', value: '', format: '' }] }
2727
2828
return {
2929
locales: this.$root.$i18n.locales.map((l) => {
30-
if (typeof l === 'string') return { text: l, value: l }
31-
else return { text: l.displayName, value: l.code }
30+
if (typeof l === 'string') return { text: l, value: l, format: l }
31+
else return { text: l.displayName, value: l.code, format: l.format }
3232
}),
3333
}
3434
},
35+
watch: {
36+
'$root.$i18n.locale'(locale) {
37+
this.$dayjs.locale(this.locales?.find((v) => v.value === locale)?.format)
38+
},
39+
},
3540
})
3641
</script>
3742

0 commit comments

Comments
 (0)