nuxt-i18n-micro 1.1.10 → 1.1.12

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
@@ -37,16 +37,16 @@ To showcase the efficiency of `Nuxt I18n Micro`, we conducted tests under identi
37
37
  #### Build Time and Resource Consumption
38
38
 
39
39
  **Nuxt I18n**:
40
- - **Total size**: 54.7 MB (3.29 MB gzip)
41
- - **Max CPU Usage**: 394.0%
42
- - **Max Memory Usage**: 8746 MB
43
- - **Elapsed Time**: 0h 1m 30s
40
+ - **Total size**: 54.7 MB (3.31 MB gzip)
41
+ - **Max CPU Usage**: 391.4%
42
+ - **Max Memory Usage**: 8305 MB
43
+ - **Elapsed Time**: 0h 1m 31s
44
44
 
45
45
  **Nuxt I18n Micro**:
46
- - **Total size**: 22.4 MB (2.9 MB gzip) — **59% smaller**
47
- - **Max CPU Usage**: 305.3% — **23% lower**
48
- - **Max Memory Usage**: 3247 MB — **63% less memory**
49
- - **Elapsed Time**: 0h 0m 17s — **81% faster**
46
+ - **Total size**: 1.93 MB (473 kB gzip) — **96% smaller**
47
+ - **Max CPU Usage**: 220.1% — **44% lower**
48
+ - **Max Memory Usage**: 655 MB — **92% less memory**
49
+ - **Elapsed Time**: 0h 0m 5s — **94% faster**
50
50
 
51
51
  #### Server Performance (10k Requests)
52
52
 
@@ -257,6 +257,49 @@ The module accepts the following options in the Nuxt configuration:
257
257
  - **autoDetectLanguage**: If `true`, automatically detects the user's preferred language and redirects accordingly.
258
258
  - **plural**: A custom function for handling pluralization.
259
259
  - **includeDefaultLocaleRoute**: A boolean. If enabled, all routes without a locale prefix will redirect to the default locale route.
260
+ - **cache**: (In development) A boolean option designed to optimize performance when working with large JSON translation files. When enabled, it caches translations specific to the current page, reducing search times and minimizing client-side load. This cached data is then sent to the client, resulting in faster page loads and improved user experience.
261
+
262
+ ## Locale Loading in Nuxt I18n Micro
263
+
264
+ The `Nuxt I18n Micro` module provides an efficient way to manage internationalization (i18n) in your Nuxt application, with a focus on performance and simplicity. One of the key features of this module is how it handles the loading of locale-specific translations. Below is an explanation of how this process works.
265
+
266
+ ### How Locale Loading Works
267
+
268
+ In `Nuxt I18n Micro`, translations are loaded dynamically based on the user's selected locale. The module uses a specific route pattern (`/_locales/:page/:locale/data.json`) to fetch the translation files for each page and locale. This approach is designed to minimize the initial load time and only load the necessary translations for the current page and locale.
269
+
270
+ #### Route Structure
271
+
272
+ The translations are organized into JSON files located in the `locales` directory. These files are split into:
273
+
274
+ - **Global Files**: Located at `locales/{locale}.json` (e.g., `locales/en.json`). These files contain translations shared across the entire application, such as menu items or common UI elements.
275
+ - **Page-Specific Files**: Located at `locales/pages/{routeName}/{locale}.json` (e.g., `locales/pages/index/en.json`). These files contain translations specific to individual pages.
276
+
277
+ #### Dynamic Locale Routes
278
+
279
+ To accommodate the dynamic nature of translation loading, `Nuxt I18n Micro` extends the default routing configuration by adding locale-specific routes. This means that for each page, a localized version of the route is generated. For example, if your application has an `index` page and supports English (`en`) and French (`fr`), the following routes would be generated:
280
+
281
+ - `/en/index`
282
+ - `/fr/index`
283
+
284
+ This structure allows the module to load the appropriate translations based on the user's selected locale.
285
+
286
+ ### Loading Translations via _locales Route
287
+
288
+ Given the way Vite and Nuxt handle modules, the `Nuxt I18n Micro` module uses a special route (`/_locales/:page/:locale/data.json`) to fetch translations. This is necessary because, during the build process, Vite may not bundle all translation files directly into the application. Instead, the module dynamically loads these files from the server as needed.
289
+
290
+ When a user navigates to a page, the module will:
291
+
292
+ 1. Check if the necessary translation file for the current page and locale is already loaded in the cache.
293
+ 2. If not, it will send a request to the `_locales` route to fetch the required translation file.
294
+ 3. Once the translation file is loaded, it is cached for future use, ensuring that the translations are readily available when the user navigates to another part of the application.
295
+
296
+ ### Caching and Pre-rendering
297
+
298
+ To further optimize performance, `Nuxt I18n Micro` supports caching and pre-rendering of translation files:
299
+
300
+ - **Caching**: Once a translation file is loaded, it is stored in the cache, reducing the need for subsequent requests.
301
+ - **Pre-rendering**: During the build process, the module can pre-render translation files for all configured locales and routes, which are then served directly from the server without the need for runtime requests.
302
+
260
303
 
261
304
  ## Conclusion
