nuxt-i18n-micro 1.1.7 → 1.1.9

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/README.md CHANGED
@@ -160,7 +160,7 @@ Switches to the given locale and redirects the user to the appropriate localized
160
160
  $switchLocale('fr')
161
161
  ```
162
162
 
163
- ### `$localeRoute(to: RouteLocationRaw): RouteLocationRaw`
163
+ ### `$localeRoute(to: RouteLocationRaw, locale?: string): RouteLocationRaw`
164
164
  Generates a localized route object based on the target route.
165
165
 
166
166
  ```typescript
@@ -8,4 +8,4 @@
8
8
  <link rel="prefetch" as="style" href="/__nuxt-i18n-micro/_nuxt/error-500.B4KzowuE.css">
9
9
  <link rel="prefetch" as="script" crossorigin href="/__nuxt-i18n-micro/_nuxt/HZLiFEh-.js">
10
10
  <script type="module" src="/__nuxt-i18n-micro/_nuxt/6EJ4fAZ2.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" id="__NUXT_DATA__" data-ssr="false">[{"serverRendered":1},false]</script>
11
- <script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-i18n-micro",buildId:"01bea438-049f-4057-924c-e8a88261b563",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
11
+ <script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-i18n-micro",buildId:"3c9001bb-920a-4988-9ad0-29eff844b06b",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
@@ -8,4 +8,4 @@
8
8
  <link rel="prefetch" as="style" href="/__nuxt-i18n-micro/_nuxt/error-500.B4KzowuE.css">
9
9
  <link rel="prefetch" as="script" crossorigin href="/__nuxt-i18n-micro/_nuxt/HZLiFEh-.js">
10
10
  <script type="module" src="/__nuxt-i18n-micro/_nuxt/6EJ4fAZ2.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" id="__NUXT_DATA__" data-ssr="false">[{"serverRendered":1},false]</script>
11
- <script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-i18n-micro",buildId:"01bea438-049f-4057-924c-e8a88261b563",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
11
+ <script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-i18n-micro",buildId:"3c9001bb-920a-4988-9ad0-29eff844b06b",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
@@ -1 +1 @@
1
- {"id":"01bea438-049f-4057-924c-e8a88261b563","timestamp":1723891595905}
1
+ {"id":"3c9001bb-920a-4988-9ad0-29eff844b06b","timestamp":1723999902563}
@@ -0,0 +1 @@
1
+ {"id":"3c9001bb-920a-4988-9ad0-29eff844b06b","timestamp":1723999902563,"matcher":{"static":{},"wildcard":{},"dynamic":{}},"prerendered":[]}
@@ -8,4 +8,4 @@
8
8
  <link rel="prefetch" as="style" href="/__nuxt-i18n-micro/_nuxt/error-500.B4KzowuE.css">
9
9
  <link rel="prefetch" as="script" crossorigin href="/__nuxt-i18n-micro/_nuxt/HZLiFEh-.js">
10
10
  <script type="module" src="/__nuxt-i18n-micro/_nuxt/6EJ4fAZ2.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" id="__NUXT_DATA__" data-ssr="false">[{"serverRendered":1},false]</script>
11
- <script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-i18n-micro",buildId:"01bea438-049f-4057-924c-e8a88261b563",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
11
+ <script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-i18n-micro",buildId:"3c9001bb-920a-4988-9ad0-29eff844b06b",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
package/dist/module.d.mts CHANGED
@@ -13,6 +13,7 @@ interface ModuleOptions {
13
13
  translationDir?: string;
14
14
  autoDetectLanguage?: boolean;
15
15
  includeDefaultLocaleRoute?: boolean;
16
+ cache?: boolean;
16
17
  plural?: string;
17
18
  }
18
19
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
package/dist/module.d.ts CHANGED
@@ -13,6 +13,7 @@ interface ModuleOptions {
13
13
  translationDir?: string;
14
14
  autoDetectLanguage?: boolean;
15
15
  includeDefaultLocaleRoute?: boolean;
16
+ cache?: boolean;
16
17
  plural?: string;
17
18
  }
18
19
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-i18n-micro",
3
3
  "configKey": "i18n",
4
- "version": "1.1.7",
4
+ "version": "1.1.9",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "0.8.3",
7
7
  "unbuild": "2.0.0"
