Skip to content

Commit

Permalink
Merge pull request #130 from elreco/OnTheFlyLocaleImport
Browse files Browse the repository at this point in the history
feat(i18n): import locales outside the component
  • Loading branch information
elreco committed Feb 21, 2024
2 parents 70e38ea + 62e7590 commit 21e2f8a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
11 changes: 8 additions & 3 deletions docs/advanced-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
atClick: () => {
const date = new Date();
return [
new Date(date.setDate(date.getDate() - 14)),
new Date(date.setDate(date.getDate() - 14)),
new Date()
];
}
Expand Down Expand Up @@ -159,7 +159,10 @@ const customShortcuts = () => {

## Localization (i18n)

Vue Tailwind Datepicker extend to day.js<br>
Vue Tailwind Datepicker now supports on-the-fly locale importing, extending to day.js. <br>
This means you only import the specific locale you need directly into your project, reducing unnecessary load of unused locales.
<br>
<br>
[List of supported locales](https://github.com/iamkun/dayjs/tree/dev/src/locale)

<DemoLayout>
Expand All @@ -174,6 +177,8 @@ Vue Tailwind Datepicker extend to day.js<br>
```vue
<script setup>
import { ref } from "vue";
// Import the specific locale from day.js
import 'dayjs/locale/fr' // Replace 'fr' with your desired locale
const dateValue = ref([]);
const options = ref({
shortcuts: {
Expand All @@ -192,7 +197,7 @@ const options = ref({
<template>
<vue-tailwind-datepicker
i18n="id"
i18n="fr"
:auto-apply="false"
:options="options"
v-model="dateValue"
Expand Down
5 changes: 4 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import dayjs from 'dayjs'
import { ref } from 'vue'
import type { Dayjs } from 'dayjs'
import VueTailwindDatePicker from './VueTailwindDatePicker.vue'
import 'dayjs/locale/fr'
import 'dayjs/locale/de'
import 'dayjs/locale/es'
const dateValue = ref({
startDate: dayjs().format('YYYY-MM-DD HH:mm:ss'),
endDate: dayjs().format('YYYY-MM-DD HH:mm:ss'),
})
const currentLocale = ref('es')
const locales = ['en', 'es', 'de']
const locales = ['en', 'es', 'de', 'fr']
function onClickSomething(e: Dayjs) {
console.log(e)
Expand Down
8 changes: 2 additions & 6 deletions src/VueTailwindDatePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
watch,
watchEffect,
} from 'vue'
import { localesMap } from './utils'
import 'dayjs/locale/en'
import VtdHeader from './components/Header.vue'
import VtdShortcut from './components/Shortcut.vue'
import VtdCalendar from './components/Calendar.vue'
Expand Down Expand Up @@ -1261,11 +1261,7 @@ watchEffect(() => {
const locale = props.i18n
const modelValueCloned = props.modelValue
nextTick(async () => {
if (locale in localesMap) {
const localeData = await localesMap[locale]()
dayjs.locale(localeData, undefined, true)
dayjs.locale(locale)
}
dayjs.locale(locale)
let s, e
if (asRange()) {
Expand Down
7 changes: 0 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,3 @@ export function injectStrict<T>(key: InjectionKey<T>, fallback?: T) {

return resolved
}

export const localesMap = Object.fromEntries(
Object.entries(import.meta.glob('../node_modules/dayjs/esm/locale/*.js', { import: 'default' })).map(
([path, loadLocale]) => [path.match(/([\w-]*)\.js$/)?.[1], loadLocale],
),
) as Record<string, () => Promise<ILocale>>

0 comments on commit 21e2f8a

Please sign in to comment.