-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
locale.ts
156 lines (126 loc) Β· 3.44 KB
/
locale.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Utilities
import { computed, inject, provide, ref } from 'vue'
import { createVuetifyAdapter } from '@/locale/adapters/vuetify'
// Types
import type { InjectionKey, Ref } from 'vue'
export interface LocaleMessages {
[key: string]: LocaleMessages | string
}
export interface LocaleOptions {
messages?: LocaleMessages
locale?: string
fallback?: string
adapter?: LocaleInstance
}
export interface LocaleInstance {
name: string
messages: Ref<LocaleMessages>
current: Ref<string>
fallback: Ref<string>
t: (key: string, ...params: unknown[]) => string
n: (value: number) => string
provide: (props: LocaleOptions) => LocaleInstance
}
export const LocaleSymbol: InjectionKey<LocaleInstance & RtlInstance> = Symbol.for('vuetify:locale')
function isLocaleInstance (obj: any): obj is LocaleInstance {
return obj.name != null
}
export function createLocale (options?: LocaleOptions & RtlOptions) {
const i18n = options?.adapter && isLocaleInstance(options?.adapter) ? options?.adapter : createVuetifyAdapter(options)
const rtl = createRtl(i18n, options)
return { ...i18n, ...rtl }
}
export function useLocale () {
const locale = inject(LocaleSymbol)
if (!locale) throw new Error('[Vuetify] Could not find injected locale instance')
return locale
}
export function provideLocale (props: LocaleOptions & RtlProps) {
const locale = inject(LocaleSymbol)
if (!locale) throw new Error('[Vuetify] Could not find injected locale instance')
const i18n = locale.provide(props)
const rtl = provideRtl(i18n, locale.rtl, props)
const data = { ...i18n, ...rtl }
provide(LocaleSymbol, data)
return data
}
// RTL
export interface RtlOptions {
rtl?: Record<string, boolean>
}
export interface RtlProps {
rtl?: boolean
}
export interface RtlInstance {
isRtl: Ref<boolean>
rtl: Ref<Record<string, boolean>>
rtlClasses: Ref<string>
}
export const RtlSymbol: InjectionKey<RtlInstance> = Symbol.for('vuetify:rtl')
function genDefaults () {
return {
af: false,
ar: true,
bg: false,
ca: false,
ckb: false,
cs: false,
de: false,
el: false,
en: false,
es: false,
et: false,
fa: true,
fi: false,
fr: false,
hr: false,
hu: false,
he: true,
id: false,
it: false,
ja: false,
ko: false,
lv: false,
lt: false,
nl: false,
no: false,
pl: false,
pt: false,
ro: false,
ru: false,
sk: false,
sl: false,
srCyrl: false,
srLatn: false,
sv: false,
th: false,
tr: false,
az: false,
uk: false,
vi: false,
zhHans: false,
zhHant: false,
}
}
export function createRtl (i18n: LocaleInstance, options?: RtlOptions): RtlInstance {
const rtl = ref<Record<string, boolean>>(options?.rtl ?? genDefaults())
const isRtl = computed(() => rtl.value[i18n.current.value] ?? false)
return {
isRtl,
rtl,
rtlClasses: computed(() => `v-locale--is-${isRtl.value ? 'rtl' : 'ltr'}`),
}
}
export function provideRtl (locale: LocaleInstance, rtl: RtlInstance['rtl'], props: RtlProps): RtlInstance {
const isRtl = computed(() => props.rtl ?? rtl.value[locale.current.value] ?? false)
return {
isRtl,
rtl,
rtlClasses: computed(() => `v-locale--is-${isRtl.value ? 'rtl' : 'ltr'}`),
}
}
export function useRtl () {
const locale = inject(LocaleSymbol)
if (!locale) throw new Error('[Vuetify] Could not find injected rtl instance')
return { isRtl: locale.isRtl, rtlClasses: locale.rtlClasses }
}