@intlayer/cli 6.1.3 → 6.1.4

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.
@@ -0,0 +1,304 @@
1
+ import { getIntlayerAPIProxy } from "@intlayer/api";
2
+ import {
3
+ formatLocale,
4
+ formatPath,
5
+ mergeDictionaries,
6
+ parallelize,
7
+ prepareIntlayer,
8
+ processPerLocaleDictionary,
9
+ reduceDictionaryContent,
10
+ writeContentDeclaration
11
+ } from "@intlayer/chokidar";
12
+ import {
13
+ ANSIColors,
14
+ colon,
15
+ colorize,
16
+ colorizeKey,
17
+ colorizePath,
18
+ getAppLogger,
19
+ getConfiguration
20
+ } from "@intlayer/config";
21
+ import {
22
+ getFilterTranslationsOnlyContent,
23
+ getLocalisedContent,
24
+ getMissingLocalesContent
25
+ } from "@intlayer/core";
26
+ import { getDictionaries } from "@intlayer/dictionaries-entry";
27
+ import { relative } from "path";
28
+ import {
29
+ ensureArray,
30
+ getTargetUnmergedDictionaries
31
+ } from "../getTargetDictionary.mjs";
32
+ import { checkAIAccess } from "../utils/checkAccess.mjs";
33
+ import { autoFill } from "./autoFill.mjs";
34
+ const NB_CONCURRENT_TRANSLATIONS = 8;
35
+ const fill = async (options) => {
36
+ const configuration = getConfiguration(options.configOptions);
37
+ const appLogger = getAppLogger(configuration, {
38
+ config: {
39
+ prefix: ""
40
+ }
41
+ });
42
+ if (options.build) {
43
+ await prepareIntlayer(configuration);
44
+ }
45
+ const { defaultLocale, locales } = configuration.internationalization;
46
+ const mode = options.mode ?? "complete";
47
+ const baseLocale = options.sourceLocale ?? defaultLocale;
48
+ const outputLocales = (options.outputLocales ? ensureArray(options.outputLocales) : locales).filter((locale) => locale !== baseLocale);
49
+ const hasAIAccess = await checkAIAccess(configuration, options.aiOptions);
50
+ if (!hasAIAccess) return;
51
+ const intlayerAPI = getIntlayerAPIProxy(void 0, configuration);
52
+ const targetUnmergedDictionaries = await getTargetUnmergedDictionaries(options);
53
+ const affectedDictionaryKeys = /* @__PURE__ */ new Set();
54
+ targetUnmergedDictionaries.forEach((dict) => {
55
+ affectedDictionaryKeys.add(dict.key);
56
+ });
57
+ appLogger([
58
+ "Affected dictionary keys for processing:",
59
+ Array.from(affectedDictionaryKeys).map((key) => colorizeKey(key)).join(", ")
60
+ ]);
61
+ const maxKeyLength = Math.max(
62
+ ...targetUnmergedDictionaries.map((dict) => dict.key.length)
63
+ );
64
+ const maxLocaleLength = Math.max(
65
+ ...locales.map((locale) => formatLocale(locale).length)
66
+ );
67
+ const dictionariesRecord = getDictionaries(configuration);
68
+ const translationTasks = [];
69
+ for (const targetUnmergedDictionary of targetUnmergedDictionaries) {
70
+ const dictionaryPreset = colon(
71
+ [
72
+ colorize(" - [", ANSIColors.GREY_DARK),
73
+ colorizeKey(targetUnmergedDictionary.key),
74
+ colorize("]", ANSIColors.GREY_DARK)
75
+ ].join(""),
76
+ { colSize: maxKeyLength + 6 }
77
+ );
78
+ const dictionaryKey = targetUnmergedDictionary.key;
79
+ const mainDictionaryToProcess = dictionariesRecord[dictionaryKey];
80
+ const sourceLocale = targetUnmergedDictionary.locale ?? baseLocale;
81
+ if (!mainDictionaryToProcess) {
82
+ appLogger(
83
+ `${dictionaryPreset} Dictionary not found in dictionariesRecord. Skipping.`,
84
+ {
85
+ level: "warn"
86
+ }
87
+ );
88
+ continue;
89
+ }
90
+ if (!targetUnmergedDictionary.filePath) {
91
+ appLogger(`${dictionaryPreset} Dictionary has no file path. Skipping.`, {
92
+ level: "warn"
93
+ });
94
+ continue;
95
+ }
96
+ const relativePath = relative(
97
+ configuration.content.baseDir,
98
+ targetUnmergedDictionary.filePath
99
+ );
100
+ appLogger(
101
+ `${dictionaryPreset} Processing content declaration: ${colorizePath(relativePath)}`,
102
+ {
103
+ level: "info"
104
+ }
105
+ );
106
+ const sourceLocaleContent = getFilterTranslationsOnlyContent(
107
+ mainDictionaryToProcess,
108
+ sourceLocale,
109
+ { dictionaryKey, keyPath: [] }
110
+ );
111
+ if (Object.keys(sourceLocaleContent).length === 0) {
112
+ appLogger(
113
+ `${dictionaryPreset} No content found for dictionary in source locale ${formatLocale(sourceLocale)}. Skipping translation for this dictionary.`,
114
+ {
115
+ level: "warn"
116
+ }
117
+ );
118
+ continue;
119
+ }
120
+ let outputLocalesList = outputLocales;
121
+ if (mode === "complete") {
122
+ const missingLocales = getMissingLocalesContent(
123
+ mainDictionaryToProcess,
124
+ outputLocales,
125
+ {
126
+ dictionaryKey: mainDictionaryToProcess.key,
127
+ keyPath: [],
128
+ plugins: []
129
+ }
130
+ );
131
+ outputLocalesList = missingLocales;
132
+ }
133
+ if (outputLocalesList.length === 0) {
134
+ appLogger(
135
+ `${dictionaryPreset} No locales to fill - Skipping dictionary`,
136
+ {
137
+ level: "warn"
138
+ }
139
+ );
140
+ continue;
141
+ }
142
+ for (const targetLocale of outputLocalesList) {
143
+ const localePreset = colon(
144
+ [
145
+ colorize("[", ANSIColors.GREY_DARK),
146
+ formatLocale(targetLocale),
147
+ colorize("]", ANSIColors.GREY_DARK)
148
+ ].join(""),
149
+ { colSize: maxLocaleLength }
150
+ );
151
+ translationTasks.push({
152
+ dictionaryKey,
153
+ sourceLocale,
154
+ targetLocale,
155
+ dictionaryPreset,
156
+ localePreset
157
+ });
158
+ }
159
+ }
160
+ const translationResults = await parallelize(
161
+ translationTasks,
162
+ async (task) => {
163
+ const mainDictionaryToProcess = dictionariesRecord[task.dictionaryKey];
164
+ appLogger(
165
+ `${task.dictionaryPreset}${task.localePreset} Preparing translation for dictionary from ${formatLocale(task.sourceLocale)} to ${formatLocale(task.targetLocale)}`,
166
+ {
167
+ level: "info"
168
+ }
169
+ );
170
+ const sourceLocaleContent = getFilterTranslationsOnlyContent(
171
+ mainDictionaryToProcess,
172
+ task.sourceLocale,
173
+ { dictionaryKey: task.dictionaryKey, keyPath: [] }
174
+ );
175
+ const presetOutputContent = getLocalisedContent(
176
+ mainDictionaryToProcess,
177
+ task.targetLocale,
178
+ { dictionaryKey: task.dictionaryKey, keyPath: [] }
179
+ );
180
+ try {
181
+ const translationResult = await intlayerAPI.ai.translateJSON({
182
+ entryFileContent: sourceLocaleContent.content,
183
+ presetOutputContent: presetOutputContent.content,
184
+ dictionaryDescription: mainDictionaryToProcess.description ?? "",
185
+ entryLocale: task.sourceLocale,
186
+ outputLocale: task.targetLocale,
187
+ mode,
188
+ aiOptions: options.aiOptions
189
+ });
190
+ if (!translationResult.data?.fileContent) {
191
+ appLogger(
192
+ `${task.dictionaryPreset}${task.localePreset} No content result`,
193
+ {
194
+ level: "error"
195
+ }
196
+ );
197
+ return { key: task.dictionaryKey, result: null };
198
+ }
199
+ const processedPerLocaleDictionary = processPerLocaleDictionary({
200
+ ...mainDictionaryToProcess,
201
+ content: translationResult.data?.fileContent,
202
+ locale: task.targetLocale
203
+ });
204
+ return {
205
+ key: task.dictionaryKey,
206
+ result: processedPerLocaleDictionary
207
+ };
208
+ } catch (error) {
209
+ appLogger(
210
+ `${task.dictionaryPreset}${task.localePreset} ${colorize("Error filling", ANSIColors.RED)}: ` + error,
211
+ {
212
+ level: "error"
213
+ }
214
+ );
215
+ return { key: task.dictionaryKey, result: null };
216
+ }
217
+ },
218
+ options.nbConcurrentTranslations ?? NB_CONCURRENT_TRANSLATIONS
219
+ );
220
+ const resultsByDictionary = /* @__PURE__ */ new Map();
221
+ for (const item of translationResults) {
222
+ if (item?.result) {
223
+ const list = resultsByDictionary.get(item.key) ?? [];
224
+ list.push(item.result);
225
+ resultsByDictionary.set(item.key, list);
226
+ }
227
+ }
228
+ for (const targetUnmergedDictionary of targetUnmergedDictionaries) {
229
+ const dictionaryKey = targetUnmergedDictionary.key;
230
+ const mainDictionaryToProcess = dictionariesRecord[dictionaryKey];
231
+ const sourceLocale = targetUnmergedDictionary.locale ?? baseLocale;
232
+ if (!mainDictionaryToProcess || !targetUnmergedDictionary.filePath) {
233
+ continue;
234
+ }
235
+ let outputLocalesList = outputLocales;
236
+ if (mode === "complete") {
237
+ const missingLocales = getMissingLocalesContent(
238
+ mainDictionaryToProcess,
239
+ outputLocales,
240
+ {
241
+ dictionaryKey: mainDictionaryToProcess.key,
242
+ keyPath: [],
243
+ plugins: []
244
+ }
245
+ );
246
+ outputLocalesList = missingLocales;
247
+ }
248
+ if (outputLocalesList.length === 0) {
249
+ continue;
250
+ }
251
+ const perLocaleResults = resultsByDictionary.get(dictionaryKey) ?? [];
252
+ const dictionaryToMerge = mode === "review" ? [...perLocaleResults, mainDictionaryToProcess] : [mainDictionaryToProcess, ...perLocaleResults];
253
+ const mergedResults = mergeDictionaries(dictionaryToMerge);
254
+ let formattedDict = targetUnmergedDictionary;
255
+ if (formattedDict.locale) {
256
+ const presetOutputContent = getLocalisedContent(
257
+ mainDictionaryToProcess,
258
+ formattedDict.locale,
259
+ { dictionaryKey, keyPath: [] }
260
+ );
261
+ formattedDict = {
262
+ ...formattedDict,
263
+ content: presetOutputContent.content
264
+ };
265
+ }
266
+ const reducedResult = reduceDictionaryContent(mergedResults, formattedDict);
267
+ if (formattedDict.autoFill || configuration.content.autoFill) {
268
+ await autoFill(
269
+ mergedResults,
270
+ targetUnmergedDictionary,
271
+ formattedDict.autoFill ?? configuration.content.autoFill,
272
+ outputLocalesList,
273
+ [sourceLocale],
274
+ configuration
275
+ );
276
+ } else {
277
+ await writeContentDeclaration(
278
+ { ...formattedDict, content: reducedResult.content },
279
+ configuration,
280
+ formattedDict.filePath
281
+ );
282
+ if (formattedDict.filePath) {
283
+ const dictionaryPreset = colon(
284
+ [
285
+ colorize(" - [", ANSIColors.GREY_DARK),
286
+ colorizeKey(targetUnmergedDictionary.key),
287
+ colorize("]", ANSIColors.GREY_DARK)
288
+ ].join(""),
289
+ { colSize: maxKeyLength + 6 }
290
+ );
291
+ appLogger(
292
+ `${dictionaryPreset} Content declaration written to ${formatPath(formattedDict.filePath)}`,
293
+ {
294
+ level: "info"
295
+ }
296
+ );
297
+ }
298
+ }
299
+ }
300
+ };
301
+ export {
302
+ fill
303
+ };
304
+ //# sourceMappingURL=fill.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/fill/fill.ts"],"sourcesContent":["import { AIOptions, getIntlayerAPIProxy } from '@intlayer/api'; // Importing only getAiAPI for now\nimport {\n formatLocale,\n formatPath,\n ListGitFilesOptions,\n mergeDictionaries,\n parallelize,\n prepareIntlayer,\n processPerLocaleDictionary,\n reduceDictionaryContent,\n writeContentDeclaration,\n} from '@intlayer/chokidar';\nimport {\n ANSIColors,\n colon,\n colorize,\n colorizeKey,\n colorizePath,\n getAppLogger,\n getConfiguration,\n Locales,\n} from '@intlayer/config';\nimport {\n type ContentNode,\n type Dictionary,\n getFilterTranslationsOnlyContent,\n getLocalisedContent,\n getMissingLocalesContent,\n} from '@intlayer/core';\nimport { getDictionaries } from '@intlayer/dictionaries-entry';\nimport { relative } from 'path';\nimport {\n ensureArray,\n GetTargetDictionaryOptions,\n getTargetUnmergedDictionaries,\n} from '../getTargetDictionary';\nimport { checkAIAccess } from '../utils/checkAccess';\nimport { autoFill } from './autoFill';\n\nconst NB_CONCURRENT_TRANSLATIONS = 8;\n\n// Arguments for the fill function\nexport type FillOptions = {\n sourceLocale?: Locales;\n outputLocales?: Locales | Locales[];\n mode?: 'complete' | 'review';\n gitOptions?: ListGitFilesOptions;\n aiOptions?: AIOptions; // Added aiOptions to be passed to translateJSON\n verbose?: boolean;\n nbConcurrentTranslations?: number;\n build?: boolean;\n} & GetTargetDictionaryOptions;\n\n/**\n * Fill translations based on the provided options.\n */\nexport const fill = async (options: FillOptions): Promise<void> => {\n const configuration = getConfiguration(options.configOptions);\n const appLogger = getAppLogger(configuration, {\n config: {\n prefix: '',\n },\n });\n\n if (options.build) {\n await prepareIntlayer(configuration);\n }\n\n const { defaultLocale, locales } = configuration.internationalization;\n const mode = options.mode ?? 'complete';\n const baseLocale = options.sourceLocale ?? defaultLocale;\n const outputLocales = (\n options.outputLocales ? ensureArray(options.outputLocales) : locales\n ).filter((locale) => locale !== baseLocale);\n\n const hasAIAccess = await checkAIAccess(configuration, options.aiOptions);\n\n if (!hasAIAccess) return;\n\n const intlayerAPI = getIntlayerAPIProxy(undefined, configuration);\n\n const targetUnmergedDictionaries =\n await getTargetUnmergedDictionaries(options);\n\n const affectedDictionaryKeys = new Set<string>();\n targetUnmergedDictionaries.forEach((dict) => {\n affectedDictionaryKeys.add(dict.key);\n });\n\n appLogger([\n 'Affected dictionary keys for processing:',\n Array.from(affectedDictionaryKeys)\n .map((key) => colorizeKey(key))\n .join(', '),\n ]);\n\n const maxKeyLength = Math.max(\n ...targetUnmergedDictionaries.map((dict) => dict.key.length)\n );\n const maxLocaleLength = Math.max(\n ...locales.map((locale) => formatLocale(locale).length)\n );\n const dictionariesRecord = getDictionaries(configuration);\n\n type TranslationTask = {\n dictionaryKey: string;\n sourceLocale: Locales;\n targetLocale: Locales;\n dictionaryPreset: string;\n localePreset: string;\n };\n\n const translationTasks: TranslationTask[] = [];\n\n for (const targetUnmergedDictionary of targetUnmergedDictionaries) {\n const dictionaryPreset = colon(\n [\n colorize(' - [', ANSIColors.GREY_DARK),\n colorizeKey(targetUnmergedDictionary.key),\n colorize(']', ANSIColors.GREY_DARK),\n ].join(''),\n { colSize: maxKeyLength + 6 }\n );\n\n const dictionaryKey = targetUnmergedDictionary.key;\n const mainDictionaryToProcess: Dictionary =\n dictionariesRecord[dictionaryKey];\n\n const sourceLocale: Locales =\n (targetUnmergedDictionary.locale as Locales) ?? baseLocale;\n\n if (!mainDictionaryToProcess) {\n appLogger(\n `${dictionaryPreset} Dictionary not found in dictionariesRecord. Skipping.`,\n {\n level: 'warn',\n }\n );\n continue;\n }\n\n if (!targetUnmergedDictionary.filePath) {\n appLogger(`${dictionaryPreset} Dictionary has no file path. Skipping.`, {\n level: 'warn',\n });\n continue;\n }\n\n const relativePath = relative(\n configuration.content.baseDir,\n targetUnmergedDictionary.filePath\n );\n\n appLogger(\n `${dictionaryPreset} Processing content declaration: ${colorizePath(relativePath)}`,\n {\n level: 'info',\n }\n );\n\n const sourceLocaleContent = getFilterTranslationsOnlyContent(\n mainDictionaryToProcess as unknown as ContentNode,\n sourceLocale,\n { dictionaryKey, keyPath: [] }\n );\n\n if (Object.keys(sourceLocaleContent).length === 0) {\n appLogger(\n `${dictionaryPreset} No content found for dictionary in source locale ${formatLocale(sourceLocale)}. Skipping translation for this dictionary.`,\n {\n level: 'warn',\n }\n );\n continue;\n }\n\n let outputLocalesList: Locales[] = outputLocales;\n\n if (mode === 'complete') {\n const missingLocales = getMissingLocalesContent(\n mainDictionaryToProcess as unknown as ContentNode,\n outputLocales,\n {\n dictionaryKey: mainDictionaryToProcess.key,\n keyPath: [],\n plugins: [],\n }\n );\n\n outputLocalesList = missingLocales;\n }\n\n if (outputLocalesList.length === 0) {\n appLogger(\n `${dictionaryPreset} No locales to fill - Skipping dictionary`,\n {\n level: 'warn',\n }\n );\n continue;\n }\n\n for (const targetLocale of outputLocalesList) {\n const localePreset = colon(\n [\n colorize('[', ANSIColors.GREY_DARK),\n formatLocale(targetLocale),\n colorize(']', ANSIColors.GREY_DARK),\n ].join(''),\n { colSize: maxLocaleLength }\n );\n\n translationTasks.push({\n dictionaryKey,\n sourceLocale,\n targetLocale,\n dictionaryPreset,\n localePreset,\n });\n }\n }\n\n const translationResults = await parallelize(\n translationTasks,\n async (task) => {\n const mainDictionaryToProcess: Dictionary =\n dictionariesRecord[task.dictionaryKey];\n\n appLogger(\n `${task.dictionaryPreset}${task.localePreset} Preparing translation for dictionary from ${formatLocale(task.sourceLocale)} to ${formatLocale(task.targetLocale)}`,\n {\n level: 'info',\n }\n );\n\n const sourceLocaleContent = getFilterTranslationsOnlyContent(\n mainDictionaryToProcess as unknown as ContentNode,\n task.sourceLocale,\n { dictionaryKey: task.dictionaryKey, keyPath: [] }\n );\n\n const presetOutputContent = getLocalisedContent(\n mainDictionaryToProcess as unknown as ContentNode,\n task.targetLocale,\n { dictionaryKey: task.dictionaryKey, keyPath: [] }\n );\n\n try {\n const translationResult = await intlayerAPI.ai.translateJSON({\n entryFileContent: sourceLocaleContent.content,\n presetOutputContent: presetOutputContent.content,\n dictionaryDescription: mainDictionaryToProcess.description ?? '',\n entryLocale: task.sourceLocale,\n outputLocale: task.targetLocale,\n mode,\n aiOptions: options.aiOptions,\n });\n\n if (!translationResult.data?.fileContent) {\n appLogger(\n `${task.dictionaryPreset}${task.localePreset} No content result`,\n {\n level: 'error',\n }\n );\n return { key: task.dictionaryKey, result: null } as const;\n }\n\n const processedPerLocaleDictionary = processPerLocaleDictionary({\n ...mainDictionaryToProcess,\n content: translationResult.data?.fileContent,\n locale: task.targetLocale,\n });\n\n return {\n key: task.dictionaryKey,\n result: processedPerLocaleDictionary,\n } as const;\n } catch (error) {\n appLogger(\n `${task.dictionaryPreset}${task.localePreset} ${colorize('Error filling', ANSIColors.RED)}: ` +\n error,\n {\n level: 'error',\n }\n );\n return { key: task.dictionaryKey, result: null } as const;\n }\n },\n options.nbConcurrentTranslations ?? NB_CONCURRENT_TRANSLATIONS\n );\n\n const resultsByDictionary = new Map<string, Dictionary[]>();\n for (const item of translationResults) {\n if (item?.result) {\n const list = resultsByDictionary.get(item.key) ?? [];\n list.push(item.result);\n resultsByDictionary.set(item.key, list);\n }\n }\n\n for (const targetUnmergedDictionary of targetUnmergedDictionaries) {\n const dictionaryKey = targetUnmergedDictionary.key;\n const mainDictionaryToProcess: Dictionary =\n dictionariesRecord[dictionaryKey];\n\n const sourceLocale: Locales =\n (targetUnmergedDictionary.locale as Locales) ?? baseLocale;\n\n if (!mainDictionaryToProcess || !targetUnmergedDictionary.filePath) {\n continue;\n }\n\n let outputLocalesList: Locales[] = outputLocales;\n\n if (mode === 'complete') {\n const missingLocales = getMissingLocalesContent(\n mainDictionaryToProcess as unknown as ContentNode,\n outputLocales,\n {\n dictionaryKey: mainDictionaryToProcess.key,\n keyPath: [],\n plugins: [],\n }\n );\n\n outputLocalesList = missingLocales;\n }\n\n if (outputLocalesList.length === 0) {\n continue;\n }\n\n const perLocaleResults = resultsByDictionary.get(dictionaryKey) ?? [];\n\n const dictionaryToMerge =\n mode === 'review'\n ? [...perLocaleResults, mainDictionaryToProcess]\n : [mainDictionaryToProcess, ...perLocaleResults];\n\n const mergedResults = mergeDictionaries(dictionaryToMerge);\n\n let formattedDict = targetUnmergedDictionary;\n\n if (formattedDict.locale) {\n const presetOutputContent = getLocalisedContent(\n mainDictionaryToProcess as unknown as ContentNode,\n formattedDict.locale,\n { dictionaryKey, keyPath: [] }\n );\n\n formattedDict = {\n ...formattedDict,\n content: presetOutputContent.content,\n };\n }\n\n const reducedResult = reduceDictionaryContent(mergedResults, formattedDict);\n\n if (formattedDict.autoFill || configuration.content.autoFill) {\n await autoFill(\n mergedResults,\n targetUnmergedDictionary,\n formattedDict.autoFill ?? configuration.content.autoFill,\n outputLocalesList,\n [sourceLocale],\n configuration\n );\n } else {\n await writeContentDeclaration(\n { ...formattedDict, content: reducedResult.content },\n configuration,\n formattedDict.filePath\n );\n\n if (formattedDict.filePath) {\n const dictionaryPreset = colon(\n [\n colorize(' - [', ANSIColors.GREY_DARK),\n colorizeKey(targetUnmergedDictionary.key),\n colorize(']', ANSIColors.GREY_DARK),\n ].join(''),\n { colSize: maxKeyLength + 6 }\n );\n appLogger(\n `${dictionaryPreset} Content declaration written to ${formatPath(formattedDict.filePath)}`,\n {\n level: 'info',\n }\n );\n }\n }\n }\n};\n"],"mappings":"AAAA,SAAoB,2BAA2B;AAC/C;AAAA,EACE;AAAA,EACA;AAAA,EAEA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AACP;AAAA,EAGE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,uBAAuB;AAChC,SAAS,gBAAgB;AACzB;AAAA,EACE;AAAA,EAEA;AAAA,OACK;AACP,SAAS,qBAAqB;AAC9B,SAAS,gBAA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@ -1,304 +1,3 @@
1
- import { getIntlayerAPIProxy } from "@intlayer/api";
2
- import {
3
- formatLocale,
4
- formatPath,
5
- mergeDictionaries,
6
- parallelize,
7
- prepareIntlayer,
8
- processPerLocaleDictionary,
9
- reduceDictionaryContent,
10
- writeContentDeclaration
11
- } from "@intlayer/chokidar";
12
- import {
13
- ANSIColors,
14
- colon,
15
- colorize,
16
- colorizeKey,
17
- colorizePath,
18
- getAppLogger,
19
- getConfiguration
20
- } from "@intlayer/config";
21
- import {
22
- getFilterTranslationsOnlyContent,
23
- getLocalisedContent,
24
- getMissingLocalesContent
25
- } from "@intlayer/core";
26
- import { getDictionaries } from "@intlayer/dictionaries-entry";
27
- import { relative } from "path";
28
- import {
29
- ensureArray,
30
- getTargetUnmergedDictionaries
31
- } from "../getTargetDictionary.mjs";
32
- import { checkAIAccess } from "../utils/checkAccess.mjs";
33
- import { autoFill } from "./autoFill.mjs";
34
- const NB_CONCURRENT_TRANSLATIONS = 8;
35
- const fill = async (options) => {
36
- const configuration = getConfiguration(options.configOptions);
37
- const appLogger = getAppLogger(configuration, {
38
- config: {
39
- prefix: ""
40
- }
41
- });
42
- if (options.build) {
43
- await prepareIntlayer(configuration);
44
- }
45
- const { defaultLocale, locales } = configuration.internationalization;
46
- const mode = options.mode ?? "complete";
47
- const baseLocale = options.sourceLocale ?? defaultLocale;
48
- const outputLocales = (options.outputLocales ? ensureArray(options.outputLocales) : locales).filter((locale) => locale !== baseLocale);
49
- const hasAIAccess = await checkAIAccess(configuration, options.aiOptions);
50
- if (!hasAIAccess) return;
51
- const intlayerAPI = getIntlayerAPIProxy(void 0, configuration);
52
- const targetUnmergedDictionaries = await getTargetUnmergedDictionaries(options);
53
- const affectedDictionaryKeys = /* @__PURE__ */ new Set();
54
- targetUnmergedDictionaries.forEach((dict) => {
55
- affectedDictionaryKeys.add(dict.key);
56
- });
57
- appLogger([
58
- "Affected dictionary keys for processing:",
59
- Array.from(affectedDictionaryKeys).map((key) => colorizeKey(key)).join(", ")
60
- ]);
61
- const maxKeyLength = Math.max(
62
- ...targetUnmergedDictionaries.map((dict) => dict.key.length)
63
- );
64
- const maxLocaleLength = Math.max(
65
- ...locales.map((locale) => formatLocale(locale).length)
66
- );
67
- const dictionariesRecord = getDictionaries(configuration);
68
- const translationTasks = [];
69
- for (const targetUnmergedDictionary of targetUnmergedDictionaries) {
70
- const dictionaryPreset = colon(
71
- [
72
- colorize(" - [", ANSIColors.GREY_DARK),
73
- colorizeKey(targetUnmergedDictionary.key),
74
- colorize("]", ANSIColors.GREY_DARK)
75
- ].join(""),
76
- { colSize: maxKeyLength + 6 }
77
- );
78
- const dictionaryKey = targetUnmergedDictionary.key;
79
- const mainDictionaryToProcess = dictionariesRecord[dictionaryKey];
80
- const sourceLocale = targetUnmergedDictionary.locale ?? baseLocale;
81
- if (!mainDictionaryToProcess) {
82
- appLogger(
83
- `${dictionaryPreset} Dictionary not found in dictionariesRecord. Skipping.`,
84
- {
85
- level: "warn"
86
- }
87
- );
88
- continue;
89
- }
90
- if (!targetUnmergedDictionary.filePath) {
91
- appLogger(`${dictionaryPreset} Dictionary has no file path. Skipping.`, {
92
- level: "warn"
93
- });
94
- continue;
95
- }
96
- const relativePath = relative(
97
- configuration.content.baseDir,
98
- targetUnmergedDictionary.filePath
99
- );
100
- appLogger(
101
- `${dictionaryPreset} Processing content declaration: ${colorizePath(relativePath)}`,
102
- {
103
- level: "info"
104
- }
105
- );
106
- const sourceLocaleContent = getFilterTranslationsOnlyContent(
107
- mainDictionaryToProcess,
108
- sourceLocale,
109
- { dictionaryKey, keyPath: [] }
110
- );
111
- if (Object.keys(sourceLocaleContent).length === 0) {
112
- appLogger(
113
- `${dictionaryPreset} No content found for dictionary in source locale ${formatLocale(sourceLocale)}. Skipping translation for this dictionary.`,
114
- {
115
- level: "warn"
116
- }
117
- );
118
- continue;
119
- }
120
- let outputLocalesList = outputLocales;
121
- if (mode === "complete") {
122
- const missingLocales = getMissingLocalesContent(
123
- mainDictionaryToProcess,
124
- outputLocales,
125
- {
126
- dictionaryKey: mainDictionaryToProcess.key,
127
- keyPath: [],
128
- plugins: []
129
- }
130
- );
131
- outputLocalesList = missingLocales;
132
- }
133
- if (outputLocalesList.length === 0) {
134
- appLogger(
135
- `${dictionaryPreset} No locales to fill - Skipping dictionary`,
136
- {
137
- level: "warn"
138
- }
139
- );
140
- continue;
141
- }
142
- for (const targetLocale of outputLocalesList) {
143
- const localePreset = colon(
144
- [
145
- colorize("[", ANSIColors.GREY_DARK),
146
- formatLocale(targetLocale),
147
- colorize("]", ANSIColors.GREY_DARK)
148
- ].join(""),
149
- { colSize: maxLocaleLength }
150
- );
151
- translationTasks.push({
152
- dictionaryKey,
153
- sourceLocale,
154
- targetLocale,
155
- dictionaryPreset,
156
- localePreset
157
- });
158
- }
159
- }
160
- const translationResults = await parallelize(
161
- translationTasks,
162
- async (task) => {
163
- const mainDictionaryToProcess = dictionariesRecord[task.dictionaryKey];
164
- appLogger(
165
- `${task.dictionaryPreset}${task.localePreset} Preparing translation for dictionary from ${formatLocale(task.sourceLocale)} to ${formatLocale(task.targetLocale)}`,
166
- {
167
- level: "info"
168
- }
169
- );
170
- const sourceLocaleContent = getFilterTranslationsOnlyContent(
171
- mainDictionaryToProcess,
172
- task.sourceLocale,
173
- { dictionaryKey: task.dictionaryKey, keyPath: [] }
174
- );
175
- const presetOutputContent = getLocalisedContent(
176
- mainDictionaryToProcess,
177
- task.targetLocale,
178
- { dictionaryKey: task.dictionaryKey, keyPath: [] }
179
- );
180
- try {
181
- const translationResult = await intlayerAPI.ai.translateJSON({
182
- entryFileContent: sourceLocaleContent.content,
183
- presetOutputContent: presetOutputContent.content,
184
- dictionaryDescription: mainDictionaryToProcess.description ?? "",
185
- entryLocale: task.sourceLocale,
186
- outputLocale: task.targetLocale,
187
- mode,
188
- aiOptions: options.aiOptions
189
- });
190
- if (!translationResult.data?.fileContent) {
191
- appLogger(
192
- `${task.dictionaryPreset}${task.localePreset} No content result`,
193
- {
194
- level: "error"
195
- }
196
- );
197
- return { key: task.dictionaryKey, result: null };
198
- }
199
- const processedPerLocaleDictionary = processPerLocaleDictionary({
200
- ...mainDictionaryToProcess,
201
- content: translationResult.data?.fileContent,
202
- locale: task.targetLocale
203
- });
204
- return {
205
- key: task.dictionaryKey,
206
- result: processedPerLocaleDictionary
207
- };
208
- } catch (error) {
209
- appLogger(
210
- `${task.dictionaryPreset}${task.localePreset} ${colorize("Error filling", ANSIColors.RED)}: ` + error,
211
- {
212
- level: "error"
213
- }
214
- );
215
- return { key: task.dictionaryKey, result: null };
216
- }
217
- },
218
- options.nbConcurrentTranslations ?? NB_CONCURRENT_TRANSLATIONS
219
- );
220
- const resultsByDictionary = /* @__PURE__ */ new Map();
221
- for (const item of translationResults) {
222
- if (item?.result) {
223
- const list = resultsByDictionary.get(item.key) ?? [];
224
- list.push(item.result);
225
- resultsByDictionary.set(item.key, list);
226
- }
227
- }
228
- for (const targetUnmergedDictionary of targetUnmergedDictionaries) {
229
- const dictionaryKey = targetUnmergedDictionary.key;
230
- const mainDictionaryToProcess = dictionariesRecord[dictionaryKey];
231
- const sourceLocale = targetUnmergedDictionary.locale ?? baseLocale;
232
- if (!mainDictionaryToProcess || !targetUnmergedDictionary.filePath) {
233
- continue;
234
- }
235
- let outputLocalesList = outputLocales;
236
- if (mode === "complete") {
237
- const missingLocales = getMissingLocalesContent(
238
- mainDictionaryToProcess,
239
- outputLocales,
240
- {
241
- dictionaryKey: mainDictionaryToProcess.key,
242
- keyPath: [],
243
- plugins: []
244
- }
245
- );
246
- outputLocalesList = missingLocales;
247
- }
248
- if (outputLocalesList.length === 0) {
249
- continue;
250
- }
251
- const perLocaleResults = resultsByDictionary.get(dictionaryKey) ?? [];
252
- const dictionaryToMerge = mode === "review" ? [...perLocaleResults, mainDictionaryToProcess] : [mainDictionaryToProcess, ...perLocaleResults];
253
- const mergedResults = mergeDictionaries(dictionaryToMerge);
254
- let formattedDict = targetUnmergedDictionary;
255
- if (formattedDict.locale) {
256
- const presetOutputContent = getLocalisedContent(
257
- mainDictionaryToProcess,
258
- formattedDict.locale,
259
- { dictionaryKey, keyPath: [] }
260
- );
261
- formattedDict = {
262
- ...formattedDict,
263
- content: presetOutputContent.content
264
- };
265
- }
266
- const reducedResult = reduceDictionaryContent(mergedResults, formattedDict);
267
- if (formattedDict.autoFill || configuration.content.autoFill) {
268
- await autoFill(
269
- mergedResults,
270
- targetUnmergedDictionary,
271
- formattedDict.autoFill ?? configuration.content.autoFill,
272
- outputLocalesList,
273
- [sourceLocale],
274
- configuration
275
- );
276
- } else {
277
- await writeContentDeclaration(
278
- { ...formattedDict, content: reducedResult.content },
279
- configuration,
280
- formattedDict.filePath
281
- );
282
- if (formattedDict.filePath) {
283
- const dictionaryPreset = colon(
284
- [
285
- colorize(" - [", ANSIColors.GREY_DARK),
286
- colorizeKey(targetUnmergedDictionary.key),
287
- colorize("]", ANSIColors.GREY_DARK)
288
- ].join(""),
289
- { colSize: maxKeyLength + 6 }
290
- );
291
- appLogger(
292
- `${dictionaryPreset} Content declaration written to ${formatPath(formattedDict.filePath)}`,
293
- {
294
- level: "info"
295
- }
296
- );
297
- }
298
- }
299
- }
300
- };
301
- export {
302
- fill
303
- };
1
+ export * from "./autoFill.mjs";
2
+ export * from "./fill.mjs";
304
3
  //# sourceMappingURL=index.mjs.map