262
305
 
@@ -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:"f1ddbdf9-9b7b-4ddb-bddb-a5d520509f71",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
11
+ <script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-i18n-micro",buildId:"e3ab2284-9b25-4b7a-b8d6-8e1f2eb8cf35",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:"f1ddbdf9-9b7b-4ddb-bddb-a5d520509f71",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
11
+ <script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-i18n-micro",buildId:"e3ab2284-9b25-4b7a-b8d6-8e1f2eb8cf35",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
@@ -1 +1 @@
1
- {"id":"f1ddbdf9-9b7b-4ddb-bddb-a5d520509f71","timestamp":1724070947234}
1
+ {"id":"e3ab2284-9b25-4b7a-b8d6-8e1f2eb8cf35","timestamp":1724081837773}
@@ -0,0 +1 @@
1
+ {"id":"e3ab2284-9b25-4b7a-b8d6-8e1f2eb8cf35","timestamp":1724081837773,"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:"f1ddbdf9-9b7b-4ddb-bddb-a5d520509f71",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
11
+ <script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-i18n-micro",buildId:"e3ab2284-9b25-4b7a-b8d6-8e1f2eb8cf35",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
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.10",
4
+ "version": "1.1.12",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "0.8.3",
7
7
  "unbuild": "2.0.0"
package/dist/module.mjs CHANGED
@@ -164,14 +164,12 @@ const module = defineNuxtModule({
164
164
  });
165
165
  pages.push(...newRoutes);
166
166
  nuxt.options.generate.routes = Array.isArray(nuxt.options.generate.routes) ? nuxt.options.generate.routes : [];
167
- if (nuxt.options?._generate) {
168
- options.locales?.forEach((locale) => {
169
- pagesNames.forEach((name) => {
170
- addPrerenderRoutes(`/_locales/${name}/${locale.code}/data.json`);
171
- });
172
- addPrerenderRoutes(`/_locales/general/${locale.code}/data.json`);
167
+ options.locales?.forEach((locale) => {
168
+ pagesNames.forEach((name) => {
169
+ addPrerenderRoutes(`/_locales/${name}/${locale.code}/data.json`);
173
170
  });
174
- }
171
+ addPrerenderRoutes(`/_locales/general/${locale.code}/data.json`);
172
+ });
175
173
  });
176
174
  nuxt.hook("nitro:config", (nitroConfig) => {
177
175
  const routes = nitroConfig.prerender?.routes || [];
@@ -1,6 +1,6 @@
1
1
  import { useTranslationHelper } from "../translationHelper.js";
2
2
  import { defineNuxtPlugin, useRuntimeConfig } from "#app";
3
- import { useFetch, useRoute, useRouter } from "#imports";
3
+ import { useRoute, useRouter } from "#imports";
4
4
  const i18nHelper = useTranslationHelper();
5
5
  const isDev = process.env.NODE_ENV !== "production";
6
6
  function interpolate(template, params) {
@@ -74,21 +74,19 @@ export default defineNuxtPlugin(async (nuxtApp) => {
74
74
  const locale = (to.params?.locale ?? i18nConfig.defaultLocale).toString();
75
75
  const routeName = to.name.replace(`localized-`, "");
76
76
  if (!i18nHelper.hasPageTranslation(locale, routeName)) {
77
- const resp = await fetch(`/_locales/${routeName}/${locale}/data.json`);
78
- if (resp.ok) {
79
- await i18nHelper.loadPageTranslations(locale, routeName, await resp.json());
80
- }
77
+ const data2 = await $fetch(`/_locales/${routeName}/${locale}/data.json`);
78
+ await i18nHelper.loadPageTranslations(locale, routeName, data2 ?? {});
81
79
  }
82
80
  }
83
81
  next();
84
82
  });
85
- const { data } = await useFetch(`/_locales/general/${initialLocale}/data.json`);
86
- await i18nHelper.loadTranslations(initialLocale, initialRouteName, data.value ?? {});
83
+ const data = await $fetch(`/_locales/general/${initialLocale}/data.json`);
84
+ await i18nHelper.loadTranslations(initialLocale, initialRouteName, data ?? {});
87
85
  if (import.meta.server) {
88
86
  const locale = (route.params?.locale ?? i18nConfig.defaultLocale).toString();
89
87
  const routeName = route.name.replace(`localized-`, "");
90
- const { data: data2 } = await useFetch(`/_locales/${routeName}/${locale}/data.json`);
91
- await i18nHelper.loadPageTranslations(initialLocale, initialRouteName, data2.value ?? {});
88
+ const data2 = await $fetch(`/_locales/${routeName}/${locale}/data.json`);
89
+ await i18nHelper.loadPageTranslations(initialLocale, initialRouteName, data2 ?? {});
92
90
  }
93
91
  return {
94
92
  provide: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-i18n-micro",
3
- "version": "1.1.10",
3
+ "version": "1.1.12",
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",
@@ -1 +0,0 @@
1
- {"id":"f1ddbdf9-9b7b-4ddb-bddb-a5d520509f71","timestamp":1724070947234,"matcher":{"static":{},"wildcard":{},"dynamic":{}},"prerendered":[]}