@zachhandley/ez-i18n 0.3.19 → 0.4.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.
- package/dist/runtime/index.js +49 -14
- package/package.json +1 -1
package/dist/runtime/index.js
CHANGED
|
@@ -77,6 +77,47 @@ function getLocale() {
|
|
|
77
77
|
function getTranslations() {
|
|
78
78
|
return translations.get();
|
|
79
79
|
}
|
|
80
|
+
var embeddedI18nPattern = /\[i18n:([^\]|]+)(?:\|([^\]]+))?]/g;
|
|
81
|
+
function parseEmbeddedParams(paramString) {
|
|
82
|
+
if (!paramString) return {};
|
|
83
|
+
const params = new URLSearchParams(paramString);
|
|
84
|
+
const result = {};
|
|
85
|
+
for (const [key, value] of params) {
|
|
86
|
+
result[key] = value;
|
|
87
|
+
}
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
function mergeEmbeddedParams(embeddedParams, overrideParams) {
|
|
91
|
+
const embeddedKeys = Object.keys(embeddedParams);
|
|
92
|
+
if (embeddedKeys.length === 0) return void 0;
|
|
93
|
+
const merged = { ...embeddedParams };
|
|
94
|
+
if (overrideParams) {
|
|
95
|
+
for (const key of embeddedKeys) {
|
|
96
|
+
if (key in overrideParams) {
|
|
97
|
+
merged[key] = overrideParams[key];
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return merged;
|
|
102
|
+
}
|
|
103
|
+
function formatEmbeddedString(str, overrideParams, translateKey) {
|
|
104
|
+
if (!str.includes("[i18n:")) return str;
|
|
105
|
+
return str.replace(embeddedI18nPattern, (_match, embeddedKey, paramString) => {
|
|
106
|
+
const embeddedParams = parseEmbeddedParams(paramString);
|
|
107
|
+
const mergedParams = mergeEmbeddedParams(embeddedParams, overrideParams);
|
|
108
|
+
return translateKey(embeddedKey, mergedParams);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
function translateKeyWithTranslations(trans, key, params) {
|
|
112
|
+
const value = getNestedValue(trans, key);
|
|
113
|
+
if (typeof value !== "string") {
|
|
114
|
+
if (typeof import.meta !== "undefined" && import.meta.env?.DEV) {
|
|
115
|
+
console.warn("[ez-i18n] Missing translation:", key);
|
|
116
|
+
}
|
|
117
|
+
return key;
|
|
118
|
+
}
|
|
119
|
+
return interpolate(value, params);
|
|
120
|
+
}
|
|
80
121
|
function getTranslationsWithSSRFallback() {
|
|
81
122
|
const trans = translations.get();
|
|
82
123
|
if (Object.keys(trans).length === 0) {
|
|
@@ -89,26 +130,20 @@ function getTranslationsWithSSRFallback() {
|
|
|
89
130
|
}
|
|
90
131
|
function t(key, params) {
|
|
91
132
|
const trans = getTranslationsWithSSRFallback();
|
|
92
|
-
const
|
|
93
|
-
if (
|
|
94
|
-
|
|
95
|
-
console.warn("[ez-i18n] Missing translation:", key);
|
|
96
|
-
}
|
|
97
|
-
return key;
|
|
133
|
+
const translate = (lookupKey, lookupParams) => translateKeyWithTranslations(trans, lookupKey, lookupParams);
|
|
134
|
+
if (key.includes("[i18n:")) {
|
|
135
|
+
return formatEmbeddedString(key, params, translate);
|
|
98
136
|
}
|
|
99
|
-
return
|
|
137
|
+
return translate(key, params);
|
|
100
138
|
}
|
|
101
139
|
function tc(key, params) {
|
|
102
140
|
return computed(translations, (trans) => {
|
|
103
141
|
const effectiveTrans = Object.keys(trans).length === 0 ? globalThis.__EZ_I18N__?.translations ?? trans : trans;
|
|
104
|
-
const
|
|
105
|
-
if (
|
|
106
|
-
|
|
107
|
-
console.warn("[ez-i18n] Missing translation:", key);
|
|
108
|
-
}
|
|
109
|
-
return key;
|
|
142
|
+
const translate = (lookupKey, lookupParams) => translateKeyWithTranslations(effectiveTrans, lookupKey, lookupParams);
|
|
143
|
+
if (key.includes("[i18n:")) {
|
|
144
|
+
return formatEmbeddedString(key, params, translate);
|
|
110
145
|
}
|
|
111
|
-
return
|
|
146
|
+
return translate(key, params);
|
|
112
147
|
});
|
|
113
148
|
}
|
|
114
149
|
export {
|