/
composables.ts
235 lines (219 loc) · 7.47 KB
/
composables.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import { findBrowserLocale, getComposer } from 'vue-i18n-routing'
import { useRoute, useRouter, useRequestHeaders, useCookie, useNuxtApp } from '#imports'
import { parseAcceptLanguage } from '#build/i18n.internal.mjs'
import { nuxtI18nInternalOptions, nuxtI18nOptionsDefault, localeCodes as _localeCodes } from '#build/i18n.options.mjs'
import {
useRouteBaseName as _useRouteBaseName,
useLocalePath as _useLocalePath,
useLocaleRoute as _useLocaleRoute,
useSwitchLocalePath as _useSwitchLocalePath,
useLocaleHead as _useLocaleHead
} from 'vue-i18n-routing'
import type { Ref } from 'vue'
import type { DetectBrowserLanguageOptions } from '#build/i18n.options.mjs'
export * from 'vue-i18n'
export type { LocaleObject } from 'vue-i18n-routing'
import type { Locale } from 'vue-i18n'
/**
* The `useRouteBaseName` composable returns function that get the route base name.
*
* @remarks
* The function returned by `useRouteBaseName` is the wrapper function with the same signature as {@link getRouteBaseName}.
*
* `useRouteBaseName` is powered by [vue-i18n-routing](https://github.com/intlify/routing/tree/main/packages/vue-i18n-routing).
*
* @param route - A route object. if not provided, the route is returned with `useRoute` will be used internally
*
* @returns A {@link RouteBaseNameFunction}.
*
* @public
*/
export function useRouteBaseName(): ReturnType<typeof _useRouteBaseName> {
return _useRouteBaseName({
route: useRoute(),
router: useRouter(),
i18n: getComposer(useNuxtApp().$i18n)
})
}
/**
* The `useLocalePath` composable returns function that resolve the locale path.
*
* @remarks
* The function returned by `useLocalePath` is the wrapper function with the same signature as {@link localePath}.
*
* `useLocalePath` is powered by [vue-i18n-routing](https://github.com/intlify/routing/tree/main/packages/vue-i18n-routing).
*
* @returns A {@link LocalePathFunction}.
*
* @public
*/
export function useLocalePath(): ReturnType<typeof _useLocalePath> {
return _useLocalePath({
router: useRouter(),
route: useRoute(),
i18n: getComposer(useNuxtApp().$i18n)
})
}
/**
* The `useLocaleRoute` composable returns function that resolve the locale route.
*
* @remarks
* The function returned by `useLocaleRoute` is the wrapper function with the same signature as {@link localeRoute}.
*
* `useLocaleRoute` is powered by [vue-i18n-routing](https://github.com/intlify/routing/tree/main/packages/vue-i18n-routing).
*
* @returns A {@link LocaleRouteFunction}.
*
* @public
*/
export function useLocaleRoute(): ReturnType<typeof _useLocaleRoute> {
return _useLocaleRoute({
router: useRouter(),
route: useRoute(),
i18n: getComposer(useNuxtApp().$i18n)
})
}
/**
* The `useSwitchLocalePath` composable returns function that resolve the locale location.
*
* @remarks
* The function returned by `useSwitchLocalePath` is the wrapper function with the same signature as {@link switchLocalePath}.
*
* `useSwitchLocalePath` composable returns function that resolve the locale location. `useSwitchLocalePath` is powered by [vue-i18n-routing](https://github.com/intlify/routing/tree/main/packages/vue-i18n-routing).
*
* @returns A {@link SwitchLocalePathFunction}.
*
* @public
*/
export function useSwitchLocalePath(): ReturnType<typeof _useSwitchLocalePath> {
return _useSwitchLocalePath({
router: useRouter(),
route: useRoute(),
i18n: getComposer(useNuxtApp().$i18n)
})
}
/**
* The `useLocaleHead` composable returns localized head properties for locale-related aspects.
*
* @param options - An options, see about details {@link I18nHeadOptions}.
*
* @returns The localized {@link I18nHeadMetaInfo | head properties} with Vue `ref`.
*
* @public
*/
export function useLocaleHead(
options: Pick<
NonNullable<Parameters<typeof _useLocaleHead>[0]>,
'addDirAttribute' | 'addSeoAttributes' | 'identifierAttribute'
> = {
addDirAttribute: false,
addSeoAttributes: false,
identifierAttribute: 'hid'
}
): ReturnType<typeof _useLocaleHead> {
const { addDirAttribute, addSeoAttributes, identifierAttribute } = options
return _useLocaleHead({
addDirAttribute,
addSeoAttributes,
identifierAttribute,
router: useRouter(),
route: useRoute(),
i18n: getComposer(useNuxtApp().$i18n)
})
}
/**
* The `useBrowserLocale` composable returns the browser locale.
*
* @remarks
* if this composable function is called on client-side, it detects the locale from the value of `navigator.languages`. Else on the server side, the locale is detected from the value of `accept-language` header.
*
* @returns the browser locale, if not detected, return `null`.
*
* @public
*/
export function useBrowserLocale(normalizedLocales = nuxtI18nInternalOptions.__normalizedLocales): string | null {
const headers = useRequestHeaders(['accept-language'])
return (
findBrowserLocale(
normalizedLocales,
process.client ? (navigator.languages as string[]) : parseAcceptLanguage(headers['accept-language'] || '')
) || null
)
}
/**
* The `useCookieLocale` composable returns the cookie locale.
*
* @remarks
* If this composable function is called client-side, it detects the locale from the value of `document.cookie` via `useCookie`. Otherwise when used server-side, it detects the locale from the value of the `cookie` header.
*
* Note that if the value of `detectBrowserLanguage.useCookie` is `false`, an empty string is always returned.
*
* @returns the cookie locale with Vue `ref`. if not detected, return **empty string** with `ref`.
*
* @public
*/
export function useCookieLocale(
options: Required<Pick<DetectBrowserLanguageOptions, 'useCookie' | 'cookieKey'>> & {
localeCodes: readonly string[]
} = {
useCookie: nuxtI18nOptionsDefault.detectBrowserLanguage.useCookie,
cookieKey: nuxtI18nOptionsDefault.detectBrowserLanguage.cookieKey,
localeCodes: _localeCodes
}
): Ref<string> {
// @ts-ignore NOTE: `ref` is auto-imported from `nuxt`
const locale: Ref<string> = ref('')
if (options.useCookie) {
let code: string | null = null
if (process.client) {
const cookie = useCookie<string>(options.cookieKey) as Ref<string>
code = cookie.value
} else if (process.server) {
const cookie = useRequestHeaders(['cookie'])
// eslint-disable-next-line @typescript-eslint/no-explicit-any
code = (cookie as any)[options.cookieKey]
}
if (code && options.localeCodes.includes(code)) {
locale.value = code
}
}
return locale
}
const warnRuntimeUsage = (method: string) =>
console.warn(
method +
'() is a compiler-hint helper that is only usable inside ' +
'the script block of a single file component. Its arguments should be ' +
'compiled away and passing it at runtime has no effect.'
)
/**
* TODO:
* `paths`, `locales` completions like `unplugin-vue-router`
* ref: https://github.com/posva/unplugin-vue-router
*/
/**
* The i18n custom route for page components
*/
export interface I18nRoute {
/**
* Customize page component routes per locale.
*
* @description You can specify static and dynamic paths for vue-router.
*/
paths?: Record<Locale, string>
/**
* Some locales to which the page component should be localized.
*/
locales?: string[]
}
/**
* Define custom route for page component
*
* @param route - The custom route
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function defineI18nRoute(route: I18nRoute | false): void {
if (process.dev) {
warnRuntimeUsage('defineI18nRoute')
}
}