nuxt-i18n-micro 1.39.0 → 1.41.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.
@@ -1,17 +1,21 @@
1
1
  import { useTranslationHelper, interpolate } from "nuxt-i18n-micro-core";
2
+ import { isNoPrefixStrategy, withPrefixStrategy } from "../helpers.js";
2
3
  import { defineNuxtPlugin, useRuntimeConfig } from "#app";
3
4
  import { useRouter, useCookie, useState, navigateTo } from "#imports";
4
5
  import { plural } from "#build/i18n.plural.mjs";
5
6
  const i18nHelper = useTranslationHelper();
6
7
  const isDev = process.env.NODE_ENV !== "production";
7
- function getCurrentLocale(route, i18nConfig, hashLocale) {
8
+ function getCurrentLocale(route, i18nConfig, hashLocale, noPrefixStrategy) {
8
9
  if (i18nConfig.hashMode && hashLocale) {
9
10
  return hashLocale;
10
11
  }
12
+ if (isNoPrefixStrategy(i18nConfig.strategy) && noPrefixStrategy) {
13
+ return noPrefixStrategy;
14
+ }
11
15
  return (route.params?.locale ?? i18nConfig.defaultLocale).toString();
12
16
  }
13
- function getCurrentName(route, i18nConfig, hashLocale) {
14
- const currentLocaleCode = getCurrentLocale(route, i18nConfig, hashLocale);
17
+ function getCurrentName(route, i18nConfig, hashLocale, noPrefixStrategy) {
18
+ const currentLocaleCode = getCurrentLocale(route, i18nConfig, hashLocale, noPrefixStrategy);
15
19
  const checkLocale = i18nConfig.locales?.find((l) => l.code === currentLocaleCode);
16
20
  if (!checkLocale) {
17
21
  return null;
@@ -52,11 +56,14 @@ function switchLocaleRoute(fromLocale, toLocale, route, router, i18nConfig, i18n
52
56
  }
53
57
  return newRoute2;
54
58
  }
55
- const newRouteName = toLocale !== i18nConfig.defaultLocale || i18nConfig.includeDefaultLocaleRoute ? `localized-${routeName}` : routeName;
59
+ let newRouteName = routeName;
56
60
  const newParams = { ...route.params, ...i18nRouteParams?.[toLocale] };
57
61
  delete newParams.locale;
58
- if (toLocale !== i18nConfig.defaultLocale || i18nConfig.includeDefaultLocaleRoute) {
59
- newParams.locale = toLocale;
62
+ if (!isNoPrefixStrategy(i18nConfig.strategy)) {
63
+ newRouteName = toLocale !== i18nConfig.defaultLocale || withPrefixStrategy(i18nConfig.strategy) ? `localized-${routeName}` : routeName;
64
+ if (toLocale !== i18nConfig.defaultLocale || withPrefixStrategy(i18nConfig.strategy)) {
65
+ newParams.locale = toLocale;
66
+ }
60
67
  }
61
68
  if (i18nConfig.hashMode) {
62
69
  const userLocaleCookie = useCookie("hash-locale");
@@ -77,6 +84,10 @@ function switchLocale(fromLocale, toLocale, route, router, i18nConfig, i18nRoute
77
84
  console.warn(`Locale ${toLocale} is not available`);
78
85
  return Promise.reject(`Locale ${toLocale} is not available`);
79
86
  }
87
+ if (isNoPrefixStrategy(i18nConfig.strategy)) {
88
+ const userLocaleCookie = useCookie("no-prefix-locale");
89
+ userLocaleCookie.value = toLocale;
90
+ }
80
91
  const switchedRoute = switchLocaleRoute(
81
92
  fromLocale,
82
93
  toLocale,
@@ -88,9 +99,12 @@ function switchLocale(fromLocale, toLocale, route, router, i18nConfig, i18nRoute
88
99
  if (typeof switchedRoute === "string" && switchedRoute.startsWith("http")) {
89
100
  return navigateTo(switchedRoute, { redirectCode: 200, external: true });
90
101
  }
102
+ if (isNoPrefixStrategy(i18nConfig.strategy)) {
103
+ return navigateTo(switchedRoute, { redirectCode: 200, external: true });
104
+ }
91
105
  return router.push(switchedRoute);
92
106
  }
93
- function getLocalizedRoute(to, router, route, i18nConfig, locale, hashLocale) {
107
+ function getLocalizedRoute(to, router, route, i18nConfig, locale, hashLocale, noPrefixStrategy) {
94
108
  const resolveParams = (to2) => {
95
109
  const params = typeof to2 === "object" && "params" in to2 && typeof to2.params === "object" ? { ...to2.params } : {};
96
110
  if (typeof to2 === "string") {
@@ -101,7 +115,7 @@ function getLocalizedRoute(to, router, route, i18nConfig, locale, hashLocale) {
101
115
  }
102
116
  return params;
103
117
  };
104
- if (i18nConfig.includeDefaultLocaleRoute) {
118
+ if (withPrefixStrategy(i18nConfig.strategy)) {
105
119
  const defaultLocale = i18nConfig.defaultLocale;
106
120
  let resolvedTo = to;
107
121
  if (typeof to === "string") {
@@ -124,13 +138,13 @@ function getLocalizedRoute(to, router, route, i18nConfig, locale, hashLocale) {
124
138
  });
125
139
  }
126
140
  }
127
- const currentLocale = locale || getCurrentLocale(route, i18nConfig, hashLocale);
141
+ const currentLocale = locale || getCurrentLocale(route, i18nConfig, hashLocale, noPrefixStrategy);
128
142
  const selectRoute = router.resolve(to);
129
143
  const routeName = getRouteName(selectRoute, currentLocale).replace(new RegExp(`-${i18nConfig.defaultLocale}$`), "");
130
144
  if (!routeName || routeName === "") {
131
145
  const resolved = router.resolve(to);
132
146
  let url = resolved.path.replace(new RegExp(`^/${currentLocale}/`), "/");
133
- if (currentLocale !== i18nConfig.defaultLocale || i18nConfig.includeDefaultLocaleRoute) {
147
+ if (currentLocale !== i18nConfig.defaultLocale || withPrefixStrategy(i18nConfig.strategy)) {
134
148
  url = "/" + currentLocale + url;
135
149
  }
136
150
  return router.resolve({
@@ -149,7 +163,7 @@ function getLocalizedRoute(to, router, route, i18nConfig, locale, hashLocale) {
149
163
  hash: selectRoute.hash
150
164
  });
151
165
  }
152
- const newRouteName = currentLocale !== i18nConfig.defaultLocale || i18nConfig.includeDefaultLocaleRoute ? `localized-${routeName}` : routeName;
166
+ const newRouteName = currentLocale !== i18nConfig.defaultLocale || withPrefixStrategy(i18nConfig.strategy) ? `localized-${routeName}` : routeName;
153
167
  if (!router.hasRoute(newRouteName)) {
154
168
  const newParams2 = resolveParams(to);
155
169
  delete newParams2.locale;
@@ -165,7 +179,7 @@ function getLocalizedRoute(to, router, route, i18nConfig, locale, hashLocale) {
165
179
  }
166
180
  const newParams = resolveParams(to);
167
181
  delete newParams.locale;
168
- if (currentLocale !== i18nConfig.defaultLocale || i18nConfig.includeDefaultLocaleRoute) {
182
+ if (currentLocale !== i18nConfig.defaultLocale || withPrefixStrategy(i18nConfig.strategy)) {
169
183
  newParams.locale = currentLocale;
170
184
  }
171
185
  return router.resolve({
@@ -189,6 +203,7 @@ export default defineNuxtPlugin(async (nuxtApp) => {
189
203
  const i18nConfig = config.public.i18nConfig;
190
204
  const apiBaseUrl = i18nConfig.apiBaseUrl ?? "_locales";
191
205
  const runtimeConfig = useRuntimeConfig();
206
+ const noPrefixDefault = isNoPrefixStrategy(i18nConfig.strategy) ? useCookie("no-prefix-locale").value ?? i18nConfig.defaultLocale : null;
192
207
  const loadTranslationsIfNeeded = async (locale, routeName, path) => {
193
208
  try {
194
209
  if (!i18nHelper.hasPageTranslation(locale, routeName)) {
@@ -207,9 +222,9 @@ export default defineNuxtPlugin(async (nuxtApp) => {
207
222
  } catch (_error) {
208
223
  }
209
224
  };
210
- async function loadTranslationsForRoute(to) {
225
+ async function loadGlobalTranslations(to) {
211
226
  const hashLocale2 = i18nConfig.hashMode ? nuxtApp.runWithContext(() => (useCookie("hash-locale").value ?? i18nConfig.defaultLocale).toString()).toString() : null;
212
- const locale = getCurrentLocale(to, i18nConfig, hashLocale2);
227
+ const locale = getCurrentLocale(to, i18nConfig, hashLocale2, noPrefixDefault);
213
228
  if (!i18nHelper.hasGeneralTranslation(locale)) {
214
229
  const data = await $fetch(`/${apiBaseUrl}/general/${locale}/data.json?v=${i18nConfig.dateBuild}`, {
215
230
  baseURL: runtimeConfig.app.baseURL
@@ -228,18 +243,18 @@ export default defineNuxtPlugin(async (nuxtApp) => {
228
243
  const router = useRouter();
229
244
  router.beforeEach(async (to, from, next) => {
230
245
  if (to.path !== from.path) {
231
- await loadTranslationsForRoute(to);
246
+ await loadGlobalTranslations(to);
232
247
  }
233
248
  if (next) {
234
249
  next();
235
250
  }
236
251
  });
237
- await loadTranslationsForRoute(router.currentRoute.value);
252
+ await loadGlobalTranslations(router.currentRoute.value);
238
253
  const getTranslation = (key, params, defaultValue) => {
239
254
  if (!key) return "";
240
255
  const route = router.currentRoute.value;
241
256
  const hashLocale2 = i18nConfig.hashMode ? (useCookie("hash-locale").value ?? i18nConfig.defaultLocale).toString() : null;
242
- const locale = getCurrentLocale(route, i18nConfig, hashLocale2);
257
+ const locale = getCurrentLocale(route, i18nConfig, hashLocale2, noPrefixDefault);
243
258
  const routeName = getRouteName(route, locale);
244
259
  let value = i18nHelper.getTranslation(locale, routeName, key);
245
260
  if (!value) {
@@ -261,12 +276,12 @@ export default defineNuxtPlugin(async (nuxtApp) => {
261
276
  const provideData = {
262
277
  i18n: void 0,
263
278
  __micro: true,
264
- getLocale: () => getCurrentLocale(router.currentRoute.value, i18nConfig, hashLocale),
265
- getLocaleName: () => getCurrentName(router.currentRoute.value, i18nConfig, hashLocale),
279
+ getLocale: () => getCurrentLocale(router.currentRoute.value, i18nConfig, hashLocale, noPrefixDefault),
280
+ getLocaleName: () => getCurrentName(router.currentRoute.value, i18nConfig, hashLocale, noPrefixDefault),
266
281
  defaultLocale: () => i18nConfig.defaultLocale,
267
282
  getLocales: () => i18nConfig.locales || [],
268
283
  getRouteName: (route, locale) => {
269
- const selectedLocale = locale ?? getCurrentLocale(router.currentRoute.value, i18nConfig, hashLocale);
284
+ const selectedLocale = locale ?? getCurrentLocale(router.currentRoute.value, i18nConfig, hashLocale, noPrefixDefault);
270
285
  const selectedRoute = route ?? router.currentRoute.value;
271
286
  return getRouteName(selectedRoute, selectedLocale);
272
287
  },
@@ -277,18 +292,18 @@ export default defineNuxtPlugin(async (nuxtApp) => {
277
292
  },
278
293
  tc: (key, params, defaultValue) => {
279
294
  const route = router.currentRoute.value;
280
- const currentLocale = getCurrentLocale(route, i18nConfig, hashLocale);
295
+ const currentLocale = getCurrentLocale(route, i18nConfig, hashLocale, noPrefixDefault);
281
296
  const { count, ..._params } = typeof params === "number" ? { count: params } : params;
282
297
  return plural(key, Number.parseInt(count.toString()), _params, currentLocale, getTranslation) ?? defaultValue ?? key;
283
298
  },
284
299
  tn: (value, options) => {
285
300
  const route = router.currentRoute.value;
286
- const locale = getCurrentLocale(route, i18nConfig, hashLocale);
301
+ const locale = getCurrentLocale(route, i18nConfig, hashLocale, noPrefixDefault);
287
302
  return formatNumber(value, locale, options);
288
303
  },
289
304
  td: (value, options) => {
290
305
  const route = router.currentRoute.value;
291
- const locale = getCurrentLocale(route, i18nConfig, hashLocale);
306
+ const locale = getCurrentLocale(route, i18nConfig, hashLocale, noPrefixDefault);
292
307
  return formatDate(value, locale, options);
293
308
  },
294
309
  has: (key) => {
@@ -296,13 +311,13 @@ export default defineNuxtPlugin(async (nuxtApp) => {
296
311
  },
297
312
  mergeTranslations: (newTranslations) => {
298
313
  const route = router.currentRoute.value;
299
- const locale = getCurrentLocale(route, i18nConfig, hashLocale);
314
+ const locale = getCurrentLocale(route, i18nConfig, hashLocale, noPrefixDefault);
300
315
  const routeName = getRouteName(route, locale);
301
316
  i18nHelper.mergeTranslation(locale, routeName, newTranslations);
302
317
  },
303
318
  switchLocaleRoute: (toLocale) => {
304
319
  const route = router.currentRoute.value;
305
- const fromLocale = getCurrentLocale(route, i18nConfig, hashLocale);
320
+ const fromLocale = getCurrentLocale(route, i18nConfig, hashLocale, noPrefixDefault);
306
321
  if (i18nConfig.hashMode) {
307
322
  hashLocale = toLocale;
308
323
  }
@@ -310,7 +325,7 @@ export default defineNuxtPlugin(async (nuxtApp) => {
310
325
  },
311
326
  switchLocalePath: (toLocale) => {
312
327
  const route = router.currentRoute.value;
313
- const fromLocale = getCurrentLocale(route, i18nConfig, hashLocale);
328
+ const fromLocale = getCurrentLocale(route, i18nConfig, hashLocale, noPrefixDefault);
314
329
  if (i18nConfig.hashMode) {
315
330
  hashLocale = toLocale;
316
331
  }
@@ -325,7 +340,7 @@ export default defineNuxtPlugin(async (nuxtApp) => {
325
340
  },
326
341
  switchLocale: (toLocale) => {
327
342
  const route = router.currentRoute.value;
328
- const fromLocale = getCurrentLocale(route, i18nConfig, hashLocale);
343
+ const fromLocale = getCurrentLocale(route, i18nConfig, hashLocale, noPrefixDefault);
329
344
  if (i18nConfig.hashMode) {
330
345
  hashLocale = toLocale;
331
346
  }
@@ -335,7 +350,7 @@ export default defineNuxtPlugin(async (nuxtApp) => {
335
350
  if (typeof route === "string") {
336
351
  route = router.resolve(route);
337
352
  }
338
- const fromLocale = getCurrentLocale(route, i18nConfig, hashLocale);
353
+ const fromLocale = getCurrentLocale(route, i18nConfig, hashLocale, noPrefixDefault);
339
354
  if (i18nConfig.hashMode) {
340
355
  hashLocale = toLocale ?? fromLocale;
341
356
  }
@@ -1,3 +1,4 @@
1
+ import { isPrefixStrategy } from "../helpers.js";
1
2
  import { defineNuxtPlugin, navigateTo, useNuxtApp, useRuntimeConfig } from "#app";
2
3
  import { useRoute, useRouter } from "#imports";
3
4
  export default defineNuxtPlugin(async (_nuxtApp) => {
@@ -35,12 +36,12 @@ export default defineNuxtPlugin(async (_nuxtApp) => {
35
36
  }
36
37
  };
37
38
  if (import.meta.server) {
38
- if (i18nConfig.includeDefaultLocaleRoute) {
39
+ if (isPrefixStrategy(i18nConfig.strategy)) {
39
40
  await handleRedirect(route);
40
41
  }
41
42
  }
42
43
  router.beforeEach(async (to, from, next) => {
43
- if (i18nConfig.includeDefaultLocaleRoute) {
44
+ if (isPrefixStrategy(i18nConfig.strategy)) {
44
45
  await handleRedirect(to);
45
46
  }
46
47
  if (next) {
@@ -61,7 +62,7 @@ export default defineNuxtPlugin(async (_nuxtApp) => {
61
62
  const resolvedRoute = router.resolve({ name: defaultRouteName });
62
63
  const newParams = { ...route.params };
63
64
  delete newParams.locale;
64
- if (i18nConfig.includeDefaultLocaleRoute) {
65
+ if (isPrefixStrategy(i18nConfig.strategy)) {
65
66
  if (router.hasRoute(`localized-${defaultRouteName}-${currentLocale}`)) {
66
67
  defaultRouteName = `localized-${defaultRouteName}-${currentLocale}`;
67
68
  } else {
@@ -1,3 +1,4 @@
1
+ import { isPrefixStrategy, isNoPrefixStrategy } from "../helpers.js";
1
2
  import { defineNuxtPlugin, useCookie, useRequestHeaders, navigateTo } from "#app";
2
3
  import { useRoute, useRouter } from "#imports";
3
4
  const parseAcceptLanguage = (acceptLanguage) => acceptLanguage.split(",").map((entry) => entry.split(";")[0].trim());
@@ -16,13 +17,32 @@ export default defineNuxtPlugin(async (nuxtApp) => {
16
17
  const router = useRouter();
17
18
  const route = useRoute();
18
19
  async function switchLocale(newLocale) {
20
+ if (isNoPrefixStrategy(i18nConfig.strategy)) {
21
+ if (newLocale !== defaultLocale) {
22
+ const newParams2 = { ...route.params };
23
+ delete newParams2.locale;
24
+ const resolvedRoute2 = router.resolve(router.currentRoute.value);
25
+ const routeName2 = resolvedRoute2.name.replace(`localized-`, "");
26
+ const newRoute2 = router.resolve({
27
+ name: routeName2,
28
+ params: newParams2
29
+ });
30
+ const userLocaleCookie2 = useCookie("no-prefix-locale");
31
+ userLocaleCookie2.value = newLocale;
32
+ await navigateTo(newRoute2, {
33
+ redirectCode: 302,
34
+ external: true
35
+ });
36
+ }
37
+ return;
38
+ }
19
39
  const currentPath = router.currentRoute;
20
40
  const resolvedRoute = router.resolve(currentPath.value);
21
41
  const routeName = resolvedRoute.name.replace(`localized-`, "");
22
- const newRouteName = i18nConfig.includeDefaultLocaleRoute || newLocale !== defaultLocale ? `localized-${routeName}` : routeName;
42
+ const newRouteName = isPrefixStrategy(i18nConfig.strategy) || newLocale !== defaultLocale ? `localized-${routeName}` : routeName;
23
43
  const newParams = { ...route.params };
24
44
  delete newParams.locale;
25
- if (i18nConfig.includeDefaultLocaleRoute || newLocale !== defaultLocale) {
45
+ if (isPrefixStrategy(i18nConfig.strategy) || newLocale !== defaultLocale) {
26
46
  newParams.locale = newLocale;
27
47
  }
28
48
  const newRoute = router.resolve({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-i18n-micro",
3
- "version": "1.39.0",
3
+ "version": "1.41.0",
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",
@@ -40,8 +40,8 @@
40
40
  "internals.d.ts"
41
41
  ],
42
42
  "dependencies": {
43
- "@nuxt/devtools-kit": "^1.6.1",
44
- "@nuxt/kit": "^3.14.1592",
43
+ "@nuxt/devtools-kit": "^1.6.4",
44
+ "@nuxt/kit": "3.13.2",
45
45
  "chokidar": "^3.6.0",
46
46
  "sirv": "^2.0.4",
47
47
  "ufo": "^1.5.4",
@@ -50,27 +50,27 @@
50
50
  },
51
51
  "devDependencies": {
52
52
  "@types/jest": "^29.5.14",
53
- "@nuxt/devtools": "^1.6.1",
54
- "@nuxt/devtools-ui-kit": "^1.6.1",
55
- "@nuxt/eslint-config": "0.5.5",
53
+ "@nuxt/devtools": "^1.6.4",
54
+ "@nuxt/devtools-ui-kit": "^1.6.4",
55
+ "@nuxt/eslint-config": "0.7.3",
56
56
  "@nuxt/module-builder": "^0.8.4",
57
- "@nuxt/schema": "^3.14.1592",
58
- "@nuxt/test-utils": "^3.14.4",
59
- "@playwright/test": "^1.49.0",
60
- "@types/node": "^20.14.11",
57
+ "@nuxt/schema": "3.13.2",
58
+ "@nuxt/test-utils": "^3.15.1",
59
+ "@playwright/test": "^1.49.1",
60
+ "@types/node": "^20.17.10",
61
61
  "changelogen": "^0.5.7",
62
62
  "eslint": "^8.57.1",
63
- "nuxt": "^3.14.1592",
63
+ "nuxt": "3.13.2",
64
64
  "typescript": "5.6.3",
65
65
  "vitepress": "^1.5.0",
66
- "vitest": "^2.1.6",
66
+ "vitest": "^2.1.8",
67
67
  "jest": "^29.7.0",
68
68
  "ts-jest": "^29.2.5",
69
69
  "vue-tsc": "2.1.10"
70
70
  },
71
71
  "optionalDependencies": {
72
- "@rollup/rollup-linux-x64-gnu": "^4.9.6",
73
- "@rollup/rollup-win32-x64-msvc": "^4.9.6"
72
+ "@rollup/rollup-linux-x64-gnu": "^4.28.1",
73
+ "@rollup/rollup-win32-x64-msvc": "^4.28.1"
74
74
  },
75
75
  "workspaces": [
76
76
  "client",