@zachhandley/ez-i18n 0.4.0 → 0.4.11
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.js +21 -3
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -937,27 +937,45 @@ function __deepMerge(target, ...sources) {
|
|
|
937
937
|
function getPublicLoaderCode() {
|
|
938
938
|
return `
|
|
939
939
|
async function __loadPublicJson(url, absolutePath) {
|
|
940
|
+
async function __parseJsonResponse(response) {
|
|
941
|
+
const text = await response.text();
|
|
942
|
+
if (!text) return {};
|
|
943
|
+
return JSON.parse(text);
|
|
944
|
+
}
|
|
945
|
+
|
|
940
946
|
// Browser - fetch with relative URL
|
|
941
947
|
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
|
|
942
|
-
|
|
948
|
+
const response = await fetch(url);
|
|
949
|
+
if (!response.ok) {
|
|
950
|
+
if (response.status === 404) return {};
|
|
951
|
+
throw new Error(\`Failed to fetch translations from \${url}: \${response.status}\`);
|
|
952
|
+
}
|
|
953
|
+
return __parseJsonResponse(response);
|
|
943
954
|
}
|
|
944
955
|
|
|
945
956
|
// Cloudflare Workers - use ASSETS binding (set by middleware)
|
|
946
957
|
const assets = globalThis.__EZ_I18N_ASSETS__;
|
|
947
958
|
if (assets && typeof assets.fetch === 'function') {
|
|
948
959
|
const response = await assets.fetch(new URL(url, 'https://assets.local'));
|
|
949
|
-
|
|
960
|
+
if (!response.ok) {
|
|
961
|
+
if (response.status === 404) return {};
|
|
962
|
+
throw new Error(\`Failed to fetch translations from \${url}: \${response.status}\`);
|
|
963
|
+
}
|
|
964
|
+
return __parseJsonResponse(response);
|
|
950
965
|
}
|
|
951
966
|
|
|
952
967
|
// Deno - use Deno.readTextFile
|
|
953
968
|
if (typeof Deno !== 'undefined') {
|
|
954
969
|
const content = await Deno.readTextFile(absolutePath);
|
|
970
|
+
if (!content) return {};
|
|
955
971
|
return JSON.parse(content);
|
|
956
972
|
}
|
|
957
973
|
|
|
958
974
|
// Node.js / Bun - use absolute path with node:fs
|
|
959
975
|
const { readFileSync } = await import('node:fs');
|
|
960
|
-
|
|
976
|
+
const content = readFileSync(absolutePath, 'utf-8');
|
|
977
|
+
if (!content) return {};
|
|
978
|
+
return JSON.parse(content);
|
|
961
979
|
}`;
|
|
962
980
|
}
|
|
963
981
|
function resolveConfig(config) {
|