nuxt-i18n-micro 1.67.1 → 1.68.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.
@@ -8,5 +8,5 @@
8
8
  <link rel="prefetch" as="script" crossorigin href="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/CcDaBmcC.js">
9
9
  <link rel="prefetch" as="style" crossorigin href="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/error-500.D1x2GrnM.css">
10
10
  <link rel="prefetch" as="script" crossorigin href="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/rqhqIC0Q.js">
11
- <script type="module" src="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/ClTsZsex.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" data-nuxt-data="nuxt-app" data-ssr="false" id="__NUXT_DATA__">[{"prerenderedAt":1,"serverRendered":2},1737386232993,false]</script>
12
- <script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__NUXT_DEVTOOLS_I18N_BASE__/",buildId:"a0710ba7-e099-433f-942a-33090226304f",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
11
+ <script type="module" src="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/ClTsZsex.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" data-nuxt-data="nuxt-app" data-ssr="false" id="__NUXT_DATA__">[{"prerenderedAt":1,"serverRendered":2},1737396627941,false]</script>
12
+ <script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__NUXT_DEVTOOLS_I18N_BASE__/",buildId:"ff63dae6-d188-4ec4-992e-20f91aca2bc1",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
@@ -8,5 +8,5 @@
8
8
  <link rel="prefetch" as="script" crossorigin href="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/CcDaBmcC.js">
9
9
  <link rel="prefetch" as="style" crossorigin href="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/error-500.D1x2GrnM.css">
10
10
  <link rel="prefetch" as="script" crossorigin href="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/rqhqIC0Q.js">
11
- <script type="module" src="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/ClTsZsex.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" data-nuxt-data="nuxt-app" data-ssr="false" id="__NUXT_DATA__">[{"prerenderedAt":1,"serverRendered":2},1737386232994,false]</script>
12
- <script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__NUXT_DEVTOOLS_I18N_BASE__/",buildId:"a0710ba7-e099-433f-942a-33090226304f",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
11
+ <script type="module" src="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/ClTsZsex.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" data-nuxt-data="nuxt-app" data-ssr="false" id="__NUXT_DATA__">[{"prerenderedAt":1,"serverRendered":2},1737396627942,false]</script>
12
+ <script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__NUXT_DEVTOOLS_I18N_BASE__/",buildId:"ff63dae6-d188-4ec4-992e-20f91aca2bc1",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
@@ -1 +1 @@
1
- {"id":"a0710ba7-e099-433f-942a-33090226304f","timestamp":1737386227680}
1
+ {"id":"ff63dae6-d188-4ec4-992e-20f91aca2bc1","timestamp":1737396621792}
@@ -0,0 +1 @@
1
+ {"id":"ff63dae6-d188-4ec4-992e-20f91aca2bc1","timestamp":1737396621792,"matcher":{"static":{},"wildcard":{},"dynamic":{}},"prerendered":[]}
@@ -8,5 +8,5 @@
8
8
  <link rel="prefetch" as="script" crossorigin href="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/CcDaBmcC.js">
9
9
  <link rel="prefetch" as="style" crossorigin href="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/error-500.D1x2GrnM.css">
10
10
  <link rel="prefetch" as="script" crossorigin href="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/rqhqIC0Q.js">
11
- <script type="module" src="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/ClTsZsex.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" data-nuxt-data="nuxt-app" data-ssr="false" id="__NUXT_DATA__">[{"prerenderedAt":1,"serverRendered":2},1737386232994,false]</script>
12
- <script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__NUXT_DEVTOOLS_I18N_BASE__/",buildId:"a0710ba7-e099-433f-942a-33090226304f",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
11
+ <script type="module" src="/__NUXT_DEVTOOLS_I18N_BASE__/_nuxt/ClTsZsex.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" data-nuxt-data="nuxt-app" data-ssr="false" id="__NUXT_DATA__">[{"prerenderedAt":1,"serverRendered":2},1737396627942,false]</script>
12
+ <script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__NUXT_DEVTOOLS_I18N_BASE__/",buildId:"ff63dae6-d188-4ec4-992e-20f91aca2bc1",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.67.1",
4
+ "version": "1.68.0",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "0.8.4",
7
7
  "unbuild": "2.0.0"
package/dist/module.mjs CHANGED
@@ -1,10 +1,8 @@
1
1
  import path, { resolve } from 'node:path';
2
2
  import * as fs from 'node:fs';
3
3
  import fs__default, { readFileSync, existsSync, mkdirSync, writeFileSync } from 'node:fs';
4
- import { readFile } from 'node:fs/promises';
5
4
  import { useNuxt, defineNuxtModule, useLogger, createResolver, addTemplate, addImportsDir, addPlugin, addServerHandler, addComponentsDir, addTypeTemplate, extendPages, addPrerenderRoutes } from '@nuxt/kit';
