Skip to content

Commit b2cd5e0

Browse files
committed
fix: Git hook bug
--bug=1
1 parent 99afbe5 commit b2cd5e0

File tree

10 files changed

+1858
-49
lines changed

10 files changed

+1858
-49
lines changed

.codei18n/mappings.json

Lines changed: 1815 additions & 0 deletions
Large diffs are not rendered by default.

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ go.work.sum
3535
bin/
3636

3737
# Build artifacts
38-
codei18n
3938
codei18n.exe
4039

4140
# macOS

adapters/translator/factory.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ import (
1010
"github.com/studyzy/codei18n/internal/log"
1111
)
1212

13-
// NewFromConfig 根据配置创建合适的翻译引擎实现。
13+
// NewFromConfig creates an appropriate translation engine implementation based on the configuration.
1414
//
15-
// 约定:
16-
// - provider "openai" "llm" 时,使用基于 OpenAI 兼容协议的 LLM 翻译
17-
// - provider "ollama" 时,使用本地 Ollama 服务
18-
// - provider "mock" 时,使用测试用的 MockTranslator
19-
// - provider "google" "deepl" 时,视为已废弃并返回错误
20-
// - provider 为空,则默认视为 "openai"
15+
// Conventions:
16+
// - When provider is "openai" or "llm", use LLM translation based on OpenAI-compatible protocol
17+
// - When provider is "ollama", use the local Ollama service
18+
// - When provider is "mock", use the MockTranslator for testing
19+
// - When provider is "google" or "deepl", it is considered deprecated and an error is returned
20+
// - If provider is empty, it defaults to "openai"
2121
func NewFromConfig(cfg *config.Config) (core.Translator, error) {
2222
provider := strings.ToLower(strings.TrimSpace(cfg.TranslationProvider))
2323
if provider == "" {
2424
provider = "openai"
2525
}
2626

27-
// 兼容文档中可能出现的 llm-api 命名
27+
// Handle compatibility with the llm-api naming that may appear in the documentation
2828
if provider == "llm-api" {
2929
provider = "openai"
3030
}
@@ -42,7 +42,7 @@ func NewFromConfig(cfg *config.Config) (core.Translator, error) {
4242

4343
baseURL := os.Getenv("OPENAI_BASE_URL")
4444
if cfg.TranslationConfig != nil {
45-
// 不同大小写和命名的兼容
45+
// Compatibility for different cases and naming conventions
4646
if val, ok := cfg.TranslationConfig["baseUrl"]; ok && val != "" {
4747
baseURL = val
4848
} else if val, ok := cfg.TranslationConfig["BaseUrl"]; ok && val != "" {
@@ -63,7 +63,7 @@ func NewFromConfig(cfg *config.Config) (core.Translator, error) {
6363
}
6464
}
6565

66-
// DeepSeek 模型的默认 BaseURL 自动检测
66+
// DeepSeek model default BaseURL auto-detection
6767
if baseURL == "" && (model == "deepseek-chat" || model == "deepseek-coder") {
6868
baseURL = "https://api.deepseek.com"
6969
log.Info("自动检测到 DeepSeek 模型,设置 BaseURL 为 %s", baseURL)

adapters/translator/factory_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/studyzy/codei18n/core/config"
77
)
88

9-
// 验证历史 provider(google/deepl)会被明确拒绝,并提示迁移路径。
9+
// Verification history providers (google/deepl) will be explicitly rejected with migration path instructions.
1010
func TestNewFromConfig_RejectsLegacyProviders(t *testing.T) {
1111
cases := []string{"google", "deepl"}
1212
for _, provider := range cases {
@@ -21,7 +21,7 @@ func TestNewFromConfig_RejectsLegacyProviders(t *testing.T) {
2121
}
2222
}
2323

24-
// 验证未知 provider 会被拒绝。
24+
// Verify that unknown providers are rejected.
2525
func TestNewFromConfig_UnknownProvider(t *testing.T) {
2626
cfg := &config.Config{TranslationProvider: "unknown-provider"}
2727
tr, err := NewFromConfig(cfg)

adapters/translator/ollama_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"time"
88
)
99

10-
// 单元测试:验证 NewOllamaTranslator 默认值行为(不依赖本地服务)。
10+
// Unit test: Verify the default value behavior of NewOllamaTranslator (without relying on local service).
1111
func TestNewOllamaTranslator_Defaults(t *testing.T) {
1212
tr := NewOllamaTranslator("", "")
1313
if tr.endpoint == "" {
@@ -21,16 +21,16 @@ func TestNewOllamaTranslator_Defaults(t *testing.T) {
2121
}
2222
}
2323

24-
// 集成测试(可选):连接本地 Ollama 服务并尝试真实翻译。
24+
// Integration test (optional): Connect to local Ollama service and attempt real translation.
2525
//
26-
// 启用方式:
26+
// How to enable:
2727
//
2828
// CODEI18N_OLLAMA_TEST=1 go test ./adapters/translator -run TestOllamaTranslator_Translate_Integration -v
2929
//
30-
// 可选环境变量:
30+
// Optional environment variables:
3131
//
32-
// CODEI18N_OLLAMA_ENDPOINT 默认为 http://localhost:11434
33-
// CODEI18N_OLLAMA_MODEL 默认为 qwen3:4b
32+
// CODEI18N_OLLAMA_ENDPOINT Defaults to http://localhost:11434
33+
// CODEI18N_OLLAMA_MODEL Defaults to qwen3:4b
3434
func TestOllamaTranslator_Translate_Integration(t *testing.T) {
3535
if os.Getenv("CODEI18N_OLLAMA_TEST") == "" {
3636
t.Skip("跳过 Ollama 集成测试:未设置 CODEI18N_OLLAMA_TEST")

cmd/codei18n/convert.go

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ func processFile(file string, adapter core.LanguageAdapter, store *mapping.Store
140140
newText string
141141
}
142142
var replacements []replacement
143+
144+
// Track comments that couldn't find translation
145+
missingTranslations := make(map[string]bool)
143146

144147
lines := strings.Split(string(src), "\n")
145148

@@ -214,6 +217,9 @@ func processFile(file string, adapter core.LanguageAdapter, store *mapping.Store
214217
// Ensure the new ID has bilingual data
215218
store.Set(newID, cfg.SourceLanguage, enText)
216219
store.Set(newID, cfg.LocalLanguage, zhText)
220+
// Delete the old ID (based on Chinese text) to keep mappings clean
221+
store.Delete(id)
222+
log.Info("Deleted old mapping ID: %s", id)
217223
}
218224

219225
break
@@ -222,10 +228,11 @@ func processFile(file string, adapter core.LanguageAdapter, store *mapping.Store
222228
}
223229
}
224230

225-
if !found {
226-
log.Warn("未找到注释的英文翻译: '%s'", normalizedCurrent)
227-
// Return early to count this as missing
228-
}
231+
if !found {
232+
log.Warn("未找到注释的英文翻译: '%s'", normalizedCurrent)
233+
// Mark this comment as missing translation
234+
missingTranslations[c.ID] = true
235+
}
229236
} else {
230237
// Apply Mode (EN -> ZH)
231238
// c.SourceText is EN
@@ -303,28 +310,8 @@ func processFile(file string, adapter core.LanguageAdapter, store *mapping.Store
303310
return 0
304311
}
305312

306-
// Count missing translations
307-
// Only count as missing if:
308-
// 1. The comment ID exists in mappings
309-
// 2. But the target language translation is missing
310-
missingCount := 0
311-
for _, c := range comments {
312-
if c.ID == "" {
313-
c.ID = utils.GenerateCommentID(c)
314-
}
315-
316-
// Check if this comment ID exists in mappings
317-
if transMap, exists := store.GetMapping().Comments[c.ID]; exists {
318-
// ID exists, check if target language translation exists
319-
if targetVal, hasTarget := transMap[convertTo]; !hasTarget || targetVal == "" {
320-
// Target translation is missing
321-
missingCount++
322-
log.Warn("注释 ID %s 缺少 %s 翻译", c.ID, convertTo)
323-
}
324-
}
325-
// If ID doesn't exist in mappings at all, it's a new comment
326-
// We don't count it as "missing translation" because it hasn't been scanned yet
327-
}
313+
// Count missing translations based on what we tracked during conversion
314+
missingCount := len(missingTranslations)
328315

329316
if err := os.WriteFile(file, []byte(newSrc), 0644); err != nil {
330317
log.Error("写入文件 %s 失败: %v", file, err)

cmd/codei18n/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func init() {
2525

2626
initCmd.Flags().StringVar(&initSourceLang, "source-lang", "en", "源码语言")
2727
initCmd.Flags().StringVar(&initTargetLang, "target-lang", "zh-CN", "本地目标语言")
28-
// 默认使用基于 LLM 的远程翻译(OpenAI 兼容协议)
28+
// Default to using LLM-based remote translation (OpenAI-compatible protocol)
2929
initCmd.Flags().StringVar(&initProvider, "provider", "openai", "翻译提供商 (openai/llm/ollama)")
3030
}
3131

cmd/codei18n/translate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ func runTranslate() {
5757
cfg.BatchSize = translateBatchSize
5858
}
5959

60-
// 如果通过命令行指定了模型,则覆盖配置中的 model
60+
// If a model is specified via the command line, it overrides the model in the configuration
6161
if translateModel != "" {
6262
if cfg.TranslationConfig == nil {
6363
cfg.TranslationConfig = make(map[string]string)
6464
}
6565
cfg.TranslationConfig["model"] = translateModel
6666
}
6767

68-
// 2. Init Translator(通过统一工厂创建)
68+
// 2. Init Translator (Created via unified factory)
6969
trans, err := translator.NewFromConfig(cfg)
7070
if err != nil {
7171
log.Fatal("初始化翻译引擎失败: %v", err)

core/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func DefaultConfig() *Config {
2424
SourceLanguage: "en",
2525
LocalLanguage: "zh-CN",
2626
ExcludePatterns: []string{".git/**", "vendor/**", ".codei18n/**"},
27-
// 默认使用基于 LLM 的远程翻译(OpenAI 兼容协议)
27+
// Default to using LLM-based remote translation (OpenAI-compatible protocol)
2828
TranslationProvider: "openai",
2929
TranslationConfig: make(map[string]string),
3030
BatchSize: 10,

core/mapping/store.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ func (s *Store) Set(id, lang, text string) {
104104
s.mapping.Comments[id][lang] = cleanText
105105
}
106106

107+
// Delete removes a comment mapping by ID
108+
func (s *Store) Delete(id string) {
109+
s.mu.Lock()
110+
defer s.mu.Unlock()
111+
112+
delete(s.mapping.Comments, id)
113+
}
114+
107115
// GetMapping returns the underlying mapping object (read-only copy recommended for complex ops)
108116
// For now returning pointer for simplicity in MVP
109117
func (s *Store) GetMapping() *domain.Mapping {

0 commit comments

Comments
 (0)