refactor(locale): isolate datefns locale file

This commit is contained in:
07akioni 2020-02-26 23:25:05 +08:00
parent 86d487c5e1
commit fe23dfa726
4 changed files with 28 additions and 8 deletions

View File

@ -1,8 +1,6 @@
import format from 'date-fns/format'
import formatDistance from 'date-fns/formatDistance'
import fromUnixTime from 'date-fns/fromUnixTime'
import zhCN from 'date-fns/locale/zh-CN'
import enUS from 'date-fns/locale/en-US'
import render from '../../_utils/vue/render'
import locale from '../../_mixins/locale'
@ -41,12 +39,6 @@ export default {
}
},
computed: {
dateFnsLocale () {
return ({
'zh-CN': zhCN,
'en-US': enUS
})[this.locale]
},
dateFnsOptions () {
return {
locale: this.dateFnsLocale

View File

@ -16,6 +16,15 @@ function getTranslationNamespace (locales, fallbackLocale, language, namespace)
}
}
function getDateFns (locales, fallbackLocale, language) {
const locale = locales[language]
if (locale) {
return locale._dateFns
} else {
return fallbackLocale._dateFns
}
}
export default function createLocaleMixin (componentLocaleNamespace) {
return {
inject: {
@ -31,6 +40,17 @@ export default function createLocaleMixin (componentLocaleNamespace) {
return this.$naive.fallbackLocale
}
},
dateFnsLocale () {
let language = null
if (this.NConfigProvider) {
language = this.NConfigProvider.inheritedLanguage
}
return getDateFns(
this.$naive.locales,
this.$naive.fallbackLocale,
language
)
},
localeNamespace () {
return this.tns(componentLocaleNamespace)
}

View File

@ -1,5 +1,8 @@
import enUS from 'date-fns/locale/en-US'
export default {
_name: 'en-US',
_dateFns: enUS,
Popconfirm: {
positiveText: 'Confirm',
negativeText: 'Cancel'
@ -28,6 +31,7 @@ export default {
Thu: 'Thu',
Fri: 'Fri',
Sat: 'Sat',
seperator: 'to',
clear: 'Clear',
now: 'Now',
confirm: 'Confirm',

View File

@ -1,5 +1,8 @@
import zhCN from 'date-fns/locale/zh-CN'
export default {
_name: 'zh-CN',
_dateFns: zhCN,
Popconfirm: {
positiveText: '确认',
negativeText: '取消'
@ -28,6 +31,7 @@ export default {
Thu: '四',
Fri: '五',
Sat: '六',
seperator: '至',
clear: '清除',
now: '此刻',
confirm: '确认',