@zachhandley/ez-i18n 0.3.14 → 0.3.16
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/index.d.ts +2 -2
- package/dist/index.js +15 -16
- package/dist/middleware.js +5 -4
- package/dist/runtime/index.d.ts +1 -0
- package/dist/runtime/index.js +2 -2
- package/dist/{types-Cg77gLzO.d.ts → types-CE7B9s_c.d.ts} +8 -0
- package/dist/utils/index.d.ts +2 -2
- package/dist/utils/index.js +0 -3
- package/package.json +1 -1
- package/src/components/EzI18nHead.astro +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AstroIntegration } from 'astro';
|
|
2
|
-
import { E as EzI18nConfig } from './types-
|
|
3
|
-
export { T as TranslateFunction } from './types-
|
|
2
|
+
import { E as EzI18nConfig } from './types-CE7B9s_c.js';
|
|
3
|
+
export { T as TranslateFunction } from './types-CE7B9s_c.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Comprehensive locale database for ez-i18n
|
package/dist/index.js
CHANGED
|
@@ -152,9 +152,6 @@ function toRelativeImport(absolutePath, projectRoot) {
|
|
|
152
152
|
}
|
|
153
153
|
function toGlobPattern(baseDir, projectRoot) {
|
|
154
154
|
const relativePath = path.relative(projectRoot, baseDir).replace(/\\/g, "/");
|
|
155
|
-
if (relativePath.startsWith("public/") || relativePath === "public") {
|
|
156
|
-
return null;
|
|
157
|
-
}
|
|
158
155
|
return `/${relativePath}/**/*.json`;
|
|
159
156
|
}
|
|
160
157
|
function isInPublicDir(filePath, projectRoot) {
|
|
@@ -896,25 +893,27 @@ function __deepMerge(target, ...sources) {
|
|
|
896
893
|
function getPublicLoaderCode() {
|
|
897
894
|
return `
|
|
898
895
|
async function __loadPublicJson(url, absolutePath) {
|
|
899
|
-
// Browser -
|
|
900
|
-
if (typeof window !== 'undefined') {
|
|
896
|
+
// Browser - fetch with relative URL
|
|
897
|
+
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
|
|
901
898
|
return fetch(url).then(r => r.json());
|
|
902
899
|
}
|
|
903
900
|
|
|
904
|
-
//
|
|
905
|
-
const
|
|
906
|
-
|
|
907
|
-
|
|
901
|
+
// Cloudflare Workers - use ASSETS binding (set by middleware)
|
|
902
|
+
const assets = globalThis.__EZ_I18N_ASSETS__;
|
|
903
|
+
if (assets && typeof assets.fetch === 'function') {
|
|
904
|
+
const response = await assets.fetch(new URL(url, 'https://assets.local'));
|
|
905
|
+
return response.json();
|
|
906
|
+
}
|
|
908
907
|
|
|
909
|
-
//
|
|
910
|
-
if (
|
|
911
|
-
|
|
908
|
+
// Deno - use Deno.readTextFile
|
|
909
|
+
if (typeof Deno !== 'undefined') {
|
|
910
|
+
const content = await Deno.readTextFile(absolutePath);
|
|
911
|
+
return JSON.parse(content);
|
|
912
912
|
}
|
|
913
913
|
|
|
914
|
-
//
|
|
914
|
+
// Node.js / Bun - use absolute path with node:fs
|
|
915
915
|
const { readFileSync } = await import('node:fs');
|
|
916
|
-
|
|
917
|
-
return JSON.parse(content);
|
|
916
|
+
return JSON.parse(readFileSync(absolutePath, 'utf-8'));
|
|
918
917
|
}`;
|
|
919
918
|
}
|
|
920
919
|
function resolveConfig(config) {
|
|
@@ -967,7 +966,7 @@ function ezI18n(config) {
|
|
|
967
966
|
import { initLocale, setTranslations } from '@zachhandley/ez-i18n/runtime';
|
|
968
967
|
|
|
969
968
|
document.addEventListener('astro:after-swap', () => {
|
|
970
|
-
const initData = globalThis.
|
|
969
|
+
const initData = globalThis.__EZ_I18N__;
|
|
971
970
|
if (initData) {
|
|
972
971
|
initLocale(initData.locale, initData.translations);
|
|
973
972
|
setTranslations(initData.translations);
|
package/dist/middleware.js
CHANGED
|
@@ -39,13 +39,14 @@ var onRequest = defineMiddleware(async ({ cookies, request, locals, redirect },
|
|
|
39
39
|
locale = browserLang;
|
|
40
40
|
}
|
|
41
41
|
locals.locale = locale;
|
|
42
|
+
const runtime = locals.runtime;
|
|
43
|
+
if (runtime?.env?.ASSETS) {
|
|
44
|
+
globalThis.__EZ_I18N_ASSETS__ = runtime.env.ASSETS;
|
|
45
|
+
}
|
|
42
46
|
try {
|
|
43
|
-
if (!import.meta.env.SITE && !globalThis.__EZ_I18N_ORIGIN__) {
|
|
44
|
-
globalThis.__EZ_I18N_ORIGIN__ = url.origin;
|
|
45
|
-
}
|
|
46
47
|
const { loadTranslations } = await import("ez-i18n:translations");
|
|
47
48
|
locals.translations = await loadTranslations(locale);
|
|
48
|
-
globalThis.
|
|
49
|
+
globalThis.__EZ_I18N__ = {
|
|
49
50
|
locale,
|
|
50
51
|
translations: locals.translations
|
|
51
52
|
};
|
package/dist/runtime/index.d.ts
CHANGED
package/dist/runtime/index.js
CHANGED
|
@@ -81,7 +81,7 @@ function getTranslations() {
|
|
|
81
81
|
function getTranslationsWithSSRFallback() {
|
|
82
82
|
const trans = translations.get();
|
|
83
83
|
if (Object.keys(trans).length === 0) {
|
|
84
|
-
const ssrTrans = globalThis.
|
|
84
|
+
const ssrTrans = globalThis.__EZ_I18N__?.translations;
|
|
85
85
|
if (ssrTrans) {
|
|
86
86
|
return ssrTrans;
|
|
87
87
|
}
|
|
@@ -101,7 +101,7 @@ function t(key, params) {
|
|
|
101
101
|
}
|
|
102
102
|
function tc(key, params) {
|
|
103
103
|
return computed(translations, (trans) => {
|
|
104
|
-
const effectiveTrans = Object.keys(trans).length === 0 ? globalThis.
|
|
104
|
+
const effectiveTrans = Object.keys(trans).length === 0 ? globalThis.__EZ_I18N__?.translations ?? trans : trans;
|
|
105
105
|
const value = getNestedValue(effectiveTrans, key);
|
|
106
106
|
if (typeof value !== "string") {
|
|
107
107
|
if (typeof import.meta !== "undefined" && import.meta.env?.DEV) {
|
|
@@ -84,6 +84,10 @@ type TranslateFunction = (key: string, params?: Record<string, string | number>)
|
|
|
84
84
|
/**
|
|
85
85
|
* Augment Astro's locals type
|
|
86
86
|
*/
|
|
87
|
+
interface EzI18nContext {
|
|
88
|
+
locale: string;
|
|
89
|
+
translations: Record<string, unknown>;
|
|
90
|
+
}
|
|
87
91
|
declare global {
|
|
88
92
|
namespace App {
|
|
89
93
|
interface Locals {
|
|
@@ -95,6 +99,10 @@ declare global {
|
|
|
95
99
|
t: TranslateFunction;
|
|
96
100
|
}
|
|
97
101
|
}
|
|
102
|
+
var __EZ_I18N__: EzI18nContext | undefined;
|
|
103
|
+
var __EZ_I18N_ASSETS__: {
|
|
104
|
+
fetch: (req: Request | URL | string) => Promise<Response>;
|
|
105
|
+
} | undefined;
|
|
98
106
|
}
|
|
99
107
|
|
|
100
108
|
export type { EzI18nConfig as E, LocaleTranslationPath as L, TranslateFunction as T, TranslationsConfig as a, TranslationCache as b };
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { L as LocaleTranslationPath, a as TranslationsConfig, b as TranslationCache } from '../types-
|
|
1
|
+
import { L as LocaleTranslationPath, a as TranslationsConfig, b as TranslationCache } from '../types-CE7B9s_c.js';
|
|
2
2
|
|
|
3
3
|
type PathType = 'file' | 'folder' | 'glob' | 'array';
|
|
4
4
|
/**
|
|
@@ -56,7 +56,7 @@ declare function toRelativeImport(absolutePath: string, projectRoot: string): st
|
|
|
56
56
|
* In virtual modules, globs must start with '/' (project root relative).
|
|
57
57
|
* Returns null if the path is in public/ (can't use import.meta.glob for public files).
|
|
58
58
|
*/
|
|
59
|
-
declare function toGlobPattern(baseDir: string, projectRoot: string): string
|
|
59
|
+
declare function toGlobPattern(baseDir: string, projectRoot: string): string;
|
|
60
60
|
/**
|
|
61
61
|
* Get namespace from file path relative to locale base directory.
|
|
62
62
|
*
|
package/dist/utils/index.js
CHANGED
|
@@ -173,9 +173,6 @@ function toRelativeImport(absolutePath, projectRoot) {
|
|
|
173
173
|
}
|
|
174
174
|
function toGlobPattern(baseDir, projectRoot) {
|
|
175
175
|
const relativePath = path.relative(projectRoot, baseDir).replace(/\\/g, "/");
|
|
176
|
-
if (relativePath.startsWith("public/") || relativePath === "public") {
|
|
177
|
-
return null;
|
|
178
|
-
}
|
|
179
176
|
return `/${relativePath}/**/*.json`;
|
|
180
177
|
}
|
|
181
178
|
function getNamespaceFromPath(filePath, localeDir) {
|
package/package.json
CHANGED
|
@@ -44,7 +44,7 @@ const serializedTranslations = JSON.stringify(translations);
|
|
|
44
44
|
const translations = JSON.parse(script.dataset.ezI18nTranslations || '{}');
|
|
45
45
|
|
|
46
46
|
// Store for runtime initialization (globalThis is SSR-safe and equals window in browsers)
|
|
47
|
-
globalThis.
|
|
47
|
+
globalThis.__EZ_I18N__ = { locale, translations };
|
|
48
48
|
})();
|
|
49
49
|
</script>
|
|
50
50
|
|
|
@@ -53,9 +53,9 @@ const serializedTranslations = JSON.stringify(translations);
|
|
|
53
53
|
import { initLocale, setTranslations } from '@zachhandley/ez-i18n/runtime';
|
|
54
54
|
|
|
55
55
|
// Get initialization data from inline script
|
|
56
|
-
// Note: Don't delete
|
|
56
|
+
// Note: Don't delete __EZ_I18N__ here - other bundles (Vue, React)
|
|
57
57
|
// may need to read it to initialize their own store instances
|
|
58
|
-
const initData =
|
|
58
|
+
const initData = globalThis.__EZ_I18N__;
|
|
59
59
|
if (initData) {
|
|
60
60
|
initLocale(initData.locale, initData.translations);
|
|
61
61
|
setTranslations(initData.translations);
|