@uipkge/nuxt 0.1.16 → 0.1.18
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/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Production client plugin — handles CDN translation loading on locale switch.
|
|
3
|
+
*
|
|
4
|
+
* The server plugin loads translations for the initial SSR locale, but its
|
|
5
|
+
* i18n:beforeLocaleSwitch hook dies after the response. This lightweight client
|
|
6
|
+
* plugin re-registers the hook so locale switches load new CDN translations
|
|
7
|
+
* without a full page reload.
|
|
8
|
+
*
|
|
9
|
+
* For SPA/CSR routes (no SSR), it also loads the initial locale from CDN.
|
|
10
|
+
*
|
|
11
|
+
* No sync logic, no API key, no $t patching — zero overhead beyond the fetch.
|
|
12
|
+
*/
|
|
13
|
+
declare const _default: (nuxtApp: unknown) => Promise<void>;
|
|
14
|
+
export default _default;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { defineNuxtPlugin, useRuntimeConfig } from "#app";
|
|
2
|
+
export default defineNuxtPlugin(async (nuxtApp) => {
|
|
3
|
+
const { projectId, cdnUrl, environment, locale: configLocale } = useRuntimeConfig().public.i18now;
|
|
4
|
+
if (!projectId) return;
|
|
5
|
+
const i18n = nuxtApp.$i18n ?? nuxtApp.vueApp.config.globalProperties.$i18n;
|
|
6
|
+
const currentLocale = i18n?.locale?.value ?? i18n?.locale ?? configLocale;
|
|
7
|
+
async function loadLocale(locale) {
|
|
8
|
+
try {
|
|
9
|
+
const url = `${cdnUrl}/${projectId}/publish/${environment}/${locale}.json`;
|
|
10
|
+
const res = await fetch(url, { signal: AbortSignal.timeout(5e3) });
|
|
11
|
+
if (!res.ok) return;
|
|
12
|
+
const data = await res.json();
|
|
13
|
+
if (typeof data !== "object" || data === null || Array.isArray(data)) return;
|
|
14
|
+
if (i18n) i18n.mergeLocaleMessage(locale, data);
|
|
15
|
+
} catch {
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
const ssrKeys = nuxtApp.payload.i18nowCdnKeys;
|
|
19
|
+
if (!ssrKeys || ssrKeys.length === 0) {
|
|
20
|
+
await loadLocale(currentLocale);
|
|
21
|
+
}
|
|
22
|
+
let activeLocale = currentLocale;
|
|
23
|
+
nuxtApp.hook("i18n:beforeLocaleSwitch", async ({ newLocale }) => {
|
|
24
|
+
if (newLocale === activeLocale) return;
|
|
25
|
+
activeLocale = newLocale;
|
|
26
|
+
await loadLocale(newLocale);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
@@ -87,7 +87,7 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
87
87
|
return;
|
|
88
88
|
}
|
|
89
89
|
syncer.setExistingKeys(Object.keys(data));
|
|
90
|
-
i18nGlobal?.
|
|
90
|
+
i18nGlobal?.mergeLocaleMessage(targetLocale, data);
|
|
91
91
|
} else {
|
|
92
92
|
syncer.setExistingKeys([]);
|
|
93
93
|
if (import.meta.dev) {
|
|
@@ -107,7 +107,13 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
109
|
}
|
|
110
|
-
|
|
110
|
+
let currentLocale = locale;
|
|
111
|
+
const ssrKeys = nuxtApp.payload.i18nowCdnKeys;
|
|
112
|
+
if (ssrKeys && ssrKeys.length > 0) {
|
|
113
|
+
syncer.setExistingKeys(ssrKeys);
|
|
114
|
+
} else {
|
|
115
|
+
await loadLocale(locale);
|
|
116
|
+
}
|
|
111
117
|
const originalGlobalT = nuxtApp.vueApp.config.globalProperties.$t;
|
|
112
118
|
if (originalGlobalT) {
|
|
113
119
|
nuxtApp.vueApp.config.globalProperties.$t = function(key, defaultValue, ...rest) {
|
|
@@ -120,6 +126,8 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
120
126
|
};
|
|
121
127
|
}
|
|
122
128
|
nuxtApp.hook("i18n:beforeLocaleSwitch", async ({ newLocale }) => {
|
|
129
|
+
if (newLocale === currentLocale) return;
|
|
130
|
+
currentLocale = newLocale;
|
|
123
131
|
await loadLocale(newLocale);
|
|
124
132
|
});
|
|
125
133
|
return {
|
package/dist/runtime/plugin.js
CHANGED
|
@@ -23,7 +23,8 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
23
23
|
return;
|
|
24
24
|
}
|
|
25
25
|
const messages = data;
|
|
26
|
-
if (i18n) i18n.
|
|
26
|
+
if (i18n) i18n.mergeLocaleMessage(locale, messages);
|
|
27
|
+
nuxtApp.payload.i18nowCdnKeys = Object.keys(messages);
|
|
27
28
|
} catch (err) {
|
|
28
29
|
if (import.meta.dev) {
|
|
29
30
|
console.warn(`[i18now] Failed to load locale '${locale}' from CDN:`, err);
|
|
@@ -31,8 +32,11 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
31
32
|
}
|
|
32
33
|
}
|
|
33
34
|
const currentLocale = i18n?.locale?.value ?? i18n?.locale ?? configLocale;
|
|
35
|
+
let activeLocale = currentLocale;
|
|
34
36
|
await loadLocale(currentLocale);
|
|
35
37
|
nuxtApp.hook("i18n:beforeLocaleSwitch", async ({ newLocale }) => {
|
|
38
|
+
if (newLocale === activeLocale) return;
|
|
39
|
+
activeLocale = newLocale;
|
|
36
40
|
await loadLocale(newLocale);
|
|
37
41
|
});
|
|
38
42
|
});
|