nuxt-i18n-micro 1.1.11 → 1.1.13
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 +43 -0
- package/dist/client/200.html +1 -1
- package/dist/client/404.html +1 -1
- package/dist/client/_nuxt/builds/latest.json +1 -1
- package/dist/client/_nuxt/builds/meta/33e4725a-6e72-421d-9f05-ad578b99d841.json +1 -0
- package/dist/client/index.html +1 -1
- package/dist/module.json +1 -1
- package/dist/module.mjs +8 -6
- package/dist/runtime/plugins/01.plugin.d.ts +0 -13
- package/dist/runtime/plugins/01.plugin.js +7 -9
- package/package.json +1 -1
- package/dist/client/_nuxt/builds/meta/9969f09f-b918-4abc-aac8-448faa6d5c24.json +0 -1
package/README.md
CHANGED
|
@@ -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
|
|
package/dist/client/200.html
CHANGED
|
@@ -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:"
|
|
11
|
+
<script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-i18n-micro",buildId:"33e4725a-6e72-421d-9f05-ad578b99d841",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
|
package/dist/client/404.html
CHANGED
|
@@ -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:"
|
|
11
|
+
<script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-i18n-micro",buildId:"33e4725a-6e72-421d-9f05-ad578b99d841",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"id":"
|
|
1
|
+
{"id":"33e4725a-6e72-421d-9f05-ad578b99d841","timestamp":1724083257474}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"id":"33e4725a-6e72-421d-9f05-ad578b99d841","timestamp":1724083257474,"matcher":{"static":{},"wildcard":{},"dynamic":{}},"prerendered":[]}
|
package/dist/client/index.html
CHANGED
|
@@ -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:"
|
|
11
|
+
<script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-i18n-micro",buildId:"33e4725a-6e72-421d-9f05-ad578b99d841",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -33,7 +33,7 @@ function setupDevToolsUI(options, resolve) {
|
|
|
33
33
|
const localesDir = path.join(nuxt.options.rootDir, options.translationDir || "locales");
|
|
34
34
|
const pagesDir = path.join(nuxt.options.rootDir, options.translationDir || "locales", "pages");
|
|
35
35
|
const localeFiles = fs.readdirSync(localesDir);
|
|
36
|
-
const
|
|
36
|
+
const pageDirs = fs.readdirSync(pagesDir).filter((file) => fs.lstatSync(path.join(pagesDir, file)).isDirectory());
|
|
37
37
|
const locales = options.locales?.map((locale) => locale.code) || [];
|
|
38
38
|
return locales.map((locale) => {
|
|
39
39
|
const files = localeFiles.filter((file) => file.startsWith(locale));
|
|
@@ -42,11 +42,13 @@ function setupDevToolsUI(options, resolve) {
|
|
|
42
42
|
acc[file] = JSON.parse(fs.readFileSync(filePath, "utf-8"));
|
|
43
43
|
return acc;
|
|
44
44
|
}, {});
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
45
|
+
pageDirs.forEach((dir) => {
|
|
46
|
+
const pageLocaleFilePath = path.join(pagesDir, dir, `${locale}.json`);
|
|
47
|
+
if (fs.existsSync(pageLocaleFilePath)) {
|
|
48
|
+
const fileKey = path.join(dir, `${locale}.json`);
|
|
49
|
+
content[fileKey] = JSON.parse(fs.readFileSync(pageLocaleFilePath, "utf-8"));
|
|
50
|
+
files.push(fileKey);
|
|
51
|
+
}
|
|
50
52
|
});
|
|
51
53
|
return { locale, files, content };
|
|
52
54
|
});
|
|
@@ -45,19 +45,6 @@ declare module 'nuxt/dist/app/nuxt' {
|
|
|
45
45
|
interface NuxtApp extends PluginsInjections {
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
|
-
declare module '@vue/runtime-core' {
|
|
49
|
-
interface ComponentCustomProperties {
|
|
50
|
-
$getLocale: () => string;
|
|
51
|
-
$getLocales: () => string[];
|
|
52
|
-
$t: <T extends Record<string, string | number | boolean>>(key: string, params?: T, defaultValue?: string) => string | number | boolean | Translations | PluralTranslations | unknown[] | unknown | null;
|
|
53
|
-
$tc: (key: string, count: number, defaultValue?: string) => string;
|
|
54
|
-
$has: (key: string) => boolean;
|
|
55
|
-
$mergeTranslations: (newTranslations: Translations) => void;
|
|
56
|
-
$switchLocale: (locale: string) => void;
|
|
57
|
-
$localeRoute: (to: RouteLocationRaw, locale?: string) => RouteLocationRaw;
|
|
58
|
-
$loadPageTranslations: (locale: string, routeName: string) => Promise<void>;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
48
|
declare module 'vue' {
|
|
62
49
|
interface ComponentCustomProperties extends PluginsInjections {
|
|
63
50
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useTranslationHelper } from "../translationHelper.js";
|
|
2
2
|
import { defineNuxtPlugin, useRuntimeConfig } from "#app";
|
|
3
|
-
import {
|
|
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
|
|
78
|
-
|
|
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
|
|
86
|
-
await i18nHelper.loadTranslations(initialLocale, initialRouteName, data
|
|
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
|
|
91
|
-
await i18nHelper.loadPageTranslations(initialLocale, initialRouteName, data2
|
|
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.
|
|
3
|
+
"version": "1.1.13",
|
|
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":"9969f09f-b918-4abc-aac8-448faa6d5c24","timestamp":1724077235706,"matcher":{"static":{},"wildcard":{},"dynamic":{}},"prerendered":[]}
|