nuxt-i18n-micro 1.63.2 → 1.65.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client/200.html +8 -8
- package/dist/client/404.html +8 -8
- package/dist/client/_nuxt/{AXE8DZQI.js → CcDaBmcC.js} +1 -1
- package/dist/client/_nuxt/{Ddb0iCRU.js → ClTsZsex.js} +59 -59
- package/dist/client/_nuxt/{Deg2o1hP.js → DT0mZaxo.js} +1 -1
- package/dist/client/_nuxt/builds/latest.json +1 -1
- package/dist/client/_nuxt/builds/meta/ca0ba9b5-54af-4e8c-85d7-f8f741ea6b35.json +1 -0
- package/dist/client/_nuxt/error-404.DCBEmQSD.css +1 -0
- package/dist/client/_nuxt/error-500.D1x2GrnM.css +1 -0
- package/dist/client/_nuxt/{D9gx-VEt.js → rqhqIC0Q.js} +1 -1
- package/dist/client/index.html +8 -8
- package/dist/module.d.mts +3 -3
- package/dist/module.d.ts +3 -3
- package/dist/module.json +1 -1
- package/dist/module.mjs +48 -26
- package/dist/runtime/components/i18n-t.vue.d.ts +1 -1
- package/dist/runtime/components/locale-redirect.vue +20 -4
- package/dist/runtime/composables/useLocaleHead.js +2 -2
- package/dist/runtime/plugins/01.plugin.d.ts +7 -8
- package/dist/runtime/plugins/01.plugin.js +85 -325
- package/dist/runtime/plugins/03.define.d.ts +2 -2
- package/dist/runtime/plugins/03.define.js +29 -29
- package/dist/runtime/plugins/04.auto-detect.js +5 -3
- package/dist/runtime/translation-server-middleware.d.ts +1 -1
- package/dist/types.d.mts +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/utils.d.mts +3 -2
- package/dist/utils.d.ts +3 -2
- package/dist/utils.mjs +4 -1
- package/package.json +5 -4
- package/dist/client/_nuxt/builds/meta/4fb36a07-acc6-4000-9c26-f521b5d784b5.json +0 -1
- package/dist/client/_nuxt/error-404.vh32VtAd.css +0 -1
- package/dist/client/_nuxt/error-500.EgQ3RNLZ.css +0 -1
- package/dist/runtime/helpers.d.ts +0 -6
- package/dist/runtime/helpers.js +0 -5
- package/dist/shared/nuxt-i18n-micro.4b19b717.d.mts +0 -57
- package/dist/shared/nuxt-i18n-micro.4b19b717.d.ts +0 -57
|
@@ -1,233 +1,40 @@
|
|
|
1
|
-
import { useTranslationHelper, interpolate } from "nuxt-i18n-micro-core";
|
|
2
|
-
import { isNoPrefixStrategy, withPrefixStrategy } from "../helpers.js";
|
|
1
|
+
import { useTranslationHelper, interpolate, isNoPrefixStrategy, RouteService, FormatService } from "nuxt-i18n-micro-core";
|
|
3
2
|
import { defineNuxtPlugin, useRuntimeConfig } from "#app";
|
|
4
|
-
import { useRouter, useCookie, useState, navigateTo } from "#imports";
|
|
3
|
+
import { useRouter, useCookie, useState, unref, navigateTo } from "#imports";
|
|
5
4
|
import { plural } from "#build/i18n.plural.mjs";
|
|
6
5
|
const i18nHelper = useTranslationHelper();
|
|
7
6
|
const isDev = process.env.NODE_ENV !== "production";
|
|
8
|
-
function getCurrentLocale(route, i18nConfig, hashLocale, noPrefixStrategy) {
|
|
9
|
-
if (i18nConfig.hashMode && hashLocale) {
|
|
10
|
-
return hashLocale;
|
|
11
|
-
}
|
|
12
|
-
if (isNoPrefixStrategy(i18nConfig.strategy) && noPrefixStrategy) {
|
|
13
|
-
return noPrefixStrategy;
|
|
14
|
-
}
|
|
15
|
-
return (route.params?.locale ?? i18nConfig.defaultLocale).toString();
|
|
16
|
-
}
|
|
17
|
-
function getCurrentName(route, i18nConfig, hashLocale, noPrefixStrategy) {
|
|
18
|
-
const currentLocaleCode = getCurrentLocale(route, i18nConfig, hashLocale, noPrefixStrategy);
|
|
19
|
-
const checkLocale = i18nConfig.locales?.find((l) => l.code === currentLocaleCode);
|
|
20
|
-
if (!checkLocale) {
|
|
21
|
-
return null;
|
|
22
|
-
}
|
|
23
|
-
return checkLocale?.displayName ?? null;
|
|
24
|
-
}
|
|
25
|
-
function getRouteName(route, locale) {
|
|
26
|
-
const name = (route.name ?? "").toString();
|
|
27
|
-
return name.toString().replace("localized-", "").replace(new RegExp(`-${locale}$`), "");
|
|
28
|
-
}
|
|
29
|
-
function switchLocaleRoute(fromLocale, toLocale, route, router, i18nConfig, i18nRouteParams) {
|
|
30
|
-
const currentLocale = i18nConfig.locales?.find((l) => l.code === toLocale);
|
|
31
|
-
function getFullPathWithBaseUrl(route2) {
|
|
32
|
-
const resolvedRoute = router.resolve(route2);
|
|
33
|
-
let fullPath = resolvedRoute.fullPath;
|
|
34
|
-
if (currentLocale?.baseDefault) {
|
|
35
|
-
fullPath = fullPath.replace(new RegExp(`^/${currentLocale.code}`), "");
|
|
36
|
-
}
|
|
37
|
-
let baseUrl = currentLocale.baseUrl;
|
|
38
|
-
if (baseUrl?.endsWith("/")) {
|
|
39
|
-
baseUrl = baseUrl.slice(0, -1);
|
|
40
|
-
}
|
|
41
|
-
return baseUrl + fullPath;
|
|
42
|
-
}
|
|
43
|
-
const routeName = getRouteName(route, fromLocale);
|
|
44
|
-
if (router.hasRoute(`localized-${routeName}-${toLocale}`)) {
|
|
45
|
-
const newParams2 = { ...route.params, ...i18nRouteParams?.[toLocale] };
|
|
46
|
-
newParams2.locale = toLocale;
|
|
47
|
-
const newRoute2 = {
|
|
48
|
-
name: `localized-${routeName}-${toLocale}`,
|
|
49
|
-
params: newParams2
|
|
50
|
-
};
|
|
51
|
-
if (currentLocale?.baseUrl) {
|
|
52
|
-
return getFullPathWithBaseUrl(newRoute2);
|
|
53
|
-
}
|
|
54
|
-
return newRoute2;
|
|
55
|
-
}
|
|
56
|
-
let newRouteName = routeName;
|
|
57
|
-
const newParams = { ...route.params, ...i18nRouteParams?.[toLocale] };
|
|
58
|
-
delete newParams.locale;
|
|
59
|
-
if (!isNoPrefixStrategy(i18nConfig.strategy)) {
|
|
60
|
-
if (routeName === "custom-fallback-route") {
|
|
61
|
-
newRouteName = routeName;
|
|
62
|
-
} else {
|
|
63
|
-
newRouteName = toLocale !== i18nConfig.defaultLocale || withPrefixStrategy(i18nConfig.strategy) ? `localized-${routeName}` : routeName;
|
|
64
|
-
}
|
|
65
|
-
if (toLocale !== i18nConfig.defaultLocale || withPrefixStrategy(i18nConfig.strategy)) {
|
|
66
|
-
newParams.locale = toLocale;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
const newRoute = {
|
|
70
|
-
name: newRouteName,
|
|
71
|
-
params: newParams
|
|
72
|
-
};
|
|
73
|
-
if (currentLocale?.baseUrl) {
|
|
74
|
-
return getFullPathWithBaseUrl(newRoute);
|
|
75
|
-
}
|
|
76
|
-
return newRoute;
|
|
77
|
-
}
|
|
78
|
-
function switchLocale(fromLocale, toLocale, route, router, i18nConfig, i18nRouteParams) {
|
|
79
|
-
const checkLocale = i18nConfig.locales?.find((l) => l.code === toLocale);
|
|
80
|
-
if (!checkLocale) {
|
|
81
|
-
console.warn(`Locale ${toLocale} is not available`);
|
|
82
|
-
return Promise.reject(`Locale ${toLocale} is not available`);
|
|
83
|
-
}
|
|
84
|
-
const switchedRoute = switchLocaleRoute(
|
|
85
|
-
fromLocale,
|
|
86
|
-
toLocale,
|
|
87
|
-
route,
|
|
88
|
-
router,
|
|
89
|
-
i18nConfig,
|
|
90
|
-
i18nRouteParams
|
|
91
|
-
);
|
|
92
|
-
if (typeof switchedRoute === "string" && switchedRoute.startsWith("http")) {
|
|
93
|
-
return navigateTo(switchedRoute, { redirectCode: 200, external: true });
|
|
94
|
-
}
|
|
95
|
-
if (isNoPrefixStrategy(i18nConfig.strategy)) {
|
|
96
|
-
switchedRoute.force = true;
|
|
97
|
-
}
|
|
98
|
-
return router.push(switchedRoute);
|
|
99
|
-
}
|
|
100
|
-
function getLocalizedRoute(to, router, route, i18nConfig, locale, hashLocale, noPrefixStrategy) {
|
|
101
|
-
const resolveParams = (to2) => {
|
|
102
|
-
const params = typeof to2 === "object" && "params" in to2 && typeof to2.params === "object" ? { ...to2.params } : {};
|
|
103
|
-
if (typeof to2 === "string") {
|
|
104
|
-
const resolved = router.resolve(to2);
|
|
105
|
-
if (resolved && resolved.params) {
|
|
106
|
-
Object.assign(params, resolved.params);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
return params;
|
|
110
|
-
};
|
|
111
|
-
if (withPrefixStrategy(i18nConfig.strategy)) {
|
|
112
|
-
const defaultLocale = i18nConfig.defaultLocale;
|
|
113
|
-
let resolvedTo = to;
|
|
114
|
-
if (typeof to === "string") {
|
|
115
|
-
resolvedTo = router.resolve("/" + defaultLocale + to);
|
|
116
|
-
}
|
|
117
|
-
const defaultRouteName = getRouteName(resolvedTo, defaultLocale);
|
|
118
|
-
const newParams2 = resolveParams(resolvedTo);
|
|
119
|
-
newParams2.locale = defaultLocale;
|
|
120
|
-
if (router.hasRoute(`localized-${defaultRouteName}`)) {
|
|
121
|
-
to = router.resolve({
|
|
122
|
-
name: `localized-${defaultRouteName}`,
|
|
123
|
-
query: resolvedTo.query,
|
|
124
|
-
params: newParams2
|
|
125
|
-
});
|
|
126
|
-
} else if (router.hasRoute(`localized-${defaultRouteName}-${defaultLocale}`)) {
|
|
127
|
-
to = router.resolve({
|
|
128
|
-
name: `localized-${defaultRouteName}-${defaultLocale}`,
|
|
129
|
-
query: resolvedTo.query,
|
|
130
|
-
params: newParams2
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
const currentLocale = locale || getCurrentLocale(route, i18nConfig, hashLocale, noPrefixStrategy);
|
|
135
|
-
const selectRoute = router.resolve(to);
|
|
136
|
-
const routeName = getRouteName(selectRoute, currentLocale).replace(new RegExp(`-${i18nConfig.defaultLocale}$`), "");
|
|
137
|
-
if (!routeName || routeName === "") {
|
|
138
|
-
const resolved = router.resolve(to);
|
|
139
|
-
let url = resolved.path.replace(new RegExp(`^/${currentLocale}/`), "/");
|
|
140
|
-
if (currentLocale !== i18nConfig.defaultLocale || withPrefixStrategy(i18nConfig.strategy)) {
|
|
141
|
-
url = "/" + currentLocale + url;
|
|
142
|
-
}
|
|
143
|
-
return router.resolve({
|
|
144
|
-
path: url,
|
|
145
|
-
query: selectRoute.query,
|
|
146
|
-
hash: selectRoute.hash
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
if (router.hasRoute(`localized-${routeName}-${currentLocale}`)) {
|
|
150
|
-
const newParams2 = resolveParams(selectRoute);
|
|
151
|
-
newParams2.locale = currentLocale;
|
|
152
|
-
return router.resolve({
|
|
153
|
-
name: `localized-${routeName}-${currentLocale}`,
|
|
154
|
-
params: newParams2,
|
|
155
|
-
query: selectRoute.query,
|
|
156
|
-
hash: selectRoute.hash
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
const newRouteName = currentLocale !== i18nConfig.defaultLocale || withPrefixStrategy(i18nConfig.strategy) ? `localized-${routeName}` : routeName;
|
|
160
|
-
if (!router.hasRoute(newRouteName)) {
|
|
161
|
-
const newParams2 = resolveParams(to);
|
|
162
|
-
delete newParams2.locale;
|
|
163
|
-
if (!router.hasRoute(routeName)) {
|
|
164
|
-
return router.resolve("/");
|
|
165
|
-
}
|
|
166
|
-
return router.resolve({
|
|
167
|
-
name: routeName,
|
|
168
|
-
params: newParams2,
|
|
169
|
-
query: selectRoute.query,
|
|
170
|
-
hash: selectRoute.hash
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
const newParams = resolveParams(to);
|
|
174
|
-
delete newParams.locale;
|
|
175
|
-
if (currentLocale !== i18nConfig.defaultLocale || withPrefixStrategy(i18nConfig.strategy)) {
|
|
176
|
-
newParams.locale = currentLocale;
|
|
177
|
-
}
|
|
178
|
-
return router.resolve({
|
|
179
|
-
name: newRouteName,
|
|
180
|
-
params: newParams,
|
|
181
|
-
query: selectRoute.query,
|
|
182
|
-
hash: selectRoute.hash
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
function formatNumber(value, locale, options) {
|
|
186
|
-
return new Intl.NumberFormat(locale, options).format(value);
|
|
187
|
-
}
|
|
188
|
-
function formatDate(value, locale, options) {
|
|
189
|
-
return new Intl.DateTimeFormat(locale, options).format(new Date(value));
|
|
190
|
-
}
|
|
191
|
-
function formatRelativeTime(value, locale, options) {
|
|
192
|
-
const date = new Date(value);
|
|
193
|
-
const now = /* @__PURE__ */ new Date();
|
|
194
|
-
const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1e3);
|
|
195
|
-
const units = [
|
|
196
|
-
{ unit: "year", seconds: 31536e3 },
|
|
197
|
-
{ unit: "month", seconds: 2592e3 },
|
|
198
|
-
{ unit: "day", seconds: 86400 },
|
|
199
|
-
{ unit: "hour", seconds: 3600 },
|
|
200
|
-
{ unit: "minute", seconds: 60 },
|
|
201
|
-
{ unit: "second", seconds: 1 }
|
|
202
|
-
];
|
|
203
|
-
for (const { unit, seconds } of units) {
|
|
204
|
-
const diff = Math.floor(diffInSeconds / seconds);
|
|
205
|
-
if (diff >= 1) {
|
|
206
|
-
return new Intl.RelativeTimeFormat(locale, options).format(-diff, unit);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
return new Intl.RelativeTimeFormat(locale, options).format(0, "second");
|
|
210
|
-
}
|
|
211
7
|
export default defineNuxtPlugin(async (nuxtApp) => {
|
|
212
|
-
if (!nuxtApp.payload.data.translations) {
|
|
213
|
-
nuxtApp.payload.data.translations = {};
|
|
214
|
-
}
|
|
215
8
|
const config = useRuntimeConfig();
|
|
216
9
|
const i18nConfig = config.public.i18nConfig;
|
|
217
10
|
const apiBaseUrl = i18nConfig.apiBaseUrl ?? "_locales";
|
|
11
|
+
const router = useRouter();
|
|
218
12
|
const runtimeConfig = useRuntimeConfig();
|
|
219
13
|
let hashLocaleDefault = null;
|
|
220
14
|
let noPrefixDefault = null;
|
|
221
15
|
if (i18nConfig.hashMode) {
|
|
222
|
-
hashLocaleDefault = await nuxtApp.runWithContext(() =>
|
|
223
|
-
return useCookie("hash-locale").value;
|
|
224
|
-
});
|
|
16
|
+
hashLocaleDefault = await nuxtApp.runWithContext(() => useCookie("hash-locale").value);
|
|
225
17
|
}
|
|
226
18
|
if (isNoPrefixStrategy(i18nConfig.strategy)) {
|
|
227
|
-
noPrefixDefault = await nuxtApp.runWithContext(() =>
|
|
228
|
-
return useCookie("no-prefix-locale").value;
|
|
229
|
-
});
|
|
19
|
+
noPrefixDefault = await nuxtApp.runWithContext(() => useCookie("no-prefix-locale").value);
|
|
230
20
|
}
|
|
21
|
+
const routeService = new RouteService(
|
|
22
|
+
i18nConfig,
|
|
23
|
+
router,
|
|
24
|
+
hashLocaleDefault,
|
|
25
|
+
noPrefixDefault,
|
|
26
|
+
(to, options) => navigateTo(to, options),
|
|
27
|
+
(name, value) => {
|
|
28
|
+
nuxtApp.runWithContext(() => {
|
|
29
|
+
return useCookie(name).value = value;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
);
|
|
33
|
+
const translationService = new FormatService();
|
|
34
|
+
const i18nRouteParams = useState("i18n-route-params", () => ({}));
|
|
35
|
+
nuxtApp.hook("page:start", () => {
|
|
36
|
+
i18nRouteParams.value = null;
|
|
37
|
+
});
|
|
231
38
|
const loadTranslationsIfNeeded = async (locale, routeName, path) => {
|
|
232
39
|
try {
|
|
233
40
|
if (!i18nHelper.hasPageTranslation(locale, routeName)) {
|
|
@@ -252,19 +59,17 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
252
59
|
}
|
|
253
60
|
};
|
|
254
61
|
async function loadGlobalTranslations(to) {
|
|
255
|
-
let
|
|
256
|
-
let noPrefixLocale = null;
|
|
62
|
+
let locale = routeService.getCurrentLocale(to);
|
|
257
63
|
if (i18nConfig.hashMode) {
|
|
258
|
-
|
|
259
|
-
return useCookie("hash-locale").value;
|
|
64
|
+
locale = await nuxtApp.runWithContext(() => {
|
|
65
|
+
return useCookie("hash-locale", { default: () => locale }).value;
|
|
260
66
|
});
|
|
261
67
|
}
|
|
262
68
|
if (isNoPrefixStrategy(i18nConfig.strategy)) {
|
|
263
|
-
|
|
264
|
-
return useCookie("no-prefix-locale").value;
|
|
69
|
+
locale = await nuxtApp.runWithContext(() => {
|
|
70
|
+
return useCookie("no-prefix-locale", { default: () => locale }).value;
|
|
265
71
|
});
|
|
266
72
|
}
|
|
267
|
-
const locale = getCurrentLocale(to, i18nConfig, hashLocale, noPrefixLocale);
|
|
268
73
|
if (!i18nHelper.hasGeneralTranslation(locale)) {
|
|
269
74
|
const url = `/${apiBaseUrl}/general/${locale}/data.json`.replace(/\/{2,}/g, "/");
|
|
270
75
|
const data = await $fetch(url, {
|
|
@@ -276,15 +81,16 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
276
81
|
await i18nHelper.loadTranslations(locale, data ?? {});
|
|
277
82
|
}
|
|
278
83
|
if (!i18nConfig.disablePageLocales) {
|
|
279
|
-
const
|
|
280
|
-
|
|
84
|
+
const locale2 = routeService.getCurrentLocale(to);
|
|
85
|
+
const routeName = routeService.getRouteName(to, locale2);
|
|
86
|
+
await loadTranslationsIfNeeded(locale2, routeName, to.fullPath);
|
|
281
87
|
}
|
|
282
88
|
await nuxtApp.callHook("i18n:register", (translations, selectedLocale) => {
|
|
283
|
-
const
|
|
284
|
-
|
|
89
|
+
const locale2 = routeService.getCurrentLocale(to);
|
|
90
|
+
const routeName = routeService.getRouteName(to, locale2);
|
|
91
|
+
i18nHelper.mergeTranslation(selectedLocale ?? locale2, routeName, translations, true);
|
|
285
92
|
}, locale);
|
|
286
93
|
}
|
|
287
|
-
const router = useRouter();
|
|
288
94
|
router.beforeEach(async (to, from, next) => {
|
|
289
95
|
if (to.path !== from.path || isNoPrefixStrategy(i18nConfig.strategy)) {
|
|
290
96
|
await loadGlobalTranslations(to);
|
|
@@ -294,85 +100,75 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
294
100
|
}
|
|
295
101
|
});
|
|
296
102
|
await loadGlobalTranslations(router.currentRoute.value);
|
|
297
|
-
const getTranslation = (key, params, defaultValue) => {
|
|
298
|
-
if (!key) return "";
|
|
299
|
-
const route = router.currentRoute.value;
|
|
300
|
-
const locale = getCurrentLocale(route, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
301
|
-
const routeName = getRouteName(route, locale);
|
|
302
|
-
let value = i18nHelper.getTranslation(locale, routeName, key);
|
|
303
|
-
if (!value) {
|
|
304
|
-
if (isDev && import.meta.client) {
|
|
305
|
-
console.warn(`Not found '${key}' key in '${locale}' locale messages.`);
|
|
306
|
-
}
|
|
307
|
-
value = defaultValue || key;
|
|
308
|
-
}
|
|
309
|
-
return typeof value === "string" && params ? interpolate(value, params) : value;
|
|
310
|
-
};
|
|
311
|
-
const i18nRouteParams = useState("i18n-route-params");
|
|
312
|
-
nuxtApp.hook("page:start", () => {
|
|
313
|
-
i18nRouteParams.value = null;
|
|
314
|
-
});
|
|
315
103
|
const provideData = {
|
|
316
104
|
i18n: void 0,
|
|
317
105
|
__micro: true,
|
|
318
|
-
getLocale: () => getCurrentLocale(
|
|
319
|
-
getLocaleName: () => getCurrentName(
|
|
106
|
+
getLocale: () => routeService.getCurrentLocale(),
|
|
107
|
+
getLocaleName: () => routeService.getCurrentName(routeService.getCurrentRoute()),
|
|
320
108
|
defaultLocale: () => i18nConfig.defaultLocale,
|
|
321
109
|
getLocales: () => i18nConfig.locales || [],
|
|
322
110
|
getRouteName: (route, locale) => {
|
|
323
|
-
const selectedLocale = locale ?? getCurrentLocale(
|
|
324
|
-
const selectedRoute = route ??
|
|
325
|
-
return getRouteName(selectedRoute, selectedLocale);
|
|
111
|
+
const selectedLocale = locale ?? routeService.getCurrentLocale();
|
|
112
|
+
const selectedRoute = route ?? routeService.getCurrentRoute();
|
|
113
|
+
return routeService.getRouteName(selectedRoute, selectedLocale);
|
|
114
|
+
},
|
|
115
|
+
t: (key, params, defaultValue) => {
|
|
116
|
+
if (!key) return "";
|
|
117
|
+
const route = routeService.getCurrentRoute();
|
|
118
|
+
const locale = routeService.getCurrentLocale();
|
|
119
|
+
const routeName = routeService.getRouteName(route, locale);
|
|
120
|
+
let value = i18nHelper.getTranslation(locale, routeName, key);
|
|
121
|
+
if (!value) {
|
|
122
|
+
if (isDev && import.meta.client) {
|
|
123
|
+
console.warn(`Not found '${key}' key in '${locale}' locale messages.`);
|
|
124
|
+
}
|
|
125
|
+
value = defaultValue || key;
|
|
126
|
+
}
|
|
127
|
+
return typeof value === "string" && params ? interpolate(value, params) : value;
|
|
326
128
|
},
|
|
327
|
-
t: getTranslation,
|
|
328
129
|
ts: (key, params, defaultValue) => {
|
|
329
|
-
const value =
|
|
130
|
+
const value = provideData.t(key, params, defaultValue);
|
|
330
131
|
return value?.toString() ?? defaultValue ?? key;
|
|
331
132
|
},
|
|
332
133
|
tc: (key, params, defaultValue) => {
|
|
333
|
-
const
|
|
334
|
-
const currentLocale = getCurrentLocale(route, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
134
|
+
const currentLocale = routeService.getCurrentLocale();
|
|
335
135
|
const { count, ..._params } = typeof params === "number" ? { count: params } : params;
|
|
336
|
-
return plural(key, Number.parseInt(count.toString()), _params, currentLocale,
|
|
136
|
+
return plural(key, Number.parseInt(count.toString()), _params, currentLocale, provideData.t) ?? defaultValue ?? key;
|
|
337
137
|
},
|
|
338
138
|
tn: (value, options) => {
|
|
339
|
-
const
|
|
340
|
-
|
|
341
|
-
return formatNumber(value, locale, options);
|
|
139
|
+
const currentLocale = routeService.getCurrentLocale();
|
|
140
|
+
return translationService.formatNumber(value, currentLocale, options);
|
|
342
141
|
},
|
|
343
142
|
td: (value, options) => {
|
|
344
|
-
const
|
|
345
|
-
|
|
346
|
-
return formatDate(value, locale, options);
|
|
143
|
+
const currentLocale = routeService.getCurrentLocale();
|
|
144
|
+
return translationService.formatDate(value, currentLocale, options);
|
|
347
145
|
},
|
|
348
|
-
tdr(value, options) {
|
|
349
|
-
const
|
|
350
|
-
|
|
351
|
-
return formatRelativeTime(value, locale, options);
|
|
146
|
+
tdr: (value, options) => {
|
|
147
|
+
const currentLocale = routeService.getCurrentLocale();
|
|
148
|
+
return translationService.formatRelativeTime(value, currentLocale, options);
|
|
352
149
|
},
|
|
353
150
|
has: (key) => {
|
|
354
|
-
return !!
|
|
151
|
+
return !!provideData.t(key);
|
|
355
152
|
},
|
|
356
153
|
mergeTranslations: (newTranslations) => {
|
|
357
|
-
const route =
|
|
358
|
-
const locale = getCurrentLocale(route
|
|
359
|
-
const routeName = getRouteName(route, locale);
|
|
154
|
+
const route = routeService.getCurrentRoute();
|
|
155
|
+
const locale = routeService.getCurrentLocale(route);
|
|
156
|
+
const routeName = routeService.getRouteName(route, locale);
|
|
360
157
|
i18nHelper.mergeTranslation(locale, routeName, newTranslations);
|
|
361
158
|
},
|
|
362
159
|
mergeGlobalTranslations: (newTranslations) => {
|
|
363
|
-
const
|
|
364
|
-
const locale = getCurrentLocale(route, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
160
|
+
const locale = routeService.getCurrentLocale();
|
|
365
161
|
i18nHelper.mergeGlobalTranslation(locale, newTranslations, true);
|
|
366
162
|
},
|
|
367
163
|
switchLocaleRoute: (toLocale) => {
|
|
368
|
-
const route =
|
|
369
|
-
const fromLocale = getCurrentLocale(route
|
|
370
|
-
return switchLocaleRoute(fromLocale, toLocale, route,
|
|
164
|
+
const route = routeService.getCurrentRoute();
|
|
165
|
+
const fromLocale = routeService.getCurrentLocale(route);
|
|
166
|
+
return routeService.switchLocaleRoute(fromLocale, toLocale, route, unref(i18nRouteParams.value));
|
|
371
167
|
},
|
|
372
168
|
switchLocalePath: (toLocale) => {
|
|
373
|
-
const route =
|
|
374
|
-
const fromLocale = getCurrentLocale(route
|
|
375
|
-
const localeRoute = switchLocaleRoute(fromLocale, toLocale, route,
|
|
169
|
+
const route = routeService.getCurrentRoute();
|
|
170
|
+
const fromLocale = routeService.getCurrentLocale(route);
|
|
171
|
+
const localeRoute = routeService.switchLocaleRoute(fromLocale, toLocale, route, unref(i18nRouteParams.value));
|
|
376
172
|
if (typeof localeRoute === "string") {
|
|
377
173
|
return localeRoute;
|
|
378
174
|
}
|
|
@@ -382,60 +178,16 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
382
178
|
return "";
|
|
383
179
|
},
|
|
384
180
|
switchLocale: (toLocale) => {
|
|
385
|
-
|
|
386
|
-
const fromLocale = getCurrentLocale(route, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
387
|
-
if (i18nConfig.hashMode) {
|
|
388
|
-
hashLocaleDefault = toLocale;
|
|
389
|
-
useCookie("hash-locale").value = toLocale;
|
|
390
|
-
}
|
|
391
|
-
if (isNoPrefixStrategy(i18nConfig.strategy)) {
|
|
392
|
-
noPrefixDefault = toLocale;
|
|
393
|
-
useCookie("no-prefix-locale").value = toLocale;
|
|
394
|
-
}
|
|
395
|
-
switchLocale(fromLocale, toLocale, route, router, i18nConfig, i18nRouteParams.value);
|
|
181
|
+
return routeService.switchLocaleLogic(toLocale, unref(i18nRouteParams.value));
|
|
396
182
|
},
|
|
397
183
|
switchRoute: (route, toLocale) => {
|
|
398
|
-
|
|
399
|
-
const fromLocale = getCurrentLocale(currentRoute, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
400
|
-
const currentLocale = toLocale ?? fromLocale;
|
|
401
|
-
if (typeof route === "string") {
|
|
402
|
-
if (currentLocale !== i18nConfig.defaultLocale || withPrefixStrategy(i18nConfig.strategy)) {
|
|
403
|
-
const currentRoute2 = router.currentRoute.value;
|
|
404
|
-
const fromLocale2 = getCurrentLocale(currentRoute2, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
405
|
-
route = router.resolve("/" + fromLocale2 + route);
|
|
406
|
-
} else {
|
|
407
|
-
route = router.resolve(route);
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
if (i18nConfig.hashMode && toLocale && toLocale !== fromLocale) {
|
|
411
|
-
hashLocaleDefault = toLocale ?? fromLocale;
|
|
412
|
-
useCookie("hash-locale").value = hashLocaleDefault;
|
|
413
|
-
}
|
|
414
|
-
if (isNoPrefixStrategy(i18nConfig.strategy) && toLocale && toLocale !== fromLocale) {
|
|
415
|
-
noPrefixDefault = toLocale ?? fromLocale;
|
|
416
|
-
useCookie("no-prefix-locale").value = noPrefixDefault;
|
|
417
|
-
}
|
|
418
|
-
switchLocale(fromLocale, toLocale ?? fromLocale, route, router, i18nConfig, i18nRouteParams.value);
|
|
184
|
+
return routeService.switchLocaleLogic(toLocale ?? routeService.getCurrentLocale(), unref(i18nRouteParams.value), route);
|
|
419
185
|
},
|
|
420
186
|
localeRoute: (to, locale) => {
|
|
421
|
-
|
|
422
|
-
const fromLocale = getCurrentLocale(currentRoute, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
423
|
-
const currentLocale = locale ?? fromLocale;
|
|
424
|
-
if (typeof to === "string") {
|
|
425
|
-
if (currentLocale !== i18nConfig.defaultLocale || withPrefixStrategy(i18nConfig.strategy)) {
|
|
426
|
-
const currentRoute2 = router.currentRoute.value;
|
|
427
|
-
const fromLocale2 = getCurrentLocale(currentRoute2, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
428
|
-
to = router.resolve("/" + fromLocale2 + to);
|
|
429
|
-
} else {
|
|
430
|
-
to = router.resolve(to);
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
const route = router.currentRoute.value;
|
|
434
|
-
return getLocalizedRoute(to, router, route, i18nConfig, currentLocale, hashLocaleDefault, noPrefixDefault);
|
|
187
|
+
return routeService.resolveLocalizedRoute(to, locale);
|
|
435
188
|
},
|
|
436
189
|
localePath: (to, locale) => {
|
|
437
|
-
const
|
|
438
|
-
const localeRoute = getLocalizedRoute(to, router, route, i18nConfig, locale, hashLocaleDefault, noPrefixDefault);
|
|
190
|
+
const localeRoute = routeService.resolveLocalizedRoute(to, locale);
|
|
439
191
|
if (typeof localeRoute === "string") {
|
|
440
192
|
return localeRoute;
|
|
441
193
|
}
|
|
@@ -453,6 +205,14 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
453
205
|
Object.entries(provideData).map(([key, value]) => [`$${key}`, value])
|
|
454
206
|
);
|
|
455
207
|
provideData.i18n = { ...provideData, ...$provideData };
|
|
208
|
+
nuxtApp.vueApp.config.globalProperties.$t = provideData.t;
|
|
209
|
+
nuxtApp.vueApp.config.globalProperties.$ts = provideData.ts;
|
|
210
|
+
nuxtApp.vueApp.config.globalProperties.$tc = provideData.tc;
|
|
211
|
+
nuxtApp.vueApp.config.globalProperties.$tn = provideData.tn;
|
|
212
|
+
nuxtApp.vueApp.config.globalProperties.$td = provideData.td;
|
|
213
|
+
nuxtApp.vueApp.config.globalProperties.$tdr = provideData.tdr;
|
|
214
|
+
nuxtApp.vueApp.config.globalProperties.$switchLocale = provideData.switchLocale;
|
|
215
|
+
nuxtApp.vueApp.config.globalProperties.$switchLocaleRoute = provideData.switchLocaleRoute;
|
|
456
216
|
return {
|
|
457
217
|
provide: provideData
|
|
458
218
|
};
|
|
@@ -2,11 +2,11 @@ declare const _default: import("nuxt/app").Plugin<{
|
|
|
2
2
|
defineI18nRoute: (routeDefinition: {
|
|
3
3
|
locales?: string[] | Record<string, Record<string, string>>;
|
|
4
4
|
localeRoutes?: Record<string, string>;
|
|
5
|
-
}) => Promise<void
|
|
5
|
+
}) => Promise<void>;
|
|
6
6
|
}> & import("nuxt/app").ObjectPlugin<{
|
|
7
7
|
defineI18nRoute: (routeDefinition: {
|
|
8
8
|
locales?: string[] | Record<string, Record<string, string>>;
|
|
9
9
|
localeRoutes?: Record<string, string>;
|
|
10
|
-
}) => Promise<void
|
|
10
|
+
}) => Promise<void>;
|
|
11
11
|
}>;
|
|
12
12
|
export default _default;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { isPrefixStrategy } from "
|
|
2
|
-
import { defineNuxtPlugin, navigateTo,
|
|
3
|
-
import { useRoute, useRouter } from "#imports";
|
|
4
|
-
export default defineNuxtPlugin(async (
|
|
1
|
+
import { isNoPrefixStrategy, isPrefixStrategy } from "nuxt-i18n-micro-core";
|
|
2
|
+
import { defineNuxtPlugin, navigateTo, useRuntimeConfig } from "#app";
|
|
3
|
+
import { unref, useRoute, useRouter, useNuxtApp, watch, computed, onUnmounted } from "#imports";
|
|
4
|
+
export default defineNuxtPlugin(async (nuxtApp) => {
|
|
5
5
|
const config = useRuntimeConfig();
|
|
6
6
|
const route = useRoute();
|
|
7
7
|
const router = useRouter();
|
|
@@ -18,7 +18,7 @@ export default defineNuxtPlugin(async (_nuxtApp) => {
|
|
|
18
18
|
return {};
|
|
19
19
|
};
|
|
20
20
|
const handleRedirect = async (to) => {
|
|
21
|
-
const currentLocale = (
|
|
21
|
+
const currentLocale = nuxtApp.$getLocale().toString();
|
|
22
22
|
const { name } = to;
|
|
23
23
|
let defaultRouteName = name?.toString().replace("localized-", "").replace(new RegExp(`-${currentLocale}$`), "");
|
|
24
24
|
if (!to.params.locale) {
|
|
@@ -31,7 +31,9 @@ export default defineNuxtPlugin(async (_nuxtApp) => {
|
|
|
31
31
|
return;
|
|
32
32
|
}
|
|
33
33
|
const newParams = { ...to.params };
|
|
34
|
-
|
|
34
|
+
if (!isNoPrefixStrategy(i18nConfig.strategy)) {
|
|
35
|
+
newParams.locale = i18nConfig.defaultLocale;
|
|
36
|
+
}
|
|
35
37
|
return navigateTo({ name: defaultRouteName, params: newParams }, { redirectCode: 301, external: true });
|
|
36
38
|
}
|
|
37
39
|
};
|
|
@@ -49,33 +51,31 @@ export default defineNuxtPlugin(async (_nuxtApp) => {
|
|
|
49
51
|
}
|
|
50
52
|
});
|
|
51
53
|
const defineI18nRoute = async (routeDefinition) => {
|
|
52
|
-
const
|
|
54
|
+
const { $getLocale } = useNuxtApp();
|
|
55
|
+
let currentLocale = computed(() => $getLocale());
|
|
53
56
|
const normalizedLocales = normalizeLocales(routeDefinition.locales);
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const resolvedRoute = router.resolve({ name: defaultRouteName });
|
|
63
|
-
const newParams = { ...route.params };
|
|
64
|
-
delete newParams.locale;
|
|
65
|
-
if (isPrefixStrategy(i18nConfig.strategy)) {
|
|
66
|
-
if (router.hasRoute(`localized-${defaultRouteName}-${currentLocale}`)) {
|
|
67
|
-
defaultRouteName = `localized-${defaultRouteName}-${currentLocale}`;
|
|
68
|
-
} else {
|
|
69
|
-
defaultRouteName = `localized-${defaultRouteName}`;
|
|
57
|
+
const updateTranslations = () => {
|
|
58
|
+
const currentLocaleValue = unref(currentLocale);
|
|
59
|
+
if (currentLocaleValue && Object.values(normalizedLocales).length) {
|
|
60
|
+
if (normalizedLocales[currentLocaleValue]) {
|
|
61
|
+
const translation = normalizedLocales[currentLocaleValue];
|
|
62
|
+
const { $mergeGlobalTranslations } = useNuxtApp();
|
|
63
|
+
if ($mergeGlobalTranslations) {
|
|
64
|
+
$mergeGlobalTranslations(translation);
|
|
70
65
|
}
|
|
71
|
-
if (!router.hasRoute(defaultRouteName)) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
newParams.locale = i18nConfig.defaultLocale;
|
|
75
|
-
newParams.name = defaultRouteName;
|
|
76
66
|
}
|
|
77
|
-
return router.push(resolvedRoute);
|
|
78
67
|
}
|
|
68
|
+
};
|
|
69
|
+
updateTranslations();
|
|
70
|
+
if (import.meta.client) {
|
|
71
|
+
let stopWatcher = watch(currentLocale, updateTranslations);
|
|
72
|
+
onUnmounted(() => {
|
|
73
|
+
if (stopWatcher) {
|
|
74
|
+
stopWatcher();
|
|
75
|
+
currentLocale = null;
|
|
76
|
+
stopWatcher = null;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
79
|
}
|
|
80
80
|
};
|
|
81
81
|
return {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isPrefixStrategy, isNoPrefixStrategy } from "
|
|
1
|
+
import { isPrefixStrategy, isNoPrefixStrategy } from "nuxt-i18n-micro-core";
|
|
2
2
|
import { defineNuxtPlugin, useCookie, useRequestHeaders, navigateTo } from "#app";
|
|
3
3
|
import { useRoute, useRouter } from "#imports";
|
|
4
4
|
const parseAcceptLanguage = (acceptLanguage) => acceptLanguage.split(",").map((entry) => entry.split(";")[0].trim());
|
|
@@ -42,8 +42,10 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
42
42
|
const newRouteName = isPrefixStrategy(i18nConfig.strategy) || newLocale !== defaultLocale ? `localized-${routeName}` : routeName;
|
|
43
43
|
const newParams = { ...route.params };
|
|
44
44
|
delete newParams.locale;
|
|
45
|
-
if (
|
|
46
|
-
|
|
45
|
+
if (!isNoPrefixStrategy(i18nConfig.strategy)) {
|
|
46
|
+
if (isPrefixStrategy(i18nConfig.strategy) || newLocale !== defaultLocale) {
|
|
47
|
+
newParams.locale = newLocale;
|
|
48
|
+
}
|
|
47
49
|
}
|
|
48
50
|
const newRoute = router.resolve({
|
|
49
51
|
name: newRouteName,
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { H3Event } from 'h3';
|
|
2
|
-
import type { Params } from '
|
|
2
|
+
import type { Params } from 'nuxt-i18n-micro-types';
|
|
3
3
|
export declare const useTranslationServerMiddleware: (event: H3Event, defaultLocale?: string, currentLocale?: string) => Promise<(key: string, params?: Params, defaultValue?: string) => string>;
|
package/dist/types.d.mts
CHANGED
|
@@ -4,4 +4,4 @@ declare module '@nuxt/schema' {
|
|
|
4
4
|
interface NuxtHooks extends ModuleHooks {}
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
-
export { type Getter, type GlobalLocaleRoutes, type Locale, type LocaleCode, type PluralFunc } from './module.js'
|
|
7
|
+
export { type Getter, type GlobalLocaleRoutes, type Locale, type LocaleCode, type ModuleOptions, type PluralFunc, type Strategies } from './module.js'
|
package/dist/types.d.ts
CHANGED
|
@@ -4,4 +4,4 @@ declare module '@nuxt/schema' {
|
|
|
4
4
|
interface NuxtHooks extends ModuleHooks {}
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
-
export { type Getter, type GlobalLocaleRoutes, type Locale, type LocaleCode, type PluralFunc } from './module'
|
|
7
|
+
export { type Getter, type GlobalLocaleRoutes, type Locale, type LocaleCode, type ModuleOptions, type PluralFunc, type Strategies } from './module'
|
package/dist/utils.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NuxtPage } from '@nuxt/schema';
|
|
2
|
-
import {
|
|
2
|
+
import { LocaleCode, Locale } from 'nuxt-i18n-micro-types';
|
|
3
3
|
|
|
4
4
|
declare function extractLocaleRoutes(content: string, filePath: string): Record<string, string> | null;
|
|
5
5
|
declare function validateDefineI18nRouteConfig(obj: Record<LocaleCode, Record<string, string>>): boolean;
|
|
@@ -11,5 +11,6 @@ declare const buildRouteName: (baseName: string, localeCode: string, isCustom: b
|
|
|
11
11
|
declare const shouldAddLocalePrefix: (locale: string, defaultLocale: Locale, addLocalePrefix: boolean, includeDefaultLocaleRoute: boolean) => boolean;
|
|
12
12
|
declare const isLocaleDefault: (locale: string | Locale, defaultLocale: Locale, includeDefaultLocaleRoute: boolean) => boolean;
|
|
13
13
|
declare const buildFullPath: (locale: string | string[], basePath: string, customRegex?: string | RegExp) => string;
|
|
14
|
+
declare const buildFullPathNoPrefix: (basePath: string) => string;
|
|
14
15
|
|
|
15
|
-
export { buildFullPath, buildRouteName, cloneArray, extractLocaleRoutes, isLocaleDefault, isPageRedirectOnly, normalizePath, removeLeadingSlash, shouldAddLocalePrefix, validateDefineI18nRouteConfig };
|
|
16
|
+
export { buildFullPath, buildFullPathNoPrefix, buildRouteName, cloneArray, extractLocaleRoutes, isLocaleDefault, isPageRedirectOnly, normalizePath, removeLeadingSlash, shouldAddLocalePrefix, validateDefineI18nRouteConfig };
|