poly-lexis 0.5.2 → 0.6.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/cli/translations.js +31 -1
- package/dist/cli/translations.js.map +1 -1
- package/dist/index.d.ts +22 -1
- package/dist/index.js +50 -1
- package/dist/index.js.map +1 -1
- package/dist/scripts/verify-translations.js +21 -1
- package/dist/scripts/verify-translations.js.map +1 -1
- package/package.json +1 -1
package/dist/cli/translations.js
CHANGED
|
@@ -532,6 +532,21 @@ var init_language_fallback = __esm({
|
|
|
532
532
|
// src/translations/utils/utils.ts
|
|
533
533
|
import * as fs from "fs";
|
|
534
534
|
import * as path2 from "path";
|
|
535
|
+
function flattenObject(obj, prefix = "") {
|
|
536
|
+
const result = {};
|
|
537
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
538
|
+
const newKey = prefix ? `${prefix}.${key}` : key;
|
|
539
|
+
if (typeof value === "string") {
|
|
540
|
+
result[newKey] = value;
|
|
541
|
+
} else if (typeof value === "object" && value !== null) {
|
|
542
|
+
Object.assign(result, flattenObject(value, newKey));
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
return result;
|
|
546
|
+
}
|
|
547
|
+
function isNestedObject(obj) {
|
|
548
|
+
return Object.values(obj).some((value) => typeof value === "object" && value !== null);
|
|
549
|
+
}
|
|
535
550
|
function readTranslations(translationsPath, language) {
|
|
536
551
|
const langPath = path2.join(translationsPath, language);
|
|
537
552
|
if (!fs.existsSync(langPath)) {
|
|
@@ -543,7 +558,12 @@ function readTranslations(translationsPath, language) {
|
|
|
543
558
|
const namespace = path2.basename(file, ".json");
|
|
544
559
|
const filePath = path2.join(langPath, file);
|
|
545
560
|
const content = fs.readFileSync(filePath, "utf-8");
|
|
546
|
-
|
|
561
|
+
const parsed = JSON.parse(content);
|
|
562
|
+
if (isNestedObject(parsed)) {
|
|
563
|
+
translations[namespace] = flattenObject(parsed);
|
|
564
|
+
} else {
|
|
565
|
+
translations[namespace] = parsed;
|
|
566
|
+
}
|
|
547
567
|
}
|
|
548
568
|
return translations;
|
|
549
569
|
}
|
|
@@ -1201,6 +1221,11 @@ function preserveVariables(text) {
|
|
|
1201
1221
|
variableMap.set(placeholder, match);
|
|
1202
1222
|
placeholderIndex++;
|
|
1203
1223
|
return placeholder;
|
|
1224
|
+
}).replace(/\{([^}]+)\}/g, (match) => {
|
|
1225
|
+
const placeholder = `XXX_${placeholderIndex}_XXX`;
|
|
1226
|
+
variableMap.set(placeholder, match);
|
|
1227
|
+
placeholderIndex++;
|
|
1228
|
+
return placeholder;
|
|
1204
1229
|
});
|
|
1205
1230
|
return { textWithPlaceholders, variableMap };
|
|
1206
1231
|
}
|
|
@@ -1295,6 +1320,11 @@ function preserveVariables2(text) {
|
|
|
1295
1320
|
variableMap.set(placeholder, match);
|
|
1296
1321
|
placeholderIndex++;
|
|
1297
1322
|
return placeholder;
|
|
1323
|
+
}).replace(/\{([^}]+)\}/g, (match) => {
|
|
1324
|
+
const placeholder = `XXX_${placeholderIndex}_XXX`;
|
|
1325
|
+
variableMap.set(placeholder, match);
|
|
1326
|
+
placeholderIndex++;
|
|
1327
|
+
return placeholder;
|
|
1298
1328
|
});
|
|
1299
1329
|
return { textWithPlaceholders, variableMap };
|
|
1300
1330
|
}
|