nuxt-i18n-micro 1.64.0 → 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 +13 -12
- 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 -336
- package/dist/runtime/plugins/03.define.d.ts +2 -2
- package/dist/runtime/plugins/03.define.js +26 -28
- package/dist/runtime/plugins/04.auto-detect.js +1 -1
- 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 +1 -1
- package/dist/utils.d.ts +1 -1
- package/package.json +5 -4
- package/dist/client/_nuxt/builds/meta/17ca5a6a-83c6-4759-8a74-a56490050218.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,244 +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
|
-
if (!isNoPrefixStrategy(i18nConfig.strategy)) 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 (!isNoPrefixStrategy(i18nConfig.strategy)) {
|
|
66
|
-
if (toLocale !== i18nConfig.defaultLocale || withPrefixStrategy(i18nConfig.strategy)) {
|
|
67
|
-
newParams.locale = toLocale;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
const newRoute = {
|
|
72
|
-
name: newRouteName,
|
|
73
|
-
params: newParams
|
|
74
|
-
};
|
|
75
|
-
if (isNoPrefixStrategy(i18nConfig.strategy)) {
|
|
76
|
-
i18nConfig.locales?.forEach((locale, _index) => {
|
|
77
|
-
if (newRoute.name.endsWith(`-${locale.code}`)) {
|
|
78
|
-
newRoute.name = newRoute.name.slice(0, -locale.code - 1);
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
if (currentLocale?.baseUrl) {
|
|
83
|
-
return getFullPathWithBaseUrl(newRoute);
|
|
84
|
-
}
|
|
85
|
-
return newRoute;
|
|
86
|
-
}
|
|
87
|
-
function switchLocale(fromLocale, toLocale, route, router, i18nConfig, i18nRouteParams) {
|
|
88
|
-
const checkLocale = i18nConfig.locales?.find((l) => l.code === toLocale);
|
|
89
|
-
if (!checkLocale) {
|
|
90
|
-
console.warn(`Locale ${toLocale} is not available`);
|
|
91
|
-
return Promise.reject(`Locale ${toLocale} is not available`);
|
|
92
|
-
}
|
|
93
|
-
const switchedRoute = switchLocaleRoute(
|
|
94
|
-
fromLocale,
|
|
95
|
-
toLocale,
|
|
96
|
-
route,
|
|
97
|
-
router,
|
|
98
|
-
i18nConfig,
|
|
99
|
-
i18nRouteParams
|
|
100
|
-
);
|
|
101
|
-
if (typeof switchedRoute === "string" && switchedRoute.startsWith("http")) {
|
|
102
|
-
return navigateTo(switchedRoute, { redirectCode: 200, external: true });
|
|
103
|
-
}
|
|
104
|
-
if (isNoPrefixStrategy(i18nConfig.strategy)) {
|
|
105
|
-
switchedRoute.force = true;
|
|
106
|
-
}
|
|
107
|
-
return router.push(switchedRoute);
|
|
108
|
-
}
|
|
109
|
-
function getLocalizedRoute(to, router, route, i18nConfig, locale, hashLocale, noPrefixStrategy) {
|
|
110
|
-
const resolveParams = (to2) => {
|
|
111
|
-
const params = typeof to2 === "object" && "params" in to2 && typeof to2.params === "object" ? { ...to2.params } : {};
|
|
112
|
-
if (typeof to2 === "string") {
|
|
113
|
-
const resolved = router.resolve(to2);
|
|
114
|
-
if (resolved && resolved.params) {
|
|
115
|
-
Object.assign(params, resolved.params);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
return params;
|
|
119
|
-
};
|
|
120
|
-
if (withPrefixStrategy(i18nConfig.strategy)) {
|
|
121
|
-
const defaultLocale = i18nConfig.defaultLocale;
|
|
122
|
-
let resolvedTo = to;
|
|
123
|
-
if (typeof to === "string") {
|
|
124
|
-
resolvedTo = router.resolve("/" + defaultLocale + to);
|
|
125
|
-
}
|
|
126
|
-
const defaultRouteName = getRouteName(resolvedTo, defaultLocale);
|
|
127
|
-
const newParams2 = resolveParams(resolvedTo);
|
|
128
|
-
if (!isNoPrefixStrategy(i18nConfig.strategy)) newParams2.locale = defaultLocale;
|
|
129
|
-
if (router.hasRoute(`localized-${defaultRouteName}`)) {
|
|
130
|
-
to = router.resolve({
|
|
131
|
-
name: `localized-${defaultRouteName}`,
|
|
132
|
-
query: resolvedTo.query,
|
|
133
|
-
params: newParams2
|
|
134
|
-
});
|
|
135
|
-
} else if (router.hasRoute(`localized-${defaultRouteName}-${defaultLocale}`)) {
|
|
136
|
-
to = router.resolve({
|
|
137
|
-
name: `localized-${defaultRouteName}-${defaultLocale}`,
|
|
138
|
-
query: resolvedTo.query,
|
|
139
|
-
params: newParams2
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
const currentLocale = locale || getCurrentLocale(route, i18nConfig, hashLocale, noPrefixStrategy);
|
|
144
|
-
const selectRoute = router.resolve(to);
|
|
145
|
-
const routeName = getRouteName(selectRoute, currentLocale).replace(new RegExp(`-${i18nConfig.defaultLocale}$`), "");
|
|
146
|
-
if (!routeName || routeName === "") {
|
|
147
|
-
const resolved = router.resolve(to);
|
|
148
|
-
let url = resolved.path.replace(new RegExp(`^/${currentLocale}/`), "/");
|
|
149
|
-
if (currentLocale !== i18nConfig.defaultLocale || withPrefixStrategy(i18nConfig.strategy)) {
|
|
150
|
-
url = "/" + currentLocale + url;
|
|
151
|
-
}
|
|
152
|
-
return router.resolve({
|
|
153
|
-
path: url,
|
|
154
|
-
query: selectRoute.query,
|
|
155
|
-
hash: selectRoute.hash
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
if (router.hasRoute(`localized-${routeName}-${currentLocale}`)) {
|
|
159
|
-
const newParams2 = resolveParams(selectRoute);
|
|
160
|
-
if (!isNoPrefixStrategy(i18nConfig.strategy)) newParams2.locale = currentLocale;
|
|
161
|
-
return router.resolve({
|
|
162
|
-
name: `localized-${routeName}-${currentLocale}`,
|
|
163
|
-
params: newParams2,
|
|
164
|
-
query: selectRoute.query,
|
|
165
|
-
hash: selectRoute.hash
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
const newRouteName = currentLocale !== i18nConfig.defaultLocale || withPrefixStrategy(i18nConfig.strategy) ? `localized-${routeName}` : routeName;
|
|
169
|
-
if (!router.hasRoute(newRouteName)) {
|
|
170
|
-
const newParams2 = resolveParams(to);
|
|
171
|
-
delete newParams2.locale;
|
|
172
|
-
if (!router.hasRoute(routeName)) {
|
|
173
|
-
return router.resolve("/");
|
|
174
|
-
}
|
|
175
|
-
return router.resolve({
|
|
176
|
-
name: routeName,
|
|
177
|
-
params: newParams2,
|
|
178
|
-
query: selectRoute.query,
|
|
179
|
-
hash: selectRoute.hash
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
const newParams = resolveParams(to);
|
|
183
|
-
delete newParams.locale;
|
|
184
|
-
if (!isNoPrefixStrategy(i18nConfig.strategy)) {
|
|
185
|
-
if (currentLocale !== i18nConfig.defaultLocale || withPrefixStrategy(i18nConfig.strategy)) {
|
|
186
|
-
newParams.locale = currentLocale;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
return router.resolve({
|
|
190
|
-
name: newRouteName,
|
|
191
|
-
params: newParams,
|
|
192
|
-
query: selectRoute.query,
|
|
193
|
-
hash: selectRoute.hash
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
function formatNumber(value, locale, options) {
|
|
197
|
-
return new Intl.NumberFormat(locale, options).format(value);
|
|
198
|
-
}
|
|
199
|
-
function formatDate(value, locale, options) {
|
|
200
|
-
return new Intl.DateTimeFormat(locale, options).format(new Date(value));
|
|
201
|
-
}
|
|
202
|
-
function formatRelativeTime(value, locale, options) {
|
|
203
|
-
const date = new Date(value);
|
|
204
|
-
const now = /* @__PURE__ */ new Date();
|
|
205
|
-
const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1e3);
|
|
206
|
-
const units = [
|
|
207
|
-
{ unit: "year", seconds: 31536e3 },
|
|
208
|
-
{ unit: "month", seconds: 2592e3 },
|
|
209
|
-
{ unit: "day", seconds: 86400 },
|
|
210
|
-
{ unit: "hour", seconds: 3600 },
|
|
211
|
-
{ unit: "minute", seconds: 60 },
|
|
212
|
-
{ unit: "second", seconds: 1 }
|
|
213
|
-
];
|
|
214
|
-
for (const { unit, seconds } of units) {
|
|
215
|
-
const diff = Math.floor(diffInSeconds / seconds);
|
|
216
|
-
if (diff >= 1) {
|
|
217
|
-
return new Intl.RelativeTimeFormat(locale, options).format(-diff, unit);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
return new Intl.RelativeTimeFormat(locale, options).format(0, "second");
|
|
221
|
-
}
|
|
222
7
|
export default defineNuxtPlugin(async (nuxtApp) => {
|
|
223
|
-
if (!nuxtApp.payload.data.translations) {
|
|
224
|
-
nuxtApp.payload.data.translations = {};
|
|
225
|
-
}
|
|
226
8
|
const config = useRuntimeConfig();
|
|
227
9
|
const i18nConfig = config.public.i18nConfig;
|
|
228
10
|
const apiBaseUrl = i18nConfig.apiBaseUrl ?? "_locales";
|
|
11
|
+
const router = useRouter();
|
|
229
12
|
const runtimeConfig = useRuntimeConfig();
|
|
230
13
|
let hashLocaleDefault = null;
|
|
231
14
|
let noPrefixDefault = null;
|
|
232
15
|
if (i18nConfig.hashMode) {
|
|
233
|
-
hashLocaleDefault = await nuxtApp.runWithContext(() =>
|
|
234
|
-
return useCookie("hash-locale").value;
|
|
235
|
-
});
|
|
16
|
+
hashLocaleDefault = await nuxtApp.runWithContext(() => useCookie("hash-locale").value);
|
|
236
17
|
}
|
|
237
18
|
if (isNoPrefixStrategy(i18nConfig.strategy)) {
|
|
238
|
-
noPrefixDefault = await nuxtApp.runWithContext(() =>
|
|
239
|
-
return useCookie("no-prefix-locale").value;
|
|
240
|
-
});
|
|
19
|
+
noPrefixDefault = await nuxtApp.runWithContext(() => useCookie("no-prefix-locale").value);
|
|
241
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
|
+
});
|
|
242
38
|
const loadTranslationsIfNeeded = async (locale, routeName, path) => {
|
|
243
39
|
try {
|
|
244
40
|
if (!i18nHelper.hasPageTranslation(locale, routeName)) {
|
|
@@ -263,19 +59,17 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
263
59
|
}
|
|
264
60
|
};
|
|
265
61
|
async function loadGlobalTranslations(to) {
|
|
266
|
-
let
|
|
267
|
-
let noPrefixLocale = null;
|
|
62
|
+
let locale = routeService.getCurrentLocale(to);
|
|
268
63
|
if (i18nConfig.hashMode) {
|
|
269
|
-
|
|
270
|
-
return useCookie("hash-locale").value;
|
|
64
|
+
locale = await nuxtApp.runWithContext(() => {
|
|
65
|
+
return useCookie("hash-locale", { default: () => locale }).value;
|
|
271
66
|
});
|
|
272
67
|
}
|
|
273
68
|
if (isNoPrefixStrategy(i18nConfig.strategy)) {
|
|
274
|
-
|
|
275
|
-
return useCookie("no-prefix-locale").value;
|
|
69
|
+
locale = await nuxtApp.runWithContext(() => {
|
|
70
|
+
return useCookie("no-prefix-locale", { default: () => locale }).value;
|
|
276
71
|
});
|
|
277
72
|
}
|
|
278
|
-
const locale = getCurrentLocale(to, i18nConfig, hashLocale, noPrefixLocale);
|
|
279
73
|
if (!i18nHelper.hasGeneralTranslation(locale)) {
|
|
280
74
|
const url = `/${apiBaseUrl}/general/${locale}/data.json`.replace(/\/{2,}/g, "/");
|
|
281
75
|
const data = await $fetch(url, {
|
|
@@ -287,15 +81,16 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
287
81
|
await i18nHelper.loadTranslations(locale, data ?? {});
|
|
288
82
|
}
|
|
289
83
|
if (!i18nConfig.disablePageLocales) {
|
|
290
|
-
const
|
|
291
|
-
|
|
84
|
+
const locale2 = routeService.getCurrentLocale(to);
|
|
85
|
+
const routeName = routeService.getRouteName(to, locale2);
|
|
86
|
+
await loadTranslationsIfNeeded(locale2, routeName, to.fullPath);
|
|
292
87
|
}
|
|
293
88
|
await nuxtApp.callHook("i18n:register", (translations, selectedLocale) => {
|
|
294
|
-
const
|
|
295
|
-
|
|
89
|
+
const locale2 = routeService.getCurrentLocale(to);
|
|
90
|
+
const routeName = routeService.getRouteName(to, locale2);
|
|
91
|
+
i18nHelper.mergeTranslation(selectedLocale ?? locale2, routeName, translations, true);
|
|
296
92
|
}, locale);
|
|
297
93
|
}
|
|
298
|
-
const router = useRouter();
|
|
299
94
|
router.beforeEach(async (to, from, next) => {
|
|
300
95
|
if (to.path !== from.path || isNoPrefixStrategy(i18nConfig.strategy)) {
|
|
301
96
|
await loadGlobalTranslations(to);
|
|
@@ -305,85 +100,75 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
305
100
|
}
|
|
306
101
|
});
|
|
307
102
|
await loadGlobalTranslations(router.currentRoute.value);
|
|
308
|
-
const getTranslation = (key, params, defaultValue) => {
|
|
309
|
-
if (!key) return "";
|
|
310
|
-
const route = router.currentRoute.value;
|
|
311
|
-
const locale = getCurrentLocale(route, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
312
|
-
const routeName = getRouteName(route, locale);
|
|
313
|
-
let value = i18nHelper.getTranslation(locale, routeName, key);
|
|
314
|
-
if (!value) {
|
|
315
|
-
if (isDev && import.meta.client) {
|
|
316
|
-
console.warn(`Not found '${key}' key in '${locale}' locale messages.`);
|
|
317
|
-
}
|
|
318
|
-
value = defaultValue || key;
|
|
319
|
-
}
|
|
320
|
-
return typeof value === "string" && params ? interpolate(value, params) : value;
|
|
321
|
-
};
|
|
322
|
-
const i18nRouteParams = useState("i18n-route-params");
|
|
323
|
-
nuxtApp.hook("page:start", () => {
|
|
324
|
-
i18nRouteParams.value = null;
|
|
325
|
-
});
|
|
326
103
|
const provideData = {
|
|
327
104
|
i18n: void 0,
|
|
328
105
|
__micro: true,
|
|
329
|
-
getLocale: () => getCurrentLocale(
|
|
330
|
-
getLocaleName: () => getCurrentName(
|
|
106
|
+
getLocale: () => routeService.getCurrentLocale(),
|
|
107
|
+
getLocaleName: () => routeService.getCurrentName(routeService.getCurrentRoute()),
|
|
331
108
|
defaultLocale: () => i18nConfig.defaultLocale,
|
|
332
109
|
getLocales: () => i18nConfig.locales || [],
|
|
333
110
|
getRouteName: (route, locale) => {
|
|
334
|
-
const selectedLocale = locale ?? getCurrentLocale(
|
|
335
|
-
const selectedRoute = route ??
|
|
336
|
-
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;
|
|
337
128
|
},
|
|
338
|
-
t: getTranslation,
|
|
339
129
|
ts: (key, params, defaultValue) => {
|
|
340
|
-
const value =
|
|
130
|
+
const value = provideData.t(key, params, defaultValue);
|
|
341
131
|
return value?.toString() ?? defaultValue ?? key;
|
|
342
132
|
},
|
|
343
133
|
tc: (key, params, defaultValue) => {
|
|
344
|
-
const
|
|
345
|
-
const currentLocale = getCurrentLocale(route, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
134
|
+
const currentLocale = routeService.getCurrentLocale();
|
|
346
135
|
const { count, ..._params } = typeof params === "number" ? { count: params } : params;
|
|
347
|
-
return plural(key, Number.parseInt(count.toString()), _params, currentLocale,
|
|
136
|
+
return plural(key, Number.parseInt(count.toString()), _params, currentLocale, provideData.t) ?? defaultValue ?? key;
|
|
348
137
|
},
|
|
349
138
|
tn: (value, options) => {
|
|
350
|
-
const
|
|
351
|
-
|
|
352
|
-
return formatNumber(value, locale, options);
|
|
139
|
+
const currentLocale = routeService.getCurrentLocale();
|
|
140
|
+
return translationService.formatNumber(value, currentLocale, options);
|
|
353
141
|
},
|
|
354
142
|
td: (value, options) => {
|
|
355
|
-
const
|
|
356
|
-
|
|
357
|
-
return formatDate(value, locale, options);
|
|
143
|
+
const currentLocale = routeService.getCurrentLocale();
|
|
144
|
+
return translationService.formatDate(value, currentLocale, options);
|
|
358
145
|
},
|
|
359
|
-
tdr(value, options) {
|
|
360
|
-
const
|
|
361
|
-
|
|
362
|
-
return formatRelativeTime(value, locale, options);
|
|
146
|
+
tdr: (value, options) => {
|
|
147
|
+
const currentLocale = routeService.getCurrentLocale();
|
|
148
|
+
return translationService.formatRelativeTime(value, currentLocale, options);
|
|
363
149
|
},
|
|
364
150
|
has: (key) => {
|
|
365
|
-
return !!
|
|
151
|
+
return !!provideData.t(key);
|
|
366
152
|
},
|
|
367
153
|
mergeTranslations: (newTranslations) => {
|
|
368
|
-
const route =
|
|
369
|
-
const locale = getCurrentLocale(route
|
|
370
|
-
const routeName = getRouteName(route, locale);
|
|
154
|
+
const route = routeService.getCurrentRoute();
|
|
155
|
+
const locale = routeService.getCurrentLocale(route);
|
|
156
|
+
const routeName = routeService.getRouteName(route, locale);
|
|
371
157
|
i18nHelper.mergeTranslation(locale, routeName, newTranslations);
|
|
372
158
|
},
|
|
373
159
|
mergeGlobalTranslations: (newTranslations) => {
|
|
374
|
-
const
|
|
375
|
-
const locale = getCurrentLocale(route, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
160
|
+
const locale = routeService.getCurrentLocale();
|
|
376
161
|
i18nHelper.mergeGlobalTranslation(locale, newTranslations, true);
|
|
377
162
|
},
|
|
378
163
|
switchLocaleRoute: (toLocale) => {
|
|
379
|
-
const route =
|
|
380
|
-
const fromLocale = getCurrentLocale(route
|
|
381
|
-
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));
|
|
382
167
|
},
|
|
383
168
|
switchLocalePath: (toLocale) => {
|
|
384
|
-
const route =
|
|
385
|
-
const fromLocale = getCurrentLocale(route
|
|
386
|
-
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));
|
|
387
172
|
if (typeof localeRoute === "string") {
|
|
388
173
|
return localeRoute;
|
|
389
174
|
}
|
|
@@ -393,60 +178,16 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
393
178
|
return "";
|
|
394
179
|
},
|
|
395
180
|
switchLocale: (toLocale) => {
|
|
396
|
-
|
|
397
|
-
const fromLocale = getCurrentLocale(route, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
398
|
-
if (i18nConfig.hashMode) {
|
|
399
|
-
hashLocaleDefault = toLocale;
|
|
400
|
-
useCookie("hash-locale").value = toLocale;
|
|
401
|
-
}
|
|
402
|
-
if (isNoPrefixStrategy(i18nConfig.strategy)) {
|
|
403
|
-
noPrefixDefault = toLocale;
|
|
404
|
-
useCookie("no-prefix-locale").value = toLocale;
|
|
405
|
-
}
|
|
406
|
-
switchLocale(fromLocale, toLocale, route, router, i18nConfig, i18nRouteParams.value);
|
|
181
|
+
return routeService.switchLocaleLogic(toLocale, unref(i18nRouteParams.value));
|
|
407
182
|
},
|
|
408
183
|
switchRoute: (route, toLocale) => {
|
|
409
|
-
|
|
410
|
-
const fromLocale = getCurrentLocale(currentRoute, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
411
|
-
const currentLocale = toLocale ?? fromLocale;
|
|
412
|
-
if (typeof route === "string") {
|
|
413
|
-
if (currentLocale !== i18nConfig.defaultLocale || withPrefixStrategy(i18nConfig.strategy)) {
|
|
414
|
-
const currentRoute2 = router.currentRoute.value;
|
|
415
|
-
const fromLocale2 = getCurrentLocale(currentRoute2, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
416
|
-
route = router.resolve("/" + fromLocale2 + route);
|
|
417
|
-
} else {
|
|
418
|
-
route = router.resolve(route);
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
if (i18nConfig.hashMode && toLocale && toLocale !== fromLocale) {
|
|
422
|
-
hashLocaleDefault = toLocale ?? fromLocale;
|
|
423
|
-
useCookie("hash-locale").value = hashLocaleDefault;
|
|
424
|
-
}
|
|
425
|
-
if (isNoPrefixStrategy(i18nConfig.strategy) && toLocale && toLocale !== fromLocale) {
|
|
426
|
-
noPrefixDefault = toLocale ?? fromLocale;
|
|
427
|
-
useCookie("no-prefix-locale").value = noPrefixDefault;
|
|
428
|
-
}
|
|
429
|
-
switchLocale(fromLocale, toLocale ?? fromLocale, route, router, i18nConfig, i18nRouteParams.value);
|
|
184
|
+
return routeService.switchLocaleLogic(toLocale ?? routeService.getCurrentLocale(), unref(i18nRouteParams.value), route);
|
|
430
185
|
},
|
|
431
186
|
localeRoute: (to, locale) => {
|
|
432
|
-
|
|
433
|
-
const fromLocale = getCurrentLocale(currentRoute, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
434
|
-
const currentLocale = locale ?? fromLocale;
|
|
435
|
-
if (typeof to === "string") {
|
|
436
|
-
if (currentLocale !== i18nConfig.defaultLocale || withPrefixStrategy(i18nConfig.strategy)) {
|
|
437
|
-
const currentRoute2 = router.currentRoute.value;
|
|
438
|
-
const fromLocale2 = getCurrentLocale(currentRoute2, i18nConfig, hashLocaleDefault, noPrefixDefault);
|
|
439
|
-
to = router.resolve("/" + fromLocale2 + to);
|
|
440
|
-
} else {
|
|
441
|
-
to = router.resolve(to);
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
const route = router.currentRoute.value;
|
|
445
|
-
return getLocalizedRoute(to, router, route, i18nConfig, currentLocale, hashLocaleDefault, noPrefixDefault);
|
|
187
|
+
return routeService.resolveLocalizedRoute(to, locale);
|
|
446
188
|
},
|
|
447
189
|
localePath: (to, locale) => {
|
|
448
|
-
const
|
|
449
|
-
const localeRoute = getLocalizedRoute(to, router, route, i18nConfig, locale, hashLocaleDefault, noPrefixDefault);
|
|
190
|
+
const localeRoute = routeService.resolveLocalizedRoute(to, locale);
|
|
450
191
|
if (typeof localeRoute === "string") {
|
|
451
192
|
return localeRoute;
|
|
452
193
|
}
|
|
@@ -464,6 +205,14 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
464
205
|
Object.entries(provideData).map(([key, value]) => [`$${key}`, value])
|
|
465
206
|
);
|
|
466
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;
|
|
467
216
|
return {
|
|
468
217
|
provide: provideData
|
|
469
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 { isNoPrefixStrategy, 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) {
|
|
@@ -51,33 +51,31 @@ export default defineNuxtPlugin(async (_nuxtApp) => {
|
|
|
51
51
|
}
|
|
52
52
|
});
|
|
53
53
|
const defineI18nRoute = async (routeDefinition) => {
|
|
54
|
-
const
|
|
54
|
+
const { $getLocale } = useNuxtApp();
|
|
55
|
+
let currentLocale = computed(() => $getLocale());
|
|
55
56
|
const normalizedLocales = normalizeLocales(routeDefinition.locales);
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const resolvedRoute = router.resolve({ name: defaultRouteName });
|
|
65
|
-
const newParams = { ...route.params };
|
|
66
|
-
delete newParams.locale;
|
|
67
|
-
if (isPrefixStrategy(i18nConfig.strategy)) {
|
|
68
|
-
if (router.hasRoute(`localized-${defaultRouteName}-${currentLocale}`)) {
|
|
69
|
-
defaultRouteName = `localized-${defaultRouteName}-${currentLocale}`;
|
|
70
|
-
} else {
|
|
71
|
-
defaultRouteName = `localized-${defaultRouteName}`;
|
|
72
|
-
}
|
|
73
|
-
if (!router.hasRoute(defaultRouteName)) {
|
|
74
|
-
return;
|
|
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);
|
|
75
65
|
}
|
|
76
|
-
newParams.locale = i18nConfig.defaultLocale;
|
|
77
|
-
newParams.name = defaultRouteName;
|
|
78
66
|
}
|
|
79
|
-
return router.push(resolvedRoute);
|
|
80
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
|
+
});
|
|
81
79
|
}
|
|
82
80
|
};
|
|
83
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());
|
|
@@ -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;
|
package/dist/utils.d.ts
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;
|