mirror of
https://github.com/tenrok/vue2-datepicker.git
synced 2026-06-23 16:50:36 +03:00
fix: 语言切换的问题
This commit is contained in:
@@ -25,6 +25,7 @@ module.exports = {
|
|||||||
'generator-star-spacing': 'off',
|
'generator-star-spacing': 'off',
|
||||||
// allow debugger during development
|
// allow debugger during development
|
||||||
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
|
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
|
||||||
|
'no-console': process.env.NODE_ENV === 'production' ? ["error", { allow: ["warn", "error"] }] : 'off'
|
||||||
'camelcase': ['off', { properties: 'never' }],
|
'camelcase': ['off', { properties: 'never' }],
|
||||||
// "vue/max-attributes-per-line": [2, {
|
// "vue/max-attributes-per-line": [2, {
|
||||||
// "singleline": 1,
|
// "singleline": 1,
|
||||||
|
|||||||
+1
-1
@@ -44,7 +44,7 @@ new Vue({ // eslint-disable-line
|
|||||||
render (h) {
|
render (h) {
|
||||||
const example1 = {
|
const example1 = {
|
||||||
'base': '<date-picker v-model="value1" lang="en" :not-before="new Date()"></date-picker>',
|
'base': '<date-picker v-model="value1" lang="en" :not-before="new Date()"></date-picker>',
|
||||||
'range': '<date-picker v-model="value2" range lang="en"></date-picker>'
|
'range': '<date-picker v-model="value2" range ></date-picker>'
|
||||||
}
|
}
|
||||||
const example2 = {
|
const example2 = {
|
||||||
'datetime': `
|
'datetime': `
|
||||||
|
|||||||
+5
-3
@@ -64,7 +64,7 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { isValidDate, isDateObejct } from '@/utils/index'
|
import { isValidDate, isDateObejct } from '@/utils/index'
|
||||||
import { t } from '@/locale/index'
|
import locale from '@/mixins/locale'
|
||||||
import scrollIntoView from '@/utils/scroll-into-view'
|
import scrollIntoView from '@/utils/scroll-into-view'
|
||||||
import PanelDate from '@/panel/date'
|
import PanelDate from '@/panel/date'
|
||||||
import PanelYear from '@/panel/year'
|
import PanelYear from '@/panel/year'
|
||||||
@@ -74,6 +74,7 @@ import PanelTime from '@/panel/time'
|
|||||||
export default {
|
export default {
|
||||||
name: 'CalendarPanel',
|
name: 'CalendarPanel',
|
||||||
components: { PanelDate, PanelYear, PanelMonth, PanelTime },
|
components: { PanelDate, PanelYear, PanelMonth, PanelTime },
|
||||||
|
mixins: [locale],
|
||||||
props: {
|
props: {
|
||||||
value: {
|
value: {
|
||||||
default: null,
|
default: null,
|
||||||
@@ -133,11 +134,9 @@ export default {
|
|||||||
const calendarYear = now.getFullYear()
|
const calendarYear = now.getFullYear()
|
||||||
const calendarMonth = now.getMonth()
|
const calendarMonth = now.getMonth()
|
||||||
const firstYear = Math.floor(calendarYear / 10) * 10
|
const firstYear = Math.floor(calendarYear / 10) * 10
|
||||||
const months = t('months')
|
|
||||||
return {
|
return {
|
||||||
panel: 'DATE',
|
panel: 'DATE',
|
||||||
dates: [],
|
dates: [],
|
||||||
months,
|
|
||||||
calendarMonth,
|
calendarMonth,
|
||||||
calendarYear,
|
calendarYear,
|
||||||
firstYear
|
firstYear
|
||||||
@@ -156,6 +155,9 @@ export default {
|
|||||||
},
|
},
|
||||||
timeHeader () {
|
timeHeader () {
|
||||||
return this.value && new Date(this.value).toLocaleDateString()
|
return this.value && new Date(this.value).toLocaleDateString()
|
||||||
|
},
|
||||||
|
months () {
|
||||||
|
return this.t('months')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
|
|||||||
+12
-7
@@ -98,13 +98,15 @@
|
|||||||
<script>
|
<script>
|
||||||
import fecha from 'fecha'
|
import fecha from 'fecha'
|
||||||
import clickoutside from '@/directives/clickoutside'
|
import clickoutside from '@/directives/clickoutside'
|
||||||
import { isValidDate, isValidRange, isDateObejct } from '@/utils/index'
|
import { isValidDate, isValidRange, isDateObejct, isPlainObject } from '@/utils/index'
|
||||||
import { use, t } from '@/locale/index'
|
|
||||||
import CalendarPanel from './calendar.vue'
|
import CalendarPanel from './calendar.vue'
|
||||||
|
import locale from '@/mixins/locale'
|
||||||
|
import Languages from '@/locale/languages'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'DatePicker',
|
name: 'DatePicker',
|
||||||
components: { CalendarPanel },
|
components: { CalendarPanel },
|
||||||
|
mixins: [locale],
|
||||||
directives: {
|
directives: {
|
||||||
clickoutside
|
clickoutside
|
||||||
},
|
},
|
||||||
@@ -189,11 +191,17 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
|
language () {
|
||||||
|
if (isPlainObject(this.lang)) {
|
||||||
|
return { ...Languages.en, ...this.lang }
|
||||||
|
}
|
||||||
|
return Languages[this.lang] || Languages.en
|
||||||
|
},
|
||||||
innerPlaceholder () {
|
innerPlaceholder () {
|
||||||
if (typeof this.placeholder === 'string') {
|
if (typeof this.placeholder === 'string') {
|
||||||
return this.placeholder
|
return this.placeholder
|
||||||
}
|
}
|
||||||
return this.range ? t('placeholder.dateRange') : t('placeholder.date')
|
return this.range ? this.t('placeholder.dateRange') : this.t('placeholder.date')
|
||||||
},
|
},
|
||||||
text () {
|
text () {
|
||||||
if (this.userInput !== null) {
|
if (this.userInput !== null) {
|
||||||
@@ -222,7 +230,7 @@ export default {
|
|||||||
if (this.shortcuts === false) {
|
if (this.shortcuts === false) {
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
const pickers = t('pickers')
|
const pickers = this.t('pickers')
|
||||||
const arr = [
|
const arr = [
|
||||||
{
|
{
|
||||||
text: pickers[0],
|
text: pickers[0],
|
||||||
@@ -248,9 +256,6 @@ export default {
|
|||||||
return arr
|
return arr
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
created () {
|
|
||||||
use(this.lang)
|
|
||||||
},
|
|
||||||
methods: {
|
methods: {
|
||||||
initCalendar () {
|
initCalendar () {
|
||||||
this.handleValueChange(this.value)
|
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 {
|
export default {
|
||||||
name: 'panelDate',
|
name: 'panelDate',
|
||||||
|
mixins: [locale],
|
||||||
props: {
|
props: {
|
||||||
value: null,
|
value: null,
|
||||||
startAt: null,
|
startAt: null,
|
||||||
@@ -33,7 +34,7 @@ export default {
|
|||||||
this.$emit('select', date)
|
this.$emit('select', date)
|
||||||
},
|
},
|
||||||
getDays (firstDayOfWeek) {
|
getDays (firstDayOfWeek) {
|
||||||
const days = t('days')
|
const days = this.t('days')
|
||||||
const firstday = parseInt(firstDayOfWeek, 10)
|
const firstday = parseInt(firstDayOfWeek, 10)
|
||||||
return days.concat(days).slice(firstday, firstday + 7)
|
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 {
|
export default {
|
||||||
name: 'panelMonth',
|
name: 'panelMonth',
|
||||||
|
mixins: [locale],
|
||||||
props: {
|
props: {
|
||||||
value: null,
|
value: null,
|
||||||
calendarYear: {
|
calendarYear: {
|
||||||
@@ -14,7 +15,7 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
render (h) {
|
render (h) {
|
||||||
let months = t('months')
|
let months = this.t('months')
|
||||||
const currentYear = this.value && new Date(this.value).getFullYear()
|
const currentYear = this.value && new Date(this.value).getFullYear()
|
||||||
const currentMonth = this.value && new Date(this.value).getMonth()
|
const currentMonth = this.value && new Date(this.value).getMonth()
|
||||||
months = months.map((v, i) => {
|
months = months.map((v, i) => {
|
||||||
|
|||||||
+10
-5
@@ -191,14 +191,19 @@ describe('datepicker', () => {
|
|||||||
expect(el.getAttribute('placeholder')).toBe('hehe')
|
expect(el.getAttribute('placeholder')).toBe('hehe')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('prop: lang', () => {
|
it.only('prop: lang', () => {
|
||||||
wrapper = shallowMount(DatePicker, {
|
wrapper = mount(DatePicker, {
|
||||||
propsData: {
|
propsData: {
|
||||||
lang: 'en'
|
lang: 'en',
|
||||||
|
value: new Date(2018, 5, 5)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
const el = wrapper.find('.mx-input').element
|
const el = wrapper.find('.mx-current-month')
|
||||||
expect(el.getAttribute('placeholder')).toBe('Select Date')
|
expect(el.text()).toBe('Jun')
|
||||||
|
wrapper.setProps({
|
||||||
|
lang: 'zh'
|
||||||
|
})
|
||||||
|
expect(el.text()).toBe('6月')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('prop: shortcuts', () => {
|
it('prop: shortcuts', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user