nuxt-i18n-micro 1.60.0 → 1.62.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,61 +0,0 @@
1
- import { resolve, join } from "node:path";
2
- import { readFile } from "node:fs/promises";
3
- import { defineEventHandler } from "h3";
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
- }
19
- export default defineEventHandler(async (event) => {
20
- const { page, locale } = event.context.params;
21
- const config = useRuntimeConfig();
22
- const { rootDirs, debug } = config.i18nConfig;
23
- const { translationDir, fallbackLocale, customRegexMatcher, locales } = config.public.i18nConfig;
24
- if (customRegexMatcher && locales && !locales.map((l) => l.code).includes(locale)) {
25
- throw createError({ statusCode: 404 });
26
- }
27
- const getTranslationPath = (locale2, page2) => page2 === "general" ? `${locale2}.json` : `pages/${page2}/${locale2}.json`;
28
- const createPaths = (locale2) => rootDirs.map((dir) => ({
29
- translationPath: resolve(dir, translationDir, getTranslationPath(locale2, page)),
30
- name: `_locales/${getTranslationPath(locale2, page)}`
31
- }));
32
- const paths = [
33
- ...fallbackLocale && fallbackLocale !== locale ? createPaths(fallbackLocale) : [],
34
- ...createPaths(locale)
35
- ];
36
- let translations = {};
37
- const serverStorage = await useStorage("assets:server");
38
- if (!storageInit) {
39
- if (debug) console.log("[nuxt-i18n-micro] clear storage cache");
40
- await Promise.all((await serverStorage.getKeys()).map((key) => serverStorage.removeItem(key)));
41
- storageInit = true;
42
- }
43
- const cacheName = join("_locales", getTranslationPath(locale, page));
44
- const isThereAsset = await serverStorage.hasItem(cacheName);
45
- if (isThereAsset) {
46
- const rawContent = await serverStorage.getItem(cacheName) ?? {};
47
- return typeof rawContent === "string" ? JSON.parse(rawContent) : rawContent;
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;
61
- });