package/dist/module.mjs CHANGED
@@ -79,6 +79,7 @@ const module = defineNuxtModule({
79
79
  translationDir: "locales",
80
80
  autoDetectLanguage: true,
81
81
  includeDefaultLocaleRoute: false,
82
+ cache: false,
82
83
  plural: `function (translation, count, _locale) {
83
84
  const forms = translation.toString().split('|')
84
85
  if (count === 0 && forms.length > 2) {
@@ -100,7 +101,8 @@ const module = defineNuxtModule({
100
101
  defaultLocale: options.defaultLocale ?? "en",
101
102
  translationDir: options.translationDir ?? "locales",
102
103
  autoDetectLanguage: options.autoDetectLanguage ?? true,
103
- includeDefaultLocaleRoute: options.includeDefaultLocaleRoute ?? false
104
+ includeDefaultLocaleRoute: options.includeDefaultLocaleRoute ?? false,
105
+ cache: options.cache ?? false
104
106
  };
105
107
  addPlugin({
106
108
  src: resolver.resolve("./runtime/01.plugin"),
@@ -1,8 +1,10 @@
1
1
  import { defineNuxtPlugin, useRuntimeConfig } from "#app";
2
2
  import { useRoute, useRouter } from "#imports";
3
+ const isDev = process.env.NODE_ENV !== "production";
3
4
  const generalLocaleCache = {};
4
5
  const routeLocaleCache = {};
5
6
  const dynamicTranslationsCaches = [];
7
+ const translationCache = { map: /* @__PURE__ */ new Map() };
6
8
  function deepClone(value) {
7
9
  if (Array.isArray(value)) {
8
10
  return value.slice();
@@ -12,13 +14,16 @@ function deepClone(value) {
12
14
  return value;
13
15
  }
14
16
  function getTranslation(translations, key) {
15
- const value = key.split(".").reduce((acc, part) => {
16
- if (typeof acc === "object" && acc !== null && part in acc) {
17
- return acc[part];
17
+ const parts = key.split(".");
18
+ let value = translations;
19
+ for (let i = 0; i < parts.length; i++) {
20
+ if (value && typeof value === "object" && parts[i] in value) {
21
+ value = value[parts[i]];
22
+ } else {
23
+ return null;
18
24
  }
19
- return void 0;
20
- }, translations);
21
- if (value && typeof value === "object") {
25
+ }
26
+ if (typeof value === "object" && value !== null) {
22
27
  return deepClone(value);
23
28
  }
24
29
  return value ?? null;
@@ -55,7 +60,7 @@ function switchLocale(locale, route, router, i18nConfig) {
55
60
  const checkLocale = i18nConfig.locales?.find((l) => l.code === locale);
56
61
  if (!checkLocale) {
57
62
  console.warn(`Locale ${locale} is not available`);
58
- return;
63
+ return Promise.reject(`Locale ${locale} is not available`);
59
64
  }
60
65
  const { defaultLocale } = i18nConfig;
61
66
  const routeName = route.name.replace(`localized-`, "");
@@ -65,7 +70,7 @@ function switchLocale(locale, route, router, i18nConfig) {
65
70
  if (locale !== defaultLocale || i18nConfig.includeDefaultLocaleRoute) {
66
71
  newParams.locale = locale;
67
72
  }
68
- window.location.href = router.resolve({ name: newRouteName, params: newParams }).fullPath;
73
+ return router.push({ name: newRouteName, params: newParams });
69
74
  }
70
75
  function getLocalizedRoute(to, router, route, i18nConfig, locale) {
71
76
  const { defaultLocale } = i18nConfig;
@@ -99,43 +104,80 @@ export default defineNuxtPlugin(async (_nuxtApp) => {
99
104
  await loadTranslations(initialLocale, initialRouteName, i18nConfig.translationDir);
100
105
  return {
101
106
  provide: {
102
- getLocale: () => (route.params?.locale ?? i18nConfig.defaultLocale).toString(),
107
+ getLocale: () => {
108
+ const route2 = useRoute();
109
+ return (route2.params?.locale ?? i18nConfig.defaultLocale).toString();
110
+ },
103
111
  getLocales: () => i18nConfig.locales || [],
104
112
  t: (key, params, defaultValue) => {
105
- const locale = (route.params?.locale ?? i18nConfig.defaultLocale).toString();
106
- const routeName = route.name.replace(`localized-`, "");
113
+ if (!key) {
114
+ console.log(`$t: key not exist`);
115
+ return "";
116
+ }
117
+ const route2 = useRoute();
118
+ const locale = (route2.params?.locale ?? i18nConfig.defaultLocale).toString();
119
+ const cacheKey = locale + ":" + key;
120
+ if (i18nConfig.cache && translationCache.map.has(cacheKey)) {
121
+ return translationCache.map.get(cacheKey);
122
+ }
123
+ const routeName = route2.name.replace(`localized-`, "");
107
124
  let value = getTranslation(routeLocaleCache[`${locale}:${routeName}`] ?? {}, key) || getTranslation(generalLocaleCache[locale] ?? {}, key) || dynamicTranslationsCaches.reduce((result, cache) => {
108
125
  return result || getTranslation(cache[locale] ?? {}, key);
109
126
  }, null);
110
127
  if (!value) {
128
+ if (isDev && import.meta.client) {
129
+ console.warn(`Not found '${key}' key in '${locale}' locale messages.`);
130
+ }
111
131
  value = defaultValue || key;
112
132
  }
113
133
  if (typeof value === "string" && params) {
114
134
  value = interpolate(value, params);
115
135
  }
136
+ if (i18nConfig.cache) {
137
+ translationCache.map.set(cacheKey, value);
138
+ }
116
139
  return value;
117
140
  },
118
141
  tc: (key, count, defaultValue) => {
119
- const locale = (route.params?.locale ?? i18nConfig.defaultLocale).toString();
120
- const routeName = route.name.replace(`localized-`, "");
142
+ if (!key) {
143
+ console.log(`$tc: key not exist`);
144
+ return "";
145
+ }
146
+ const route2 = useRoute();
147
+ const locale = (route2.params?.locale ?? i18nConfig.defaultLocale).toString();
148
+ const cacheKey = locale + ":" + key + ":" + count;
149
+ if (i18nConfig.cache && translationCache.map.has(cacheKey)) {
150
+ return translationCache.map.get(cacheKey);
151
+ }
152
+ const routeName = route2.name.replace(`localized-`, "");
121
153
  let translation = getPluralTranslation(routeLocaleCache[`${locale}:${routeName}`] ?? {}, key) || getPluralTranslation(generalLocaleCache[locale] ?? {}, key) || dynamicTranslationsCaches.reduce((result, cache) => {
122
154
  return result || getPluralTranslation(cache[locale] ?? {}, key);
123
155
  }, null);
124
156
  if (!translation) {
157
+ if (isDev && import.meta.client) {
158
+ console.warn(`Not found '${key}' key in '${locale}' locale messages.`);
159
+ }
125
160
  translation = defaultValue || key;
126
161
  }
127
- return plural(translation.toString(), count, locale);
162
+ const value = plural(translation.toString(), count, locale);
163
+ if (i18nConfig.cache) {
164
+ translationCache.map.set(cacheKey, value);
165
+ }
166
+ return value;
128
167
  },
129
168
  mergeTranslations: (newTranslations) => {
130
- const routeName = route.name.replace(`localized-`, "");
131
- const locale = (route.params?.locale ?? i18nConfig.defaultLocale).toString();
169
+ const route2 = useRoute();
170
+ const routeName = route2.name.replace(`localized-`, "");
171
+ const locale = (route2.params?.locale ?? i18nConfig.defaultLocale).toString();
132
172
  mergeTranslations(routeName, locale, newTranslations);
133
173
  },
134
174
  switchLocale: (locale) => {
135
- switchLocale(locale, route, router, i18nConfig);
175
+ const route2 = useRoute();
176
+ switchLocale(locale, route2, router, i18nConfig);
136
177
  },
137
178
  localeRoute: (to, locale) => {
138
- return getLocalizedRoute(to, router, route, i18nConfig, locale);
179
+ const route2 = useRoute();
180
+ return getLocalizedRoute(to, router, route2, i18nConfig, locale);
139
181
  }
140
182
  }
141
183
  };
@@ -10,8 +10,14 @@ export default defineNuxtPlugin((_nuxtApp) => {
10
10
  const { locales } = routeDefinition;
11
11
  const { name } = route;
12
12
  if (locales && !locales.includes(currentLocale)) {
13
- const defaultRouteName = i18nConfig.defaultLocale !== i18nConfig.defaultLocale ? `localized-${name?.toString}` : name;
13
+ let defaultRouteName = name?.toString().replace("localized-", "");
14
14
  const resolvedRoute = router.resolve({ name: defaultRouteName });
15
+ const newParams = { ...route.params };
16
+ delete newParams.locale;
17
+ if (i18nConfig.includeDefaultLocaleRoute) {
18
+ defaultRouteName = `localized-${defaultRouteName}`;
19
+ newParams.locale = i18nConfig.defaultLocale;
20
+ }
15
21
  return router.push(resolvedRoute);
16
22
  }
17
23
  };
@@ -0,0 +1 @@
1
+ export {};
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-i18n-micro",
3
- "version": "1.1.7",
3
+ "version": "1.1.9",
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",
@@ -70,7 +70,7 @@
70
70
  "@playwright/test": "^1.46.1",
71
71
  "@types/node": "^20.14.11",
72
72
  "changelogen": "^0.5.5",
73
- "eslint": "^9.7.0",
73
+ "eslint": "^8.56.0",
74
74
  "execa": "^9.3.0",
75
75
  "nuxt": "^3.12.4",
76
76
  "typescript": "latest",
@@ -1 +0,0 @@
1
- {"id":"01bea438-049f-4057-924c-e8a88261b563","timestamp":1723891595905,"matcher":{"static":{},"wildcard":{},"dynamic":{}},"prerendered":[]}