Skip to content

Commit d6292d2

Browse files
🐛 fix: Fix saveImmediately
1 parent 1ed45d7 commit d6292d2

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

packages/lobe-i18n/src/commands/TranslateLocale/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ class TranslateLocale {
119119
const targetFilename = resolve(config.output, locale, filename);
120120
const entryObj = entry[filename] as LocaleObj;
121121
const targetObj = diff(entryObj, getLocaleObj(targetFilename)).target;
122-
writeJSON(targetFilename, targetObj);
123122
if (isEqualJsonKeys(entryObj, targetObj)) continue;
123+
124124
this.query.push({
125125
entry: entryObj,
126126
filename: targetFilename,
@@ -150,8 +150,8 @@ class TranslateLocale {
150150
const targetFilename = resolve(config.output, locale) + '.json';
151151
const entryObj = entry;
152152
const targetObj = diff(entryObj, getLocaleObj(targetFilename)).target;
153-
writeJSON(targetFilename, targetObj);
154153
if (isEqualJsonKeys(entryObj, targetObj)) continue;
154+
155155
this.query.push({
156156
entry: entryObj,
157157
filename: targetFilename,

packages/lobe-i18n/src/commands/TranslateMarkdown/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ class TranslateMarkdown {
140140
to: locale,
141141
});
142142
}
143-
} catch {
143+
} catch (error) {
144+
consola.error(`Error processing file ${file}:`, error);
144145
alert.error(`${file} not found`, true);
145146
}
146147
}

packages/lobe-i18n/src/core/I18n.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ export class I18n {
106106
// 用于存储合并的结果
107107
let mergedResult = '';
108108

109+
// 预先初始化翻译结果数组,用于立即保存功能
110+
const tempResults: string[] = Array.from({ length: this.maxStep }, () => '');
111+
109112
const updateProgress = () => {
110113
const completedChunks = chunkProgress.filter((status) => status === 2).length;
111114
const inProgressChunks = chunkProgress.filter((status) => status === 1).length;
@@ -163,13 +166,12 @@ export class I18n {
163166

164167
// 如果开启立即保存,则每个 chunk 完成后立即合并并保存
165168
if (this.config.saveImmediately && filename) {
166-
// 对于字符串类型,我们需要重新组装并保存
167-
const tempTranslatedArray = [...translatedSplitString];
168-
tempTranslatedArray[index] = result;
169+
// 存储当前结果到临时数组
170+
tempResults[index] = result;
169171
// 只有当前面的 chunks 都完成了,才能正确组装
170172
if (index === 0 || chunkProgress.slice(0, index).every((status) => status === 2)) {
171173
mergedResult = await this.translateMarkdownService.genMarkdownByString(
172-
tempTranslatedArray.filter(Boolean),
174+
tempResults.filter(Boolean),
173175
);
174176
writeMarkdown(filename, mergedResult);
175177
onChunkComplete?.(result, mergedResult);
@@ -191,10 +193,11 @@ export class I18n {
191193
step: this.maxStep,
192194
});
193195

194-
// 如果没有开启立即保存,则使用原有逻辑
195-
const result = filename
196-
? mergedResult
197-
: await this.translateMarkdownService.genMarkdownByString(translatedSplitString);
196+
// 如果开启了立即保存且有合并结果,则使用合并结果,否则使用原有逻辑
197+
const result =
198+
this.config.saveImmediately && mergedResult
199+
? mergedResult
200+
: await this.translateMarkdownService.genMarkdownByString(translatedSplitString);
198201

199202
return {
200203
result,

0 commit comments

Comments
 (0)