nuxt-i18n-micro 1.62.0 → 1.63.1
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 +2 -2
- package/dist/client/404.html +2 -2
- package/dist/client/_nuxt/builds/latest.json +1 -1
- package/dist/client/_nuxt/builds/meta/0cb6df0a-98e0-4304-bb61-9075b6e1735c.json +1 -0
- package/dist/client/index.html +2 -2
- package/dist/module.json +1 -1
- package/dist/module.mjs +2 -0
- package/dist/runtime/composables/useLocaleHead.js +7 -2
- package/dist/runtime/plugins/01.plugin.d.ts +3 -3
- package/dist/runtime/plugins/01.plugin.js +118 -104
- package/dist/runtime/translation-server-middleware.js +4 -5
- package/package.json +3 -3
- package/dist/client/_nuxt/builds/meta/c8228d06-262a-456f-a053-aa7994ec21bb.json +0 -1
package/dist/client/200.html
CHANGED
|
@@ -8,5 +8,5 @@
|
|
|
8
8
|
<link rel="prefetch" as="script" crossorigin href="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/AXE8DZQI.js">
|
|
9
9
|
<link rel="prefetch" as="style" crossorigin href="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/error-500.EgQ3RNLZ.css">
|
|
10
10
|
<link rel="prefetch" as="script" crossorigin href="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/D9gx-VEt.js">
|
|
11
|
-
<script type="module" src="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/Ddb0iCRU.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" data-nuxt-data="nuxt-app" data-ssr="false" id="__NUXT_DATA__">[{"prerenderedAt":1,"serverRendered":2},
|
|
12
|
-
<script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__NUXT_DEVTOOLS_I18N_BASE__/",buildId:"
|
|
11
|
+
<script type="module" src="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/Ddb0iCRU.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" data-nuxt-data="nuxt-app" data-ssr="false" id="__NUXT_DATA__">[{"prerenderedAt":1,"serverRendered":2},1737032181530,false]</script>
|
|
12
|
+
<script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__NUXT_DEVTOOLS_I18N_BASE__/",buildId:"0cb6df0a-98e0-4304-bb61-9075b6e1735c",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
|
package/dist/client/404.html
CHANGED
|
@@ -8,5 +8,5 @@
|
|
|
8
8
|
<link rel="prefetch" as="script" crossorigin href="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/AXE8DZQI.js">
|
|
9
9
|
<link rel="prefetch" as="style" crossorigin href="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/error-500.EgQ3RNLZ.css">
|
|
10
10
|
<link rel="prefetch" as="script" crossorigin href="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/D9gx-VEt.js">
|
|
11
|
-
<script type="module" src="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/Ddb0iCRU.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" data-nuxt-data="nuxt-app" data-ssr="false" id="__NUXT_DATA__">[{"prerenderedAt":1,"serverRendered":2},
|
|
12
|
-
<script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__NUXT_DEVTOOLS_I18N_BASE__/",buildId:"
|
|
11
|
+
<script type="module" src="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/Ddb0iCRU.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" data-nuxt-data="nuxt-app" data-ssr="false" id="__NUXT_DATA__">[{"prerenderedAt":1,"serverRendered":2},1737032181531,false]</script>
|
|
12
|
+
<script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__NUXT_DEVTOOLS_I18N_BASE__/",buildId:"0cb6df0a-98e0-4304-bb61-9075b6e1735c",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"id":"
|
|
1
|
+
{"id":"0cb6df0a-98e0-4304-bb61-9075b6e1735c","timestamp":1737032176225}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"id":"0cb6df0a-98e0-4304-bb61-9075b6e1735c","timestamp":1737032176225,"matcher":{"static":{},"wildcard":{},"dynamic":{}},"prerendered":[]}
|
package/dist/client/index.html
CHANGED
|
@@ -8,5 +8,5 @@
|
|
|
8
8
|
<link rel="prefetch" as="script" crossorigin href="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/AXE8DZQI.js">
|
|
9
9
|
<link rel="prefetch" as="style" crossorigin href="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/error-500.EgQ3RNLZ.css">
|
|
10
10
|
<link rel="prefetch" as="script" crossorigin href="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/D9gx-VEt.js">
|
|
11
|
-
<script type="module" src="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/Ddb0iCRU.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" data-nuxt-data="nuxt-app" data-ssr="false" id="__NUXT_DATA__">[{"prerenderedAt":1,"serverRendered":2},
|
|
12
|
-
<script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__NUXT_DEVTOOLS_I18N_BASE__/",buildId:"
|
|
11
|
+
<script type="module" src="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/Ddb0iCRU.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" data-nuxt-data="nuxt-app" data-ssr="false" id="__NUXT_DATA__">[{"prerenderedAt":1,"serverRendered":2},1737032181531,false]</script>
|
|
12
|
+
<script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__NUXT_DEVTOOLS_I18N_BASE__/",buildId:"0cb6df0a-98e0-4304-bb61-9075b6e1735c",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -471,6 +471,8 @@ const module = defineNuxtModule({
|
|
|
471
471
|
plural: void 0,
|
|
472
472
|
locales: localeManager.locales ?? [],
|
|
473
473
|
meta: options.meta ?? true,
|
|
474
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
475
|
+
// @ts-ignore
|
|
474
476
|
metaBaseUrl: options.metaBaseUrl ?? void 0,
|
|
475
477
|
define: options.define ?? true,
|
|
476
478
|
disableWatcher: options.disableWatcher ?? false,
|
|
@@ -12,6 +12,7 @@ export const useLocaleHead = ({ addDirAttribute = true, identifierAttribute = "i
|
|
|
12
12
|
const { $getLocales, $getLocale } = useNuxtApp();
|
|
13
13
|
const route = useRoute();
|
|
14
14
|
const locale = unref($getLocale());
|
|
15
|
+
const locales = unref($getLocales());
|
|
15
16
|
const routeName = (route.name ?? "").toString();
|
|
16
17
|
const currentLocale = unref($getLocales().find((loc) => loc.code === locale));
|
|
17
18
|
if (!currentLocale) {
|
|
@@ -22,14 +23,18 @@ export const useLocaleHead = ({ addDirAttribute = true, identifierAttribute = "i
|
|
|
22
23
|
let fullPath = unref(route.fullPath);
|
|
23
24
|
let ogUrl = joinURL(unref(baseUrl), fullPath);
|
|
24
25
|
let indexUrl = joinURL(unref(baseUrl));
|
|
26
|
+
if (!fullPath.startsWith("/")) {
|
|
27
|
+
fullPath = `/${fullPath}`;
|
|
28
|
+
}
|
|
25
29
|
if (!ogUrl.endsWith("/")) {
|
|
26
30
|
ogUrl += "/";
|
|
27
31
|
}
|
|
28
32
|
if (!indexUrl.endsWith("/")) {
|
|
29
33
|
indexUrl += "/";
|
|
30
34
|
}
|
|
31
|
-
|
|
32
|
-
|
|
35
|
+
const matchedLocale = locales.find((locale2) => fullPath.startsWith(`/${locale2.code}`));
|
|
36
|
+
if (routeName.startsWith("localized-") && matchedLocale) {
|
|
37
|
+
fullPath = fullPath.slice(matchedLocale.code.length + 1);
|
|
33
38
|
ogUrl = joinURL(unref(baseUrl), locale, fullPath);
|
|
34
39
|
}
|
|
35
40
|
metaObject.value = {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { RouteLocationAsPath, RouteLocationAsRelative, RouteLocationAsString, RouteLocationNormalizedLoaded, RouteLocationRaw, RouteLocationResolved, RouteLocationResolvedGeneric } from 'vue-router';
|
|
2
2
|
import type { Translation, Translations } from 'nuxt-i18n-micro-core';
|
|
3
|
-
import type {
|
|
3
|
+
import type { Locale, I18nRouteParams, Params } from '../../types.js';
|
|
4
4
|
declare const _default: import("nuxt/app").Plugin<{
|
|
5
5
|
i18n: undefined;
|
|
6
6
|
__micro: boolean;
|
|
@@ -21,7 +21,7 @@ declare const _default: import("nuxt/app").Plugin<{
|
|
|
21
21
|
switchLocaleRoute: (toLocale: string) => string | import("vue-router").RouteLocationAsRelativeGeneric | import("vue-router").RouteLocationAsPathGeneric;
|
|
22
22
|
switchLocalePath: (toLocale: string) => string;
|
|
23
23
|
switchLocale: (toLocale: string) => void;
|
|
24
|
-
switchRoute: (
|
|
24
|
+
switchRoute: (route: RouteLocationNormalizedLoaded | RouteLocationResolvedGeneric | string, toLocale?: string) => void;
|
|
25
25
|
localeRoute: (to: RouteLocationAsString | RouteLocationAsRelative | RouteLocationAsPath | string, locale?: string) => RouteLocationResolved;
|
|
26
26
|
localePath: (to: RouteLocationAsString | RouteLocationAsRelative | RouteLocationAsPath, locale?: string) => string;
|
|
27
27
|
setI18nRouteParams: (value: I18nRouteParams) => I18nRouteParams;
|
|
@@ -45,7 +45,7 @@ declare const _default: import("nuxt/app").Plugin<{
|
|
|
45
45
|
switchLocaleRoute: (toLocale: string) => string | import("vue-router").RouteLocationAsRelativeGeneric | import("vue-router").RouteLocationAsPathGeneric;
|
|
46
46
|
switchLocalePath: (toLocale: string) => string;
|
|
47
47
|
switchLocale: (toLocale: string) => void;
|
|
48
|
-
switchRoute: (
|
|
48
|
+
switchRoute: (route: RouteLocationNormalizedLoaded | RouteLocationResolvedGeneric | string, toLocale?: string) => void;
|
|
49
49
|
localeRoute: (to: RouteLocationAsString | RouteLocationAsRelative | RouteLocationAsPath | string, locale?: string) => RouteLocationResolved;
|
|
50
50
|
localePath: (to: RouteLocationAsString | RouteLocationAsRelative | RouteLocationAsPath, locale?: string) => string;
|
|
51
51
|
setI18nRouteParams: (value: I18nRouteParams) => I18nRouteParams;
|
|
@@ -1,12 +1,21 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useTranslationHelper, interpolate } from "nuxt-i18n-micro-core";
|
|
2
2
|
import { isNoPrefixStrategy, withPrefixStrategy } from "../helpers.js";
|
|
3
3
|
import { defineNuxtPlugin, useRuntimeConfig } from "#app";
|
|
4
|
-
import {
|
|
4
|
+
import { useRouter, useCookie, useState, navigateTo } from "#imports";
|
|
5
5
|
import { plural } from "#build/i18n.plural.mjs";
|
|
6
6
|
const i18nHelper = useTranslationHelper();
|
|
7
7
|
const isDev = process.env.NODE_ENV !== "production";
|
|
8
|
-
function
|
|
9
|
-
|
|
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);
|
|
10
19
|
const checkLocale = i18nConfig.locales?.find((l) => l.code === currentLocaleCode);
|
|
11
20
|
if (!checkLocale) {
|
|
12
21
|
return null;
|
|
@@ -35,10 +44,6 @@ function switchLocaleRoute(fromLocale, toLocale, route, router, i18nConfig, i18n
|
|
|
35
44
|
if (router.hasRoute(`localized-${routeName}-${toLocale}`)) {
|
|
36
45
|
const newParams2 = { ...route.params, ...i18nRouteParams?.[toLocale] };
|
|
37
46
|
newParams2.locale = toLocale;
|
|
38
|
-
if (i18nConfig.hashMode) {
|
|
39
|
-
const userLocaleCookie = useCookie("hash-locale");
|
|
40
|
-
userLocaleCookie.value = toLocale;
|
|
41
|
-
}
|
|
42
47
|
const newRoute2 = {
|
|
43
48
|
name: `localized-${routeName}-${toLocale}`,
|
|
44
49
|
params: newParams2
|
|
@@ -61,10 +66,6 @@ function switchLocaleRoute(fromLocale, toLocale, route, router, i18nConfig, i18n
|
|
|
61
66
|
newParams.locale = toLocale;
|
|
62
67
|
}
|
|
63
68
|
}
|
|
64
|
-
if (i18nConfig.hashMode) {
|
|
65
|
-
const userLocaleCookie = useCookie("hash-locale");
|
|
66
|
-
userLocaleCookie.value = toLocale;
|
|
67
|
-
}
|
|
68
69
|
const newRoute = {
|
|
69
70
|
name: newRouteName,
|
|
70
71
|
params: newParams
|
|
@@ -80,11 +81,6 @@ function switchLocale(fromLocale, toLocale, route, router, i18nConfig, i18nRoute
|
|
|
80
81
|
console.warn(`Locale ${toLocale} is not available`);
|
|
81
82
|
return Promise.reject(`Locale ${toLocale} is not available`);
|
|
82
83
|
}
|
|
83
|
-
if (isNoPrefixStrategy(i18nConfig.strategy)) {
|
|
84
|
-
const userLocaleCookie = useCookie("no-prefix-locale");
|
|
85
|
-
userLocaleCookie.value = toLocale;
|
|
86
|
-
}
|
|
87
|
-
i18nHelper.setLocale(toLocale);
|
|
88
84
|
const switchedRoute = switchLocaleRoute(
|
|
89
85
|
fromLocale,
|
|
90
86
|
toLocale,
|
|
@@ -101,7 +97,7 @@ function switchLocale(fromLocale, toLocale, route, router, i18nConfig, i18nRoute
|
|
|
101
97
|
}
|
|
102
98
|
return router.push(switchedRoute);
|
|
103
99
|
}
|
|
104
|
-
function getLocalizedRoute(to, router, i18nConfig, locale) {
|
|
100
|
+
function getLocalizedRoute(to, router, route, i18nConfig, locale, hashLocale, noPrefixStrategy) {
|
|
105
101
|
const resolveParams = (to2) => {
|
|
106
102
|
const params = typeof to2 === "object" && "params" in to2 && typeof to2.params === "object" ? { ...to2.params } : {};
|
|
107
103
|
if (typeof to2 === "string") {
|
|
@@ -135,7 +131,7 @@ function getLocalizedRoute(to, router, i18nConfig, locale) {
|
|
|
135
131
|
});
|
|
136
132
|
}
|
|
137
133
|
}
|
|
138
|
-
const currentLocale = locale ||
|
|
134
|
+
const currentLocale = locale || getCurrentLocale(route, i18nConfig, hashLocale, noPrefixStrategy);
|
|
139
135
|
const selectRoute = router.resolve(to);
|
|
140
136
|
const routeName = getRouteName(selectRoute, currentLocale).replace(new RegExp(`-${i18nConfig.defaultLocale}$`), "");
|
|
141
137
|
if (!routeName || routeName === "") {
|
|
@@ -220,21 +216,21 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
220
216
|
const i18nConfig = config.public.i18nConfig;
|
|
221
217
|
const apiBaseUrl = i18nConfig.apiBaseUrl ?? "_locales";
|
|
222
218
|
const runtimeConfig = useRuntimeConfig();
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
if (currentNoPrefixDefault) {
|
|
230
|
-
i18nHelper.setLocale(currentNoPrefixDefault);
|
|
219
|
+
let hashLocaleDefault = null;
|
|
220
|
+
let noPrefixDefault = null;
|
|
221
|
+
if (i18nConfig.hashMode) {
|
|
222
|
+
hashLocaleDefault = await nuxtApp.runWithContext(() => {
|
|
223
|
+
return useCookie("hash-locale").value;
|
|
224
|
+
});
|
|
231
225
|
}
|
|
232
|
-
if (
|
|
233
|
-
|
|
226
|
+
if (isNoPrefixStrategy(i18nConfig.strategy)) {
|
|
227
|
+
noPrefixDefault = await nuxtApp.runWithContext(() => {
|
|
228
|
+
return useCookie("no-prefix-locale").value;
|
|
229
|
+
});
|
|
234
230
|
}
|
|
235
|
-
const loadTranslationsIfNeeded = async (routeName, path) => {
|
|
231
|
+
const loadTranslationsIfNeeded = async (locale, routeName, path) => {
|
|
236
232
|
try {
|
|
237
|
-
if (!i18nHelper.hasPageTranslation(routeName)) {
|
|
233
|
+
if (!i18nHelper.hasPageTranslation(locale, routeName)) {
|
|
238
234
|
let fRouteName = routeName;
|
|
239
235
|
if (i18nConfig.routesLocaleLinks && i18nConfig.routesLocaleLinks[fRouteName]) {
|
|
240
236
|
fRouteName = i18nConfig.routesLocaleLinks[fRouteName];
|
|
@@ -243,7 +239,6 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
243
239
|
console.warn(`[nuxt-i18n-next] The page name is missing in the path: ${path}. Please ensure that definePageMeta({ name: 'pageName' }) is set.`);
|
|
244
240
|
return;
|
|
245
241
|
}
|
|
246
|
-
const locale = i18nHelper.getLocale();
|
|
247
242
|
const url = `/${apiBaseUrl}/${fRouteName}/${locale}/data.json`.replace(/\/{2,}/g, "/");
|
|
248
243
|
const data = await $fetch(url, {
|
|
249
244
|
baseURL: runtimeConfig.app.baseURL,
|
|
@@ -251,30 +246,26 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
251
246
|
v: i18nConfig.dateBuild
|
|
252
247
|
}
|
|
253
248
|
});
|
|
254
|
-
await i18nHelper.loadPageTranslations(routeName, data ?? {});
|
|
249
|
+
await i18nHelper.loadPageTranslations(locale, routeName, data ?? {});
|
|
255
250
|
}
|
|
256
251
|
} catch (_error) {
|
|
257
252
|
}
|
|
258
253
|
};
|
|
259
254
|
async function loadGlobalTranslations(to) {
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
if (
|
|
263
|
-
await nuxtApp.runWithContext(() => {
|
|
264
|
-
|
|
265
|
-
i18nHelper.setLocale(locale);
|
|
266
|
-
return true;
|
|
255
|
+
let hashLocale = null;
|
|
256
|
+
let noPrefixLocale = null;
|
|
257
|
+
if (i18nConfig.hashMode) {
|
|
258
|
+
hashLocale = await nuxtApp.runWithContext(() => {
|
|
259
|
+
return useCookie("hash-locale").value;
|
|
267
260
|
});
|
|
268
261
|
}
|
|
269
|
-
if (i18nConfig.
|
|
270
|
-
await nuxtApp.runWithContext(() => {
|
|
271
|
-
|
|
272
|
-
i18nHelper.setLocale(locale);
|
|
273
|
-
return true;
|
|
262
|
+
if (isNoPrefixStrategy(i18nConfig.strategy)) {
|
|
263
|
+
noPrefixLocale = await nuxtApp.runWithContext(() => {
|
|
264
|
+
return useCookie("no-prefix-locale").value;
|
|
274
265
|
});
|
|
275
266
|
}
|
|
276
|
-
|
|
277
|
-
|
|
267
|
+
const locale = getCurrentLocale(to, i18nConfig, hashLocale, noPrefixLocale);
|
|
268
|
+
if (!i18nHelper.hasGeneralTranslation(locale)) {
|
|
278
269
|
const url = `/${apiBaseUrl}/general/${locale}/data.json`.replace(/\/{2,}/g, "/");
|
|
279
270
|
const data = await $fetch(url, {
|
|
280
271
|
baseURL: runtimeConfig.app.baseURL,
|
|
@@ -282,20 +273,20 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
282
273
|
v: i18nConfig.dateBuild
|
|
283
274
|
}
|
|
284
275
|
});
|
|
285
|
-
await i18nHelper.loadTranslations(data ?? {});
|
|
276
|
+
await i18nHelper.loadTranslations(locale, data ?? {});
|
|
286
277
|
}
|
|
287
278
|
if (!i18nConfig.disablePageLocales) {
|
|
288
|
-
const routeName = getRouteName(to,
|
|
289
|
-
await loadTranslationsIfNeeded(routeName, to.fullPath);
|
|
279
|
+
const routeName = getRouteName(to, locale);
|
|
280
|
+
await loadTranslationsIfNeeded(locale, routeName, to.fullPath);
|
|
290
281
|
}
|
|
291
282
|
await nuxtApp.callHook("i18n:register", (translations, selectedLocale) => {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
}, i18nHelper.getLocale());
|
|
283
|
+
const routeName = getRouteName(to, locale);
|
|
284
|
+
i18nHelper.mergeTranslation(selectedLocale ?? locale, routeName, translations, true);
|
|
285
|
+
}, locale);
|
|
296
286
|
}
|
|
287
|
+
const router = useRouter();
|
|
297
288
|
router.beforeEach(async (to, from, next) => {
|
|
298
|
-
if (to.
|
|
289
|
+
if (to.path !== from.path || isNoPrefixStrategy(i18nConfig.strategy)) {
|
|
299
290
|
await loadGlobalTranslations(to);
|
|
300
291
|
}
|
|
301
292
|
if (next) {
|
|
@@ -305,10 +296,10 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
305
296
|
await loadGlobalTranslations(router.currentRoute.value);
|
|
306
297
|
const getTranslation = (key, params, defaultValue) => {
|
|
307
298
|
if (!key) return "";
|
|
308
|
-
const
|
|
309
|
-
const
|
|
310
|
-
const routeName = getRouteName(
|
|
311
|
-
let value = i18nHelper.getTranslation(routeName, key);
|
|
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);
|
|
312
303
|
if (!value) {
|
|
313
304
|
if (isDev && import.meta.client) {
|
|
314
305
|
console.warn(`Not found '${key}' key in '${locale}' locale messages.`);
|
|
@@ -324,13 +315,14 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
324
315
|
const provideData = {
|
|
325
316
|
i18n: void 0,
|
|
326
317
|
__micro: true,
|
|
327
|
-
getLocale: () =>
|
|
328
|
-
getLocaleName: () => getCurrentName(i18nConfig),
|
|
318
|
+
getLocale: () => getCurrentLocale(router.currentRoute.value, i18nConfig, hashLocaleDefault, noPrefixDefault),
|
|
319
|
+
getLocaleName: () => getCurrentName(router.currentRoute.value, i18nConfig, hashLocaleDefault, noPrefixDefault),
|
|
329
320
|
defaultLocale: () => i18nConfig.defaultLocale,
|
|
330
321
|
getLocales: () => i18nConfig.locales || [],
|
|
331
|
-
getRouteName: (
|
|
332
|
-
const
|
|
333
|
-
|
|
322
|
+
getRouteName: (route, locale) => {
|
|
323
|
+
const selectedLocale = locale ?? getCurrentLocale(router.currentRoute.value, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
324
|
+
const selectedRoute = route ?? router.currentRoute.value;
|
|
325
|
+
return getRouteName(selectedRoute, selectedLocale);
|
|
334
326
|
},
|
|
335
327
|
t: getTranslation,
|
|
336
328
|
ts: (key, params, defaultValue) => {
|
|
@@ -338,41 +330,49 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
338
330
|
return value?.toString() ?? defaultValue ?? key;
|
|
339
331
|
},
|
|
340
332
|
tc: (key, params, defaultValue) => {
|
|
341
|
-
const
|
|
333
|
+
const route = router.currentRoute.value;
|
|
334
|
+
const currentLocale = getCurrentLocale(route, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
342
335
|
const { count, ..._params } = typeof params === "number" ? { count: params } : params;
|
|
343
|
-
return plural(key, Number.parseInt(count.toString()), _params,
|
|
336
|
+
return plural(key, Number.parseInt(count.toString()), _params, currentLocale, getTranslation) ?? defaultValue ?? key;
|
|
344
337
|
},
|
|
345
338
|
tn: (value, options) => {
|
|
346
|
-
const
|
|
339
|
+
const route = router.currentRoute.value;
|
|
340
|
+
const locale = getCurrentLocale(route, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
347
341
|
return formatNumber(value, locale, options);
|
|
348
342
|
},
|
|
349
343
|
td: (value, options) => {
|
|
350
|
-
const
|
|
344
|
+
const route = router.currentRoute.value;
|
|
345
|
+
const locale = getCurrentLocale(route, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
351
346
|
return formatDate(value, locale, options);
|
|
352
347
|
},
|
|
353
348
|
tdr(value, options) {
|
|
354
|
-
const
|
|
349
|
+
const route = router.currentRoute.value;
|
|
350
|
+
const locale = getCurrentLocale(route, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
355
351
|
return formatRelativeTime(value, locale, options);
|
|
356
352
|
},
|
|
357
353
|
has: (key) => {
|
|
358
354
|
return !!getTranslation(key);
|
|
359
355
|
},
|
|
360
356
|
mergeTranslations: (newTranslations) => {
|
|
361
|
-
const
|
|
362
|
-
const locale = (
|
|
363
|
-
const routeName = getRouteName(
|
|
364
|
-
i18nHelper.mergeTranslation(routeName, newTranslations);
|
|
357
|
+
const route = router.currentRoute.value;
|
|
358
|
+
const locale = getCurrentLocale(route, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
359
|
+
const routeName = getRouteName(route, locale);
|
|
360
|
+
i18nHelper.mergeTranslation(locale, routeName, newTranslations);
|
|
365
361
|
},
|
|
366
362
|
mergeGlobalTranslations: (newTranslations) => {
|
|
367
|
-
|
|
363
|
+
const route = router.currentRoute.value;
|
|
364
|
+
const locale = getCurrentLocale(route, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
365
|
+
i18nHelper.mergeGlobalTranslation(locale, newTranslations, true);
|
|
368
366
|
},
|
|
369
367
|
switchLocaleRoute: (toLocale) => {
|
|
370
|
-
const
|
|
371
|
-
|
|
368
|
+
const route = router.currentRoute.value;
|
|
369
|
+
const fromLocale = getCurrentLocale(route, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
370
|
+
return switchLocaleRoute(fromLocale, toLocale, route, router, i18nConfig, i18nRouteParams.value);
|
|
372
371
|
},
|
|
373
372
|
switchLocalePath: (toLocale) => {
|
|
374
|
-
const
|
|
375
|
-
const
|
|
373
|
+
const route = router.currentRoute.value;
|
|
374
|
+
const fromLocale = getCurrentLocale(route, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
375
|
+
const localeRoute = switchLocaleRoute(fromLocale, toLocale, route, router, i18nConfig, i18nRouteParams.value);
|
|
376
376
|
if (typeof localeRoute === "string") {
|
|
377
377
|
return localeRoute;
|
|
378
378
|
}
|
|
@@ -382,46 +382,60 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
382
382
|
return "";
|
|
383
383
|
},
|
|
384
384
|
switchLocale: (toLocale) => {
|
|
385
|
-
const
|
|
386
|
-
|
|
385
|
+
const route = router.currentRoute.value;
|
|
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);
|
|
387
396
|
},
|
|
388
|
-
switchRoute: (
|
|
397
|
+
switchRoute: (route, toLocale) => {
|
|
389
398
|
const currentRoute = router.currentRoute.value;
|
|
390
|
-
const fromLocale = (currentRoute
|
|
391
|
-
const
|
|
392
|
-
if (typeof
|
|
393
|
-
if (
|
|
394
|
-
|
|
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);
|
|
395
406
|
} else {
|
|
396
|
-
|
|
407
|
+
route = router.resolve(route);
|
|
397
408
|
}
|
|
398
409
|
}
|
|
399
|
-
if (
|
|
400
|
-
|
|
410
|
+
if (i18nConfig.hashMode && toLocale && toLocale !== fromLocale) {
|
|
411
|
+
hashLocaleDefault = toLocale ?? fromLocale;
|
|
412
|
+
useCookie("hash-locale").value = hashLocaleDefault;
|
|
401
413
|
}
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
i18nConfig,
|
|
408
|
-
i18nRouteParams.value
|
|
409
|
-
);
|
|
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);
|
|
410
419
|
},
|
|
411
420
|
localeRoute: (to, locale) => {
|
|
412
|
-
const
|
|
413
|
-
const
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
421
|
+
const currentRoute = router.currentRoute.value;
|
|
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);
|
|
417
429
|
} else {
|
|
418
430
|
to = router.resolve(to);
|
|
419
431
|
}
|
|
420
432
|
}
|
|
421
|
-
|
|
433
|
+
const route = router.currentRoute.value;
|
|
434
|
+
return getLocalizedRoute(to, router, route, i18nConfig, currentLocale, hashLocaleDefault, noPrefixDefault);
|
|
422
435
|
},
|
|
423
436
|
localePath: (to, locale) => {
|
|
424
|
-
const
|
|
437
|
+
const route = router.currentRoute.value;
|
|
438
|
+
const localeRoute = getLocalizedRoute(to, router, route, i18nConfig, locale, hashLocaleDefault, noPrefixDefault);
|
|
425
439
|
if (typeof localeRoute === "string") {
|
|
426
440
|
return localeRoute;
|
|
427
441
|
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { getQuery, getCookie } from "h3";
|
|
2
2
|
import { interpolate, useTranslationHelper } from "nuxt-i18n-micro-core";
|
|
3
|
-
const i18nHelper = useTranslationHelper();
|
|
4
3
|
async function fetchTranslations(locale) {
|
|
5
4
|
try {
|
|
6
5
|
const translations = await $fetch(`/_locales/general/${locale}/data.json`);
|
|
@@ -11,14 +10,14 @@ async function fetchTranslations(locale) {
|
|
|
11
10
|
}
|
|
12
11
|
}
|
|
13
12
|
export const useTranslationServerMiddleware = async (event, defaultLocale, currentLocale) => {
|
|
13
|
+
const { getTranslation, loadTranslations, hasGeneralTranslation } = useTranslationHelper();
|
|
14
14
|
const locale = (currentLocale || event.context.params?.locale || getQuery(event)?.locale || getCookie(event, "user-locale") || event.headers.get("accept-language")?.split(",")[0] || defaultLocale || "en").toString();
|
|
15
|
-
|
|
16
|
-
if (!i18nHelper.hasGeneralTranslation()) {
|
|
15
|
+
if (!hasGeneralTranslation(locale)) {
|
|
17
16
|
const translations = await fetchTranslations(locale);
|
|
18
|
-
await
|
|
17
|
+
await loadTranslations(locale, translations);
|
|
19
18
|
}
|
|
20
19
|
function t(key, params, defaultValue) {
|
|
21
|
-
let translation =
|
|
20
|
+
let translation = getTranslation(locale, "index", key);
|
|
22
21
|
if (!translation) {
|
|
23
22
|
translation = defaultValue || key;
|
|
24
23
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-i18n-micro",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.63.1",
|
|
4
4
|
"description": "Nuxt I18n Micro is a lightweight, high-performance internationalization module for Nuxt, designed to handle multi-language support with minimal overhead, fast build times, and efficient runtime performance.",
|
|
5
5
|
"repository": "s00d/nuxt-i18n-micro",
|
|
6
6
|
"license": "MIT",
|
|
@@ -53,8 +53,8 @@
|
|
|
53
53
|
"globby": "^14.0.2",
|
|
54
54
|
"sirv": "^2.0.4",
|
|
55
55
|
"ufo": "^1.5.4",
|
|
56
|
-
"nuxt-i18n-micro-core": "1.0.
|
|
57
|
-
"nuxt-i18n-micro-test-utils": "1.0.
|
|
56
|
+
"nuxt-i18n-micro-core": "1.0.12",
|
|
57
|
+
"nuxt-i18n-micro-test-utils": "1.0.5"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
60
|
"@nuxt/devtools": "^1.7.0",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"id":"c8228d06-262a-456f-a053-aa7994ec21bb","timestamp":1737022232581,"matcher":{"static":{},"wildcard":{},"dynamic":{}},"prerendered":[]}
|