Skip to content

Commit 6247c61

Browse files
committed
feat: Translate save process --story=1
1 parent cfd99fc commit 6247c61

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

cmd/codei18n/init.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func runInit(cmd *cobra.Command) {
5454
// No, cobra flags have default values.
5555
// We should only override if user EXPLICITLY provided the flag.
5656
// How to check if flag was changed? cmd.Flags().Changed("source-lang")
57-
57+
5858
if cmd.Flags().Changed("source-lang") {
5959
cfg.SourceLanguage = initSourceLang
6060
}
@@ -73,7 +73,7 @@ func runInit(cmd *cobra.Command) {
7373
if _, err := os.Stat(".codei18n/config.json"); err == nil && !initForce {
7474
log.Fatal("配置文件已存在 (使用 --force 覆盖)")
7575
}
76-
76+
7777
if err := config.SaveConfig(projectCfg); err != nil {
7878
log.Fatal("保存配置失败: %v", err)
7979
}
@@ -106,7 +106,7 @@ func runInit(cmd *cobra.Command) {
106106
} else {
107107
log.Info("提示: 运行 'codei18n translate' 可生成翻译")
108108
}
109-
109+
110110
log.Success("项目初始化完成!")
111111
}
112112

core/workflow/translate.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ func Translate(cfg *config.Config, opts TranslateOptions) (*TranslateResult, err
175175
}
176176

177177
countMu.Lock()
178-
defer countMu.Unlock()
179178

180179
if err != nil {
181180
failCount += len(currentBatch)
@@ -186,8 +185,14 @@ func Translate(cfg *config.Config, opts TranslateOptions) (*TranslateResult, err
186185
store.Set(t.id, t.toLang, res)
187186
successCount++
188187
}
188+
// Save progress immediately
189+
if err := store.Save(); err != nil {
190+
log.Warn("保存进度失败: %v", err)
191+
}
189192
}
190193

194+
countMu.Unlock()
195+
191196
s.Suffix = fmt.Sprintf(" 正在翻译... (%d/%d 成功, %d 失败)", successCount, len(tasks), failCount)
192197
}(batch)
193198
}

tests/init_integration_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestInitEnhancedFlow(t *testing.T) {
2121

2222
// 1. Setup a fake Git repo
2323
require.NoError(t, exec.Command("git", "init", tempDir).Run())
24-
24+
2525
// Create a sample file
2626
sampleContent := `package main
2727
// Test Init
@@ -51,7 +51,7 @@ func main() {}`
5151
// JSON marshaling escapes '>', so we unmarshal to verify content properly
5252
var mappingData map[string]interface{}
5353
require.NoError(t, json.Unmarshal(mappingContent, &mappingData))
54-
54+
5555
comments := mappingData["comments"].(map[string]interface{})
5656
var foundTranslation bool
5757
for _, v := range comments {
@@ -91,7 +91,7 @@ func TestInitGlobalConfigInheritance(t *testing.T) {
9191
// Create a fake global config in a subfolder
9292
fakeHome := filepath.Join(tempDir, "fake_home")
9393
os.MkdirAll(filepath.Join(fakeHome, ".codei18n"), 0755)
94-
94+
9595
globalConfig := `{
9696
"sourceLanguage": "fr",
9797
"localLanguage": "de",
@@ -103,7 +103,7 @@ func TestInitGlobalConfigInheritance(t *testing.T) {
103103
}`
104104
os.WriteFile(filepath.Join(fakeHome, ".codei18n", "config.json"), []byte(globalConfig), 0644)
105105

106-
// Run init in a project dir, pointing to fake config via --config flag?
106+
// Run init in a project dir, pointing to fake config via --config flag?
107107
// The init command logic uses config.LoadConfig() which looks at HOME.
108108
// We cannot easily mock HOME env var for the subprocess unless we set it.
109109
projectDir := filepath.Join(tempDir, "project")
@@ -113,14 +113,14 @@ func TestInitGlobalConfigInheritance(t *testing.T) {
113113
cmd.Dir = projectDir
114114
// Mock HOME to point to fake_home
115115
cmd.Env = append(os.Environ(), "HOME="+fakeHome)
116-
116+
117117
out, err := cmd.CombinedOutput()
118118
require.NoError(t, err, string(out))
119119

120120
// Verify project config
121121
projectConfigPath := filepath.Join(projectDir, ".codei18n", "config.json")
122122
require.FileExists(t, projectConfigPath)
123-
123+
124124
content, _ := os.ReadFile(projectConfigPath)
125125
sContent := string(content)
126126

0 commit comments

Comments
 (0)