mirror of
https://github.com/tenrok/vue2-datepicker.git
synced 2026-06-17 05:10:34 +03:00
fix: 语言切换的问题
This commit is contained in:
+5
-3
@@ -64,7 +64,7 @@
|
||||
|
||||
<script>
|
||||
import { isValidDate, isDateObejct } from '@/utils/index'
|
||||
import { t } from '@/locale/index'
|
||||
import locale from '@/mixins/locale'
|
||||
import scrollIntoView from '@/utils/scroll-into-view'
|
||||
import PanelDate from '@/panel/date'
|
||||
import PanelYear from '@/panel/year'
|
||||
@@ -74,6 +74,7 @@ import PanelTime from '@/panel/time'
|
||||
export default {
|
||||
name: 'CalendarPanel',
|
||||
components: { PanelDate, PanelYear, PanelMonth, PanelTime },
|
||||
mixins: [locale],
|
||||
props: {
|
||||
value: {
|
||||
default: null,
|
||||
@@ -133,11 +134,9 @@ export default {
|
||||
const calendarYear = now.getFullYear()
|
||||
const calendarMonth = now.getMonth()
|
||||
const firstYear = Math.floor(calendarYear / 10) * 10
|
||||
const months = t('months')
|
||||
return {
|
||||
panel: 'DATE',
|
||||
dates: [],
|
||||
months,
|
||||
calendarMonth,
|
||||
calendarYear,
|
||||
firstYear
|
||||
@@ -156,6 +155,9 @@ export default {
|
||||
},
|
||||
timeHeader () {
|
||||
return this.value && new Date(this.value).toLocaleDateString()
|
||||
},
|
||||
months () {
|
||||
return this.t('months')
|
||||
}
|
||||
},
|
||||
watch: {
|
||||
|
||||
+12
-7
@@ -98,13 +98,15 @@
|
||||
<script>
|
||||
import fecha from 'fecha'
|
||||
import clickoutside from '@/directives/clickoutside'
|
||||
import { isValidDate, isValidRange, isDateObejct } from '@/utils/index'
|
||||
import { use, t } from '@/locale/index'
|
||||
import { isValidDate, isValidRange, isDateObejct, isPlainObject } from '@/utils/index'
|
||||
import CalendarPanel from './calendar.vue'
|
||||
import locale from '@/mixins/locale'
|
||||
import Languages from '@/locale/languages'
|
||||
|
||||
export default {
|
||||
name: 'DatePicker',
|
||||
components: { CalendarPanel },
|
||||
mixins: [locale],
|
||||
directives: {
|
||||
clickoutside
|
||||
},
|
||||
@@ -189,11 +191,17 @@ export default {
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
language () {
|
||||
if (isPlainObject(this.lang)) {
|
||||
return { ...Languages.en, ...this.lang }
|
||||
}
|
||||
return Languages[this.lang] || Languages.en
|
||||
},
|
||||
innerPlaceholder () {
|
||||
if (typeof this.placeholder === 'string') {
|
||||
return this.placeholder
|
||||
}
|
||||
return this.range ? t('placeholder.dateRange') : t('placeholder.date')
|
||||
return this.range ? this.t('placeholder.dateRange') : this.t('placeholder.date')
|
||||
},
|
||||
text () {
|
||||
if (this.userInput !== null) {
|
||||
@@ -222,7 +230,7 @@ export default {
|
||||
if (this.shortcuts === false) {
|
||||
return []
|
||||
}
|
||||
const pickers = t('pickers')
|
||||
const pickers = this.t('pickers')
|
||||
const arr = [
|
||||
{
|
||||
text: pickers[0],
|
||||
@@ -248,9 +256,6 @@ export default {
|
||||
return arr
|
||||
}
|
||||
},
|
||||
created () {
|
||||
use(this.lang)
|
||||
},
|
||||
methods: {
|
||||
initCalendar () {
|
||||
this.handleValueChange(this.value)
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
import Languages from './languages'
|
||||
import { isPlainObject } from '@/utils/index'
|
||||
|
||||
let lang = 'zh'
|
||||
|
||||
export function use (target) {
|
||||
if (isPlainObject(target)) {
|
||||
lang = { ...Languages.en, ...target }
|
||||
} else {
|
||||
lang = Languages[target] || Languages.en
|
||||
}
|
||||
}
|
||||
|
||||
export function t (path) {
|
||||
const arr = path.split('.')
|
||||
let current = lang
|
||||
let value
|
||||
for (let i = 0, len = arr.length; i < len; i++) {
|
||||
const prop = arr[i]
|
||||
value = current[prop]
|
||||
if (i === len - 1) {
|
||||
return value
|
||||
}
|
||||
if (!value) {
|
||||
return ''
|
||||
}
|
||||
current = value
|
||||
}
|
||||
return ''
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
|
||||
export default {
|
||||
methods: {
|
||||
t (path) {
|
||||
let component = this
|
||||
let name = component.$options.name
|
||||
while (component && (!name || name !== 'DatePicker')) {
|
||||
component = component.$parent
|
||||
if (component) {
|
||||
name = component.$options.name
|
||||
}
|
||||
}
|
||||
if (component && component.language) {
|
||||
const arr = path.split('.')
|
||||
let current = component.language
|
||||
let value
|
||||
for (let i = 0, len = arr.length; i < len; i++) {
|
||||
const prop = arr[i]
|
||||
value = current[prop]
|
||||
if (i === len - 1) {
|
||||
return value
|
||||
}
|
||||
if (!value) {
|
||||
return ''
|
||||
}
|
||||
current = value
|
||||
}
|
||||
}
|
||||
return ''
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-2
@@ -1,7 +1,8 @@
|
||||
import { t } from '@/locale/index'
|
||||
import locale from '@/mixins/locale'
|
||||
|
||||
export default {
|
||||
name: 'panelDate',
|
||||
mixins: [locale],
|
||||
props: {
|
||||
value: null,
|
||||
startAt: null,
|
||||
@@ -33,7 +34,7 @@ export default {
|
||||
this.$emit('select', date)
|
||||
},
|
||||
getDays (firstDayOfWeek) {
|
||||
const days = t('days')
|
||||
const days = this.t('days')
|
||||
const firstday = parseInt(firstDayOfWeek, 10)
|
||||
return days.concat(days).slice(firstday, firstday + 7)
|
||||
},
|
||||
|
||||
+3
-2
@@ -1,7 +1,8 @@
|
||||
import { t } from '@/locale/index'
|
||||
import locale from '@/mixins/locale'
|
||||
|
||||
export default {
|
||||
name: 'panelMonth',
|
||||
mixins: [locale],
|
||||
props: {
|
||||
value: null,
|
||||
calendarYear: {
|
||||
@@ -14,7 +15,7 @@ export default {
|
||||
}
|
||||
},
|
||||
render (h) {
|
||||
let months = t('months')
|
||||
let months = this.t('months')
|
||||
const currentYear = this.value && new Date(this.value).getFullYear()
|
||||
const currentMonth = this.value && new Date(this.value).getMonth()
|
||||
months = months.map((v, i) => {
|
||||
|
||||
Reference in New Issue
Block a user