locale.mjs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Utilities
  2. import { computed, inject, provide, ref } from 'vue';
  3. import { defaultRtl } from "../locale/index.mjs";
  4. import { createVuetifyAdapter } from "../locale/adapters/vuetify.mjs"; // Types
  5. export const LocaleSymbol = Symbol.for('vuetify:locale');
  6. function isLocaleInstance(obj) {
  7. return obj.name != null;
  8. }
  9. export function createLocale(options) {
  10. const i18n = options?.adapter && isLocaleInstance(options?.adapter) ? options?.adapter : createVuetifyAdapter(options);
  11. const rtl = createRtl(i18n, options);
  12. return {
  13. ...i18n,
  14. ...rtl
  15. };
  16. }
  17. export function useLocale() {
  18. const locale = inject(LocaleSymbol);
  19. if (!locale) throw new Error('[Vuetify] Could not find injected locale instance');
  20. return locale;
  21. }
  22. export function provideLocale(props) {
  23. const locale = inject(LocaleSymbol);
  24. if (!locale) throw new Error('[Vuetify] Could not find injected locale instance');
  25. const i18n = locale.provide(props);
  26. const rtl = provideRtl(i18n, locale.rtl, props);
  27. const data = {
  28. ...i18n,
  29. ...rtl
  30. };
  31. provide(LocaleSymbol, data);
  32. return data;
  33. }
  34. // RTL
  35. export const RtlSymbol = Symbol.for('vuetify:rtl');
  36. export function createRtl(i18n, options) {
  37. const rtl = ref(options?.rtl ?? defaultRtl);
  38. const isRtl = computed(() => rtl.value[i18n.current.value] ?? false);
  39. return {
  40. isRtl,
  41. rtl,
  42. rtlClasses: computed(() => `v-locale--is-${isRtl.value ? 'rtl' : 'ltr'}`)
  43. };
  44. }
  45. export function provideRtl(locale, rtl, props) {
  46. const isRtl = computed(() => props.rtl ?? rtl.value[locale.current.value] ?? false);
  47. return {
  48. isRtl,
  49. rtl,
  50. rtlClasses: computed(() => `v-locale--is-${isRtl.value ? 'rtl' : 'ltr'}`)
  51. };
  52. }
  53. export function useRtl() {
  54. const locale = inject(LocaleSymbol);
  55. if (!locale) throw new Error('[Vuetify] Could not find injected rtl instance');
  56. return {
  57. isRtl: locale.isRtl,
  58. rtlClasses: locale.rtlClasses
  59. };
  60. }
  61. //# sourceMappingURL=locale.mjs.map