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 format from 'date-fns/format'
import formatDistance from 'date-fns/formatDistance' import formatDistance from 'date-fns/formatDistance'
import fromUnixTime from 'date-fns/fromUnixTime' 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 render from '../../_utils/vue/render'
import locale from '../../_mixins/locale' import locale from '../../_mixins/locale'
@ -41,12 +39,6 @@ export default {
} }
}, },
computed: { computed: {
dateFnsLocale () {
return ({
'zh-CN': zhCN,
'en-US': enUS
})[this.locale]
},
dateFnsOptions () { dateFnsOptions () {
return { return {
locale: this.dateFnsLocale 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) { export default function createLocaleMixin (componentLocaleNamespace) {
return { return {
inject: { inject: {
@ -31,6 +40,17 @@ export default function createLocaleMixin (componentLocaleNamespace) {
return this.$naive.fallbackLocale 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 () { localeNamespace () {
return this.tns(componentLocaleNamespace) return this.tns(componentLocaleNamespace)
} }

View File

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

View File

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