6
5
  import { watch } from 'chokidar';
7
- import { globby } from 'globby';
8
6
  import { isPrefixAndDefaultStrategy, isPrefixStrategy, isNoPrefixStrategy, withPrefixStrategy } from 'nuxt-i18n-micro-core';
9
7
  import { fileURLToPath } from 'node:url';
10
8
  import { onDevToolsInitialized, extendServerRpc } from '@nuxt/devtools-kit';
@@ -400,34 +398,6 @@ declare module '#app' {
400
398
 
401
399
  export {}`;
402
400
  }
403
- function deepMerge(target, source) {
404
- if (typeof source !== "object" || source === null) {
405
- return target === void 0 ? source : target;
406
- }
407
- if (Array.isArray(target)) {
408
- return target;
409
- }
410
- if (source instanceof Object) {
411
- if (!(target instanceof Object) || Array.isArray(target)) {
412
- target = {};
413
- }
414
- for (const key in source) {
415
- if (key === "__proto__" || key === "constructor")
416
- continue;
417
- if (target !== null && typeof target[key] === "object" && target[key] !== null) {
418
- target[key] = deepMerge(
419
- target[key],
420
- source[key]
421
- );
422
- } else {
423
- if (target instanceof Object && !(key in target)) {
424
- target[key] = source[key];
425
- }
426
- }
427
- }
428
- }
429
- return target;
430
- }
431
401
  const module = defineNuxtModule({
432
402
  meta: {
433
403
  name: "nuxt-i18n-micro",
@@ -635,14 +605,6 @@ const module = defineNuxtModule({
635
605
  addPrerenderRoutes(prerenderRoutes);
636
606
  });
637
607
  nuxt.hook("nitro:config", (nitroConfig) => {
638
- nitroConfig.bundledStorage = nitroConfig.bundledStorage || [];
639
- nitroConfig.bundledStorage.push("/i18n-locales");
640
- nitroConfig.storage = nitroConfig.storage || {};
641
- nitroConfig.devStorage = nitroConfig.devStorage || {};
642
- nitroConfig.devStorage["i18n-locales"] = {
643
- driver: "fs",
644
- base: path.join(nuxt.options.rootDir, "server/assets/i18n-locales")
645
- };
646
608
  if (nitroConfig.imports) {
647
609
  nitroConfig.imports.presets = nitroConfig.imports.presets || [];
648
610
  nitroConfig.imports.presets.push({
@@ -681,64 +643,6 @@ const module = defineNuxtModule({
681
643
  }
682
644
  }
683
645
  });
684
- nuxt.hook("nitro:init", async (nitro) => {
685
- logger.debug("[nuxt-i18n-micro] clear storage cache");
686
- await nitro.storage.clear("i18n-locales");
687
- if (!await nitro.storage.hasItem(`i18n-locales:.gitignore`)) {
688
- const dir = path.join(nuxt.options.rootDir, "server/assets/i18n-locales");
689
- fs__default.mkdirSync(dir, { recursive: true });
690
- fs__default.writeFileSync(`${dir}/.gitignore`, "*");
691
- }
692
- const translationDir = options.translationDir ?? "";
693
- const fallbackLocale = options.fallbackLocale ?? null;
694
- const translationsByLocale = {};
695
- try {
696
- for (const rootDir of rootDirs) {
697
- const baseDir = path.resolve(rootDir, translationDir);
698
- const files = await globby("**/*.json", { cwd: baseDir });
699
- const promises = files.map(async (file) => {
700
- const filePath = path.join(baseDir, file);
701
- const content = await readFile(filePath, "utf-8");
702
- const data = JSON.parse(content);
703
- const parts = file.split("/");
704
- const locale = parts.pop()?.replace(".json", "") || "";
705
- const pageKey = parts.pop() || "general";
706
- if (!translationsByLocale[locale]) {
707
- translationsByLocale[locale] = {};
708
- }
709
- translationsByLocale[locale] = deepMerge({
710
- [pageKey]: data
711
- }, translationsByLocale[locale]);
712
- });
713
- await Promise.all(promises);
714
- }
715
- const savePromises = [];
716
- for (const [locale, translations] of Object.entries(translationsByLocale)) {
717
- for (const [key, value] of Object.entries(translations)) {
718
- const storageKey = `i18n-locales:${locale}:${key}`;
719
- const promise = (async () => {
720
- let translation = value;
721
- if (fallbackLocale) {
722
- translation = deepMerge(
723
- translation,
724
- translationsByLocale[fallbackLocale][key] ?? {}
725
- );
726
- }
727
- if (typeof translation === "object" && translation !== null) {
728
- await nitro.storage.setItem(storageKey, translation);
729
- if (options.debug) {
730
- logger.log(`[nuxt-i18n-micro] Translation saved to Nitro storage with key: ${storageKey}`);
731
- }
732
- }
733
- })();
734
- savePromises.push(promise);
735
- }
736
- }
737
- await Promise.all(savePromises);
738
- } catch (err) {
739
- logger.error("[nuxt-i18n-micro] Error processing translations:", err);
740
- }
741
- });
742
646
  if (!options.disableUpdater) {
743
647
  nuxt.hook("nitro:build:before", async (_nitro) => {
744
648
  const isProd = nuxt.options.dev === false;
@@ -1,18 +1,61 @@
1
+ import { resolve, join } from "node:path";
2
+ import { readFile } from "node:fs/promises";
1
3
  import { defineEventHandler } from "h3";
2
- import { prefixStorage } from "unstorage";
3
- import { createError, useRuntimeConfig, useStorage } from "#imports";
4
- const serverStorage = prefixStorage(useStorage(), "i18n-locales");
4
+ import { useRuntimeConfig, createError, useStorage } from "#imports";
5
+ let storageInit = false;
6
+ function deepMerge(target, source) {
7
+ for (const key in source) {
8
+ if (key === "__proto__" || key === "constructor") continue;
9
+ if (Array.isArray(source[key])) {
10
+ target[key] = source[key];
11
+ } else if (source[key] instanceof Object) {
12
+ target[key] = target[key] instanceof Object ? deepMerge(target[key], source[key]) : source[key];
13
+ } else {
14
+ target[key] = source[key];
15
+ }
16
+ }
17
+ return target;
18
+ }
5
19
  export default defineEventHandler(async (event) => {
6
20
  const { page, locale } = event.context.params;
7
21
  const config = useRuntimeConfig();
8
- const { customRegexMatcher, locales } = config.public.i18nConfig;
22
+ const { rootDirs, debug } = config.i18nConfig;
23
+ const { translationDir, fallbackLocale, customRegexMatcher, locales } = config.public.i18nConfig;
9
24
  if (customRegexMatcher && locales && !locales.map((l) => l.code).includes(locale)) {
10
25
  throw createError({ statusCode: 404 });
11
26
  }
12
- const cacheKey = `${locale}:${page}`;
13
- if (await serverStorage.hasItem(cacheKey)) {
14
- const rawContent = await serverStorage.getItem(cacheKey) || {};
27
+ const getTranslationPath = (locale2, page2) => page2 === "general" ? `${locale2}.json` : `pages/${page2}/${locale2}.json`;
28
+ let translations = {};
29
+ const serverStorage = useStorage("assets:server");
30
+ if (!storageInit) {
31
+ if (debug) console.log("[nuxt-i18n-micro] clear storage cache");
32
+ await Promise.all((await serverStorage.getKeys()).map((key) => serverStorage.removeItem(key)));
33
+ storageInit = true;
34
+ }
35
+ const cacheName = join("_locales", getTranslationPath(locale, page));
36
+ const isThereAsset = await serverStorage.hasItem(cacheName);
37
+ if (isThereAsset) {
38
+ const rawContent = await serverStorage.getItem(cacheName) ?? {};
15
39
  return typeof rawContent === "string" ? JSON.parse(rawContent) : rawContent;
16
40
  }
17
- return {};
41
+ const createPaths = (locale2) => rootDirs.map((dir) => ({
42
+ translationPath: resolve(dir, translationDir, getTranslationPath(locale2, page)),
43
+ name: `_locales/${getTranslationPath(locale2, page)}`
44
+ }));
45
+ const paths = [
46
+ ...fallbackLocale && fallbackLocale !== locale ? createPaths(fallbackLocale) : [],
47
+ ...createPaths(locale)
48
+ ];
49
+ for (const { translationPath, name } of paths) {
50
+ try {
51
+ if (debug) console.log("[nuxt-i18n-micro] load locale", translationPath, name);
52
+ const content = await readFile(translationPath, "utf-8");
53
+ const fileContent = JSON.parse(content);
54
+ translations = deepMerge(translations, fileContent);
55
+ } catch (e) {
56
+ if (debug) console.error("[nuxt-i18n-micro] load locale error", e);
57
+ }
58
+ }
59
+ await serverStorage.setItem(cacheName, translations);
60
+ return translations;
18
61
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-i18n-micro",
3
- "version": "1.67.1",
3
+ "version": "1.68.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",
@@ -1 +0,0 @@
1
- {"id":"a0710ba7-e099-433f-942a-33090226304f","timestamp":1737386227680,"matcher":{"static":{},"wildcard":{},"dynamic":{}},"prerendered":[]}