@scoutello/i18n-magic 0.57.2 ā 0.57.3
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/commands/sync-locales.d.ts.map +1 -1
- package/dist/commands/sync-locales.js +186 -98
- package/dist/commands/sync-locales.js.map +1 -1
- package/dist/lib/utils.d.ts +6 -2
- package/dist/lib/utils.d.ts.map +1 -1
- package/dist/lib/utils.js +17 -12
- package/dist/lib/utils.js.map +1 -1
- package/package.json +10 -8
- package/src/commands/sync-locales.ts +254 -155
- package/src/lib/utils.ts +19 -10
- package/dist/commands/create-pruned-namespace-automated.d.ts +0 -20
- package/dist/commands/create-pruned-namespace-automated.d.ts.map +0 -1
- package/dist/commands/create-pruned-namespace-automated.js +0 -98
- package/dist/commands/create-pruned-namespace-automated.js.map +0 -1
- package/dist/commands/create-pruned-namespace.d.ts +0 -3
- package/dist/commands/create-pruned-namespace.d.ts.map +0 -1
- package/dist/commands/create-pruned-namespace.js +0 -123
- package/dist/commands/create-pruned-namespace.js.map +0 -1
- package/dist/i18n-magic.cjs.development.js +0 -1787
- package/dist/i18n-magic.cjs.development.js.map +0 -1
- package/dist/i18n-magic.cjs.production.min.js +0 -2
- package/dist/i18n-magic.cjs.production.min.js.map +0 -1
- package/dist/i18n-magic.esm.js +0 -1776
- package/dist/i18n-magic.esm.js.map +0 -1
package/src/lib/utils.ts
CHANGED
|
@@ -138,7 +138,10 @@ export const loadLocalesFile = async (
|
|
|
138
138
|
| ((locale: string, namespace: string) => Promise<Record<string, string>>),
|
|
139
139
|
locale: string,
|
|
140
140
|
namespace: string,
|
|
141
|
+
options?: { silent?: boolean },
|
|
141
142
|
) => {
|
|
143
|
+
const silent = options?.silent ?? false
|
|
144
|
+
|
|
142
145
|
if (typeof loadPath === "string") {
|
|
143
146
|
const resolvedPath = loadPath
|
|
144
147
|
.replace("{{lng}}", locale)
|
|
@@ -146,7 +149,9 @@ export const loadLocalesFile = async (
|
|
|
146
149
|
|
|
147
150
|
// Check if file exists, return empty object if it doesn't
|
|
148
151
|
if (!fs.existsSync(resolvedPath)) {
|
|
149
|
-
|
|
152
|
+
if (!silent) {
|
|
153
|
+
console.log(`š Creating new namespace file: ${resolvedPath}`)
|
|
154
|
+
}
|
|
150
155
|
return {}
|
|
151
156
|
}
|
|
152
157
|
|
|
@@ -568,12 +573,16 @@ export const findExistingTranslations = async (
|
|
|
568
573
|
loadPath:
|
|
569
574
|
| string
|
|
570
575
|
| ((locale: string, namespace: string) => Promise<Record<string, string>>),
|
|
576
|
+
options?: { silent?: boolean },
|
|
571
577
|
): Promise<Record<string, string | null>> => {
|
|
578
|
+
const silent = options?.silent ?? false
|
|
579
|
+
const log = silent ? () => {} : console.log
|
|
580
|
+
|
|
572
581
|
// Load all namespace files in parallel first
|
|
573
582
|
const namespaceKeys: Record<string, Record<string, string>> = {}
|
|
574
583
|
const loadPromises = namespaces.map(async (namespace) => {
|
|
575
584
|
try {
|
|
576
|
-
const existingKeys = await loadLocalesFile(loadPath, locale, namespace)
|
|
585
|
+
const existingKeys = await loadLocalesFile(loadPath, locale, namespace, { silent })
|
|
577
586
|
namespaceKeys[namespace] = existingKeys
|
|
578
587
|
} catch (error) {
|
|
579
588
|
namespaceKeys[namespace] = {}
|
|
@@ -583,21 +592,21 @@ export const findExistingTranslations = async (
|
|
|
583
592
|
await Promise.all(loadPromises)
|
|
584
593
|
|
|
585
594
|
// Log how many keys were found in each namespace for the default locale
|
|
586
|
-
|
|
595
|
+
log(`\nš Searching for existing translations in ${locale}:`)
|
|
587
596
|
for (const namespace of namespaces) {
|
|
588
597
|
const nsKeys = Object.keys(namespaceKeys[namespace] || {})
|
|
589
|
-
|
|
598
|
+
log(` š ${namespace}.json: ${nsKeys.length} keys available`)
|
|
590
599
|
// Show sample keys from the namespace (first 3)
|
|
591
600
|
if (nsKeys.length > 0) {
|
|
592
601
|
const sampleKeys = nsKeys.slice(0, 3)
|
|
593
|
-
|
|
602
|
+
log(` Sample keys: ${sampleKeys.join(", ")}${nsKeys.length > 3 ? "..." : ""}`)
|
|
594
603
|
}
|
|
595
604
|
}
|
|
596
605
|
|
|
597
606
|
// Show sample of keys we're searching for
|
|
598
607
|
if (keys.length > 0) {
|
|
599
608
|
const sampleSearchKeys = keys.slice(0, 3)
|
|
600
|
-
|
|
609
|
+
log(`\n š Looking for keys like: ${sampleSearchKeys.join(", ")}${keys.length > 3 ? "..." : ""}`)
|
|
601
610
|
}
|
|
602
611
|
|
|
603
612
|
// Now find translations for all keys
|
|
@@ -630,14 +639,14 @@ export const findExistingTranslations = async (
|
|
|
630
639
|
0,
|
|
631
640
|
)
|
|
632
641
|
const notFound = keys.length - totalFound
|
|
633
|
-
|
|
642
|
+
log(`\nš Search results for ${keys.length} missing keys:`)
|
|
634
643
|
for (const [namespace, count] of Object.entries(foundInNamespace)) {
|
|
635
|
-
|
|
644
|
+
log(` ā
Found ${count} keys in ${namespace}.json`)
|
|
636
645
|
}
|
|
637
646
|
if (notFound > 0) {
|
|
638
|
-
|
|
647
|
+
log(` ā ${notFound} keys not found in any namespace`)
|
|
639
648
|
}
|
|
640
|
-
|
|
649
|
+
log("")
|
|
641
650
|
|
|
642
651
|
return results
|
|
643
652
|
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { Configuration } from "../lib/types";
|
|
2
|
-
export interface PruneOptions {
|
|
3
|
-
sourceNamespace: string;
|
|
4
|
-
newNamespace: string;
|
|
5
|
-
globPatterns: string[];
|
|
6
|
-
}
|
|
7
|
-
export interface PruneResult {
|
|
8
|
-
locale: string;
|
|
9
|
-
keyCount: number;
|
|
10
|
-
success: boolean;
|
|
11
|
-
error?: string;
|
|
12
|
-
}
|
|
13
|
-
export interface PruneResponse {
|
|
14
|
-
success: boolean;
|
|
15
|
-
message: string;
|
|
16
|
-
keysCount: number;
|
|
17
|
-
results?: PruneResult[];
|
|
18
|
-
}
|
|
19
|
-
export declare const createPrunedNamespaceAutomated: (config: Configuration, options: PruneOptions) => Promise<PruneResponse>;
|
|
20
|
-
//# sourceMappingURL=create-pruned-namespace-automated.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"create-pruned-namespace-automated.d.ts","sourceRoot":"","sources":["../../src/commands/create-pruned-namespace-automated.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAQjD,MAAM,WAAW,YAAY;IAC3B,eAAe,EAAE,MAAM,CAAA;IACvB,YAAY,EAAE,MAAM,CAAA;IACpB,YAAY,EAAE,MAAM,EAAE,CAAA;CACvB;AAED,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,EAAE,MAAM,CAAA;IAChB,OAAO,EAAE,OAAO,CAAA;IAChB,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAED,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,OAAO,CAAA;IAChB,OAAO,EAAE,MAAM,CAAA;IACf,SAAS,EAAE,MAAM,CAAA;IACjB,OAAO,CAAC,EAAE,WAAW,EAAE,CAAA;CACxB;AAED,eAAO,MAAM,8BAA8B,GACzC,QAAQ,aAAa,EACrB,SAAS,YAAY,KACpB,OAAO,CAAC,aAAa,CAkIvB,CAAA"}
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.createPrunedNamespaceAutomated = void 0;
|
|
7
|
-
const fast_glob_1 = __importDefault(require("fast-glob"));
|
|
8
|
-
const i18next_scanner_1 = require("i18next-scanner");
|
|
9
|
-
const node_fs_1 = __importDefault(require("node:fs"));
|
|
10
|
-
const utils_1 = require("../lib/utils");
|
|
11
|
-
const createPrunedNamespaceAutomated = async (config, options) => {
|
|
12
|
-
const { namespaces, loadPath, savePath, locales, defaultNamespace } = config;
|
|
13
|
-
const { sourceNamespace, newNamespace, globPatterns } = options;
|
|
14
|
-
// Validate inputs
|
|
15
|
-
if (!namespaces.includes(sourceNamespace)) {
|
|
16
|
-
throw new Error(`Source namespace '${sourceNamespace}' not found in configuration`);
|
|
17
|
-
}
|
|
18
|
-
if (namespaces.includes(newNamespace)) {
|
|
19
|
-
throw new Error(`Namespace '${newNamespace}' already exists`);
|
|
20
|
-
}
|
|
21
|
-
console.log(`Creating pruned namespace '${newNamespace}' from '${sourceNamespace}'`);
|
|
22
|
-
console.log(`Using glob patterns: ${globPatterns.join(", ")}`);
|
|
23
|
-
// Extract keys from files matching the glob patterns
|
|
24
|
-
const parser = new i18next_scanner_1.Parser({
|
|
25
|
-
nsSeparator: false,
|
|
26
|
-
keySeparator: false,
|
|
27
|
-
});
|
|
28
|
-
const files = await (0, fast_glob_1.default)([...globPatterns, "!**/node_modules/**"]);
|
|
29
|
-
console.log(`Found ${files.length} files to scan`);
|
|
30
|
-
const extractedKeys = [];
|
|
31
|
-
for (const file of files) {
|
|
32
|
-
const content = node_fs_1.default.readFileSync(file, "utf-8");
|
|
33
|
-
parser.parseFuncFromString(content, { list: ["t"] }, (key) => {
|
|
34
|
-
extractedKeys.push(key);
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
const uniqueExtractedKeys = (0, utils_1.removeDuplicatesFromArray)(extractedKeys);
|
|
38
|
-
console.log(`Found ${uniqueExtractedKeys.length} unique translation keys`);
|
|
39
|
-
// Filter keys that belong to the source namespace
|
|
40
|
-
const relevantKeys = [];
|
|
41
|
-
for (const key of uniqueExtractedKeys) {
|
|
42
|
-
const pureKey = (0, utils_1.getPureKey)(key, sourceNamespace, sourceNamespace === defaultNamespace);
|
|
43
|
-
if (pureKey) {
|
|
44
|
-
relevantKeys.push(pureKey);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
console.log(`Found ${relevantKeys.length} keys from namespace '${sourceNamespace}'`);
|
|
48
|
-
if (relevantKeys.length === 0) {
|
|
49
|
-
console.log("No relevant keys found. Exiting...");
|
|
50
|
-
return {
|
|
51
|
-
success: false,
|
|
52
|
-
message: "No relevant keys found",
|
|
53
|
-
keysCount: 0,
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
// Get translations from source namespace and create new namespace files
|
|
57
|
-
const results = [];
|
|
58
|
-
for (const locale of locales) {
|
|
59
|
-
try {
|
|
60
|
-
// Load source namespace translations
|
|
61
|
-
const sourceTranslations = await (0, utils_1.loadLocalesFile)(loadPath, locale, sourceNamespace);
|
|
62
|
-
// Create new namespace with only the keys used in the glob pattern files
|
|
63
|
-
const newNamespaceTranslations = {};
|
|
64
|
-
for (const key of relevantKeys) {
|
|
65
|
-
if (sourceTranslations[key]) {
|
|
66
|
-
newNamespaceTranslations[key] = sourceTranslations[key];
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
// Write the new namespace file
|
|
70
|
-
await (0, utils_1.writeLocalesFile)(savePath, locale, newNamespace, newNamespaceTranslations);
|
|
71
|
-
const keyCount = Object.keys(newNamespaceTranslations).length;
|
|
72
|
-
console.log(`Created pruned namespace '${newNamespace}' for locale '${locale}' with ${keyCount} keys`);
|
|
73
|
-
results.push({
|
|
74
|
-
locale,
|
|
75
|
-
keyCount,
|
|
76
|
-
success: true,
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
catch (error) {
|
|
80
|
-
console.error(`Error creating pruned namespace for locale '${locale}':`, error);
|
|
81
|
-
results.push({
|
|
82
|
-
locale,
|
|
83
|
-
keyCount: 0,
|
|
84
|
-
success: false,
|
|
85
|
-
error: error instanceof Error ? error.message : String(error),
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
console.log(`ā
Successfully created pruned namespace '${newNamespace}'`);
|
|
90
|
-
return {
|
|
91
|
-
success: true,
|
|
92
|
-
message: `Created pruned namespace '${newNamespace}' with ${relevantKeys.length} keys`,
|
|
93
|
-
keysCount: relevantKeys.length,
|
|
94
|
-
results,
|
|
95
|
-
};
|
|
96
|
-
};
|
|
97
|
-
exports.createPrunedNamespaceAutomated = createPrunedNamespaceAutomated;
|
|
98
|
-
//# sourceMappingURL=create-pruned-namespace-automated.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"create-pruned-namespace-automated.js","sourceRoot":"","sources":["../../src/commands/create-pruned-namespace-automated.ts"],"names":[],"mappings":";;;;;;AAAA,0DAA4B;AAC5B,qDAAwC;AACxC,sDAAwB;AAExB,wCAKqB;AAsBd,MAAM,8BAA8B,GAAG,KAAK,EACjD,MAAqB,EACrB,OAAqB,EACG,EAAE;IAC1B,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,QAAQ,EAAE,OAAO,EAAE,gBAAgB,EAAE,GAAG,MAAM,CAAA;IAC5E,MAAM,EAAE,eAAe,EAAE,YAAY,EAAE,YAAY,EAAE,GAAG,OAAO,CAAA;IAE/D,kBAAkB;IAClB,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,eAAe,CAAC,EAAE,CAAC;QAC1C,MAAM,IAAI,KAAK,CACb,qBAAqB,eAAe,8BAA8B,CACnE,CAAA;IACH,CAAC;IAED,IAAI,UAAU,CAAC,QAAQ,CAAC,YAAY,CAAC,EAAE,CAAC;QACtC,MAAM,IAAI,KAAK,CAAC,cAAc,YAAY,kBAAkB,CAAC,CAAA;IAC/D,CAAC;IAED,OAAO,CAAC,GAAG,CACT,8BAA8B,YAAY,WAAW,eAAe,GAAG,CACxE,CAAA;IACD,OAAO,CAAC,GAAG,CAAC,wBAAwB,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;IAE9D,qDAAqD;IACrD,MAAM,MAAM,GAAG,IAAI,wBAAM,CAAC;QACxB,WAAW,EAAE,KAAK;QAClB,YAAY,EAAE,KAAK;KACpB,CAAC,CAAA;IAEF,MAAM,KAAK,GAAG,MAAM,IAAA,mBAAI,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,CAAC,4CAA4C,YAAY,GAAG,CAAC,CAAA;IAExE,OAAO;QACL,OAAO,EAAE,IAAI;QACb,OAAO,EAAE,6BAA6B,YAAY,UAAU,YAAY,CAAC,MAAM,OAAO;QACtF,SAAS,EAAE,YAAY,CAAC,MAAM;QAC9B,OAAO;KACR,CAAA;AACH,CAAC,CAAA;AArIY,QAAA,8BAA8B,kCAqI1C"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"create-pruned-namespace.d.ts","sourceRoot":"","sources":["../../src/commands/create-pruned-namespace.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAQjD,eAAO,MAAM,qBAAqB,GAAU,QAAQ,aAAa,kBA2JhE,CAAA"}
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.createPrunedNamespace = void 0;
|
|
7
|
-
const fast_glob_1 = __importDefault(require("fast-glob"));
|
|
8
|
-
const i18next_scanner_1 = require("i18next-scanner");
|
|
9
|
-
const node_fs_1 = __importDefault(require("node:fs"));
|
|
10
|
-
const prompts_1 = __importDefault(require("prompts"));
|
|
11
|
-
const utils_1 = require("../lib/utils");
|
|
12
|
-
const createPrunedNamespace = async (config) => {
|
|
13
|
-
const { namespaces, loadPath, savePath, locales, defaultNamespace } = config;
|
|
14
|
-
// Step 1: Ask for source namespace
|
|
15
|
-
const sourceNamespaceResponse = await (0, prompts_1.default)({
|
|
16
|
-
type: "select",
|
|
17
|
-
name: "value",
|
|
18
|
-
message: "Select source namespace to create pruned version from:",
|
|
19
|
-
choices: namespaces.map((namespace) => ({
|
|
20
|
-
title: namespace,
|
|
21
|
-
value: namespace,
|
|
22
|
-
})),
|
|
23
|
-
onState: (state) => {
|
|
24
|
-
if (state.aborted) {
|
|
25
|
-
process.nextTick(() => {
|
|
26
|
-
process.exit(0);
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
},
|
|
30
|
-
});
|
|
31
|
-
const sourceNamespace = sourceNamespaceResponse.value;
|
|
32
|
-
// Step 2: Ask for new namespace name
|
|
33
|
-
const newNamespaceResponse = await (0, prompts_1.default)({
|
|
34
|
-
type: "text",
|
|
35
|
-
name: "value",
|
|
36
|
-
message: "Enter the name for the new namespace:",
|
|
37
|
-
validate: (value) => {
|
|
38
|
-
if (!value)
|
|
39
|
-
return "Namespace name cannot be empty";
|
|
40
|
-
if (namespaces.includes(value))
|
|
41
|
-
return "Namespace already exists";
|
|
42
|
-
return true;
|
|
43
|
-
},
|
|
44
|
-
onState: (state) => {
|
|
45
|
-
if (state.aborted) {
|
|
46
|
-
process.nextTick(() => {
|
|
47
|
-
process.exit(0);
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
},
|
|
51
|
-
});
|
|
52
|
-
const newNamespace = newNamespaceResponse.value;
|
|
53
|
-
// Step 3: Ask for glob patterns to find relevant keys
|
|
54
|
-
const allPatterns = config.globPatterns.map((pattern) => typeof pattern === "string" ? pattern : pattern.pattern);
|
|
55
|
-
const globPatternsResponse = await (0, prompts_1.default)({
|
|
56
|
-
type: "list",
|
|
57
|
-
name: "value",
|
|
58
|
-
message: "Enter glob patterns to find relevant keys (comma separated):",
|
|
59
|
-
initial: allPatterns.join(","),
|
|
60
|
-
separator: ",",
|
|
61
|
-
onState: (state) => {
|
|
62
|
-
if (state.aborted) {
|
|
63
|
-
process.nextTick(() => {
|
|
64
|
-
process.exit(0);
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
},
|
|
68
|
-
});
|
|
69
|
-
const selectedGlobPatterns = globPatternsResponse.value;
|
|
70
|
-
console.log(`Finding keys used in files matching: ${selectedGlobPatterns.join(", ")}`);
|
|
71
|
-
// Extract keys from files matching the glob patterns
|
|
72
|
-
const parser = new i18next_scanner_1.Parser({
|
|
73
|
-
nsSeparator: false,
|
|
74
|
-
keySeparator: false,
|
|
75
|
-
});
|
|
76
|
-
const files = await (0, fast_glob_1.default)([...selectedGlobPatterns, "!**/node_modules/**"]);
|
|
77
|
-
console.log(`Found ${files.length} files to scan`);
|
|
78
|
-
const extractedKeys = [];
|
|
79
|
-
for (const file of files) {
|
|
80
|
-
const content = node_fs_1.default.readFileSync(file, "utf-8");
|
|
81
|
-
parser.parseFuncFromString(content, { list: ["t"] }, (key) => {
|
|
82
|
-
extractedKeys.push(key);
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
const uniqueExtractedKeys = (0, utils_1.removeDuplicatesFromArray)(extractedKeys);
|
|
86
|
-
console.log(`Found ${uniqueExtractedKeys.length} unique translation keys`);
|
|
87
|
-
// Filter keys that belong to the source namespace
|
|
88
|
-
const relevantKeys = [];
|
|
89
|
-
for (const key of uniqueExtractedKeys) {
|
|
90
|
-
const pureKey = (0, utils_1.getPureKey)(key, sourceNamespace, sourceNamespace === defaultNamespace);
|
|
91
|
-
if (pureKey) {
|
|
92
|
-
relevantKeys.push(pureKey);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
console.log(`Found ${relevantKeys.length} keys from namespace '${sourceNamespace}'`);
|
|
96
|
-
if (relevantKeys.length === 0) {
|
|
97
|
-
console.log("No relevant keys found. Exiting...");
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
// Get translations from source namespace and create new namespace files
|
|
101
|
-
for (const locale of locales) {
|
|
102
|
-
try {
|
|
103
|
-
// Load source namespace translations
|
|
104
|
-
const sourceTranslations = await (0, utils_1.loadLocalesFile)(loadPath, locale, sourceNamespace);
|
|
105
|
-
// Create new namespace with only the keys used in the glob pattern files
|
|
106
|
-
const newNamespaceTranslations = {};
|
|
107
|
-
for (const key of relevantKeys) {
|
|
108
|
-
if (sourceTranslations[key]) {
|
|
109
|
-
newNamespaceTranslations[key] = sourceTranslations[key];
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
// Write the new namespace file
|
|
113
|
-
await (0, utils_1.writeLocalesFile)(savePath, locale, newNamespace, newNamespaceTranslations);
|
|
114
|
-
console.log(`Created pruned namespace '${newNamespace}' for locale '${locale}' with ${Object.keys(newNamespaceTranslations).length} keys`);
|
|
115
|
-
}
|
|
116
|
-
catch (error) {
|
|
117
|
-
console.error(`Error creating pruned namespace for locale '${locale}':`, error);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
console.log(`ā
Successfully created pruned namespace '${newNamespace}'`);
|
|
121
|
-
};
|
|
122
|
-
exports.createPrunedNamespace = createPrunedNamespace;
|
|
123
|
-
//# sourceMappingURL=create-pruned-namespace.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"create-pruned-namespace.js","sourceRoot":"","sources":["../../src/commands/create-pruned-namespace.ts"],"names":[],"mappings":";;;;;;AAAA,0DAA4B;AAC5B,qDAAwC;AACxC,sDAAwB;AACxB,sDAA6B;AAE7B,wCAKqB;AAEd,MAAM,qBAAqB,GAAG,KAAK,EAAE,MAAqB,EAAE,EAAE;IACnE,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,QAAQ,EAAE,OAAO,EAAE,gBAAgB,EAAE,GAAG,MAAM,CAAA;IAE5E,mCAAmC;IACnC,MAAM,uBAAuB,GAAG,MAAM,IAAA,iBAAO,EAAC;QAC5C,IAAI,EAAE,QAAQ;QACd,IAAI,EAAE,OAAO;QACb,OAAO,EAAE,wDAAwD;QACjE,OAAO,EAAE,UAAU,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;YACtC,KAAK,EAAE,SAAS;YAChB,KAAK,EAAE,SAAS;SACjB,CAAC,CAAC;QACH,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE;YACjB,IAAI,KAAK,CAAC,OAAO,EAAE,CAAC;gBAClB,OAAO,CAAC,QAAQ,CAAC,GAAG,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