translate

package
v0.9.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2026 License: MIT Imports: 27 Imported by: 0

Documentation

Overview

Package translate implements AI-powered translation of PO file entries using multiple HTTP API-based AI providers: Google AI (Gemini), Groq, OpenCode (multi-format), GitHub Copilot (native OAuth), Custom OpenAI, and Ollama.

Index

Constants

View Source
const (
	ProviderGoogle       = "google"
	ProviderGemini       = "gemini"
	ProviderGroq         = "groq"
	ProviderOpenCode     = "opencode"
	ProviderCopilot      = "copilot"
	ProviderCustomOpenAI = "custom-openai"
	ProviderOllama       = "ollama"
)
View Source
const (
	ParallelSequential   = "sequential"
	ParallelFullParallel = "full-parallel"
)
View Source
const AndroidSystemPrompt = `` /* 1516-byte string literal not displayed */

AndroidSystemPrompt is the system prompt for translating Android strings.xml UI strings.

View Source
const DefaultSystemPrompt = `` /* 1422-byte string literal not displayed */
View Source
const DocsSystemPrompt = `` /* 2260-byte string literal not displayed */

DocsSystemPrompt is the system prompt for translating documentation PO files (man pages via po4a) that contain groff/roff markup.

View Source
const I18NextSystemPrompt = `` /* 1488-byte string literal not displayed */

I18NextSystemPrompt is the system prompt for translating i18next JSON UI strings.

Variables

This section is empty.

Functions

func DefaultProviders

func DefaultProviders() map[string]Provider

DefaultProviders returns the pre-configured provider definitions.

func Translate

func Translate(ctx context.Context, poFile *po.File, opts Options) error

Translate translates untranslated entries in a PO file using an AI provider. This is the single-language entry point used by the sequential path and by parallel workers.

func TranslateAll

func TranslateAll(ctx context.Context, langTasks []LangTask, opts Options) error

TranslateAll translates multiple languages according to opts.ParallelMode.

func TranslateAllARB added in v0.4.0

func TranslateAllARB(ctx context.Context, langTasks []ARBLangTask, opts Options) error

TranslateAllARB translates ARB files for all language tasks.

func TranslateAllAndroid added in v0.3.0

func TranslateAllAndroid(ctx context.Context, langTasks []AndroidLangTask, opts Options) error

TranslateAllAndroid translates multiple Android strings.xml files according to opts.ParallelMode.

func TranslateAllJSON

func TranslateAllJSON(ctx context.Context, langTasks []JSONLangTask, opts Options) error

TranslateAllJSON translates multiple i18next JSON files according to opts.ParallelMode.

func TranslateAllKV added in v0.9.1

func TranslateAllKV(ctx context.Context, langTasks []KVLangTask, opts Options, translator KVChunkTranslator) error

func TranslateAllMarkdown added in v0.4.0

func TranslateAllMarkdown(ctx context.Context, langTasks []MarkdownLangTask, opts Options) error

TranslateAllMarkdown translates Markdown files for all language tasks.

func TranslateAllProperties added in v0.4.0

func TranslateAllProperties(ctx context.Context, langTasks []PropertiesLangTask, opts Options) error

TranslateAllProperties translates .properties files for all language tasks.

func TranslateAllVueI18n added in v0.8.0

func TranslateAllVueI18n(ctx context.Context, langTasks []VueI18nLangTask, opts Options) error

TranslateAllVueI18n translates vue-i18n JSON files for all language tasks.

func TranslateAllYAML added in v0.4.0

func TranslateAllYAML(ctx context.Context, langTasks []YAMLLangTask, opts Options) error

TranslateAllYAML translates YAML files for all language tasks.

func TranslateMulti

func TranslateMulti(ctx context.Context, tasks []translationTask, opts Options) error

TranslateMulti translates multiple PO files: sequential or full-parallel.

Types

type ARBLangTask added in v0.4.0

type ARBLangTask struct {
	// Lang is the BCP-47 language code.
	Lang string
	// LangName is the human-readable language name.
	LangName string
	// FilePath is the absolute path to write the translated file.
	FilePath string
	// File is the target ARB file (synced from source).
	File *arbfile.File
	// SourceFile is the source ARB file.
	SourceFile *arbfile.File
}

ARBLangTask holds a single ARB language file ready for translation.

type AndroidLangTask added in v0.3.0

type AndroidLangTask struct {
	Lang       string
	LangName   string
	File       *android.File
	FilePath   string
	SourceFile *android.File // Source (English) file for looking up original values
}

AndroidLangTask is a language translation task for Android strings.xml files.

type JSONLangTask

type JSONLangTask struct {
	Lang     string
	LangName string
	File     *i18next.File
	FilePath string
}

JSONLangTask is a language translation task for i18next JSON files.

type KVChunkTranslator added in v0.9.2

type KVChunkTranslator interface {
	BuildUserPrompt(keys []string, srcVals map[string]string, opts Options) string
	DefaultChunkSize() int
}

func DefaultKVChunkTranslator added in v0.9.2

func DefaultKVChunkTranslator() KVChunkTranslator

func I18NextChunkTranslator added in v0.9.2

func I18NextChunkTranslator() KVChunkTranslator

func MarkdownKVChunkTranslator added in v0.9.2

func MarkdownKVChunkTranslator() KVChunkTranslator

type KVLangTask added in v0.9.2

type KVLangTask struct {
	Lang         string
	LangName     string
	FilePath     string
	File         formatfile.KVFile
	SourceValues map[string]string
}

type LangTask

type LangTask struct {
	Lang   string
	POFile *po.File
	POPath string
}

LangTask is a language translation task exposed to main.go.

type MarkdownLangTask added in v0.4.0

type MarkdownLangTask struct {
	// Lang is the BCP-47 language code.
	Lang string
	// LangName is the human-readable language name.
	LangName string
	// FilePath is the absolute path to write the translated file.
	FilePath string
	// File is the target Markdown file (synced from source).
	File *mdfile.File
	// SourceFile is the source Markdown file.
	SourceFile *mdfile.File
}

MarkdownLangTask holds a single Markdown language task ready for translation.

type Options

type Options struct {
	// Provider is the AI provider configuration.
	Provider Provider
	// Language is the target language code (e.g., "ru", "de").
	Language string
	// LanguageName is the human-readable name (e.g., "Russian", "German").
	LanguageName string
	// ChunkSize is how many strings to translate per API call (0 = all at once).
	ChunkSize int
	// ParallelMode controls parallelization (sequential, parallel-langs, parallel-chunks, full-parallel).
	ParallelMode string
	// MaxConcurrent is the maximum number of concurrent tasks for parallel modes.
	MaxConcurrent int
	// RequestDelay is the delay between launching parallel tasks.
	RequestDelay time.Duration
	// Timeout is the per-request timeout (overrides provider timeout if set).
	Timeout time.Duration
	// MaxRetries is the maximum number of retries on rate limit (429). Default: 3.
	MaxRetries int
	// RetranslateExisting if true, re-translates already translated entries.
	RetranslateExisting bool
	// TranslateFuzzy if true, translates fuzzy entries and clears the fuzzy flag.
	TranslateFuzzy bool
	// SystemPrompt overrides the default system prompt.
	SystemPrompt string
	// PromptType specifies which prompt to use: "default", "docs", "i18next", "android".
	// If SystemPrompt is set, this is ignored.
	PromptType string
	// OnProgress is called after each batch/chunk is translated.
	OnProgress func(lang string, done, total int)
	// OnLog emits log messages during translation.
	OnLog func(format string, args ...any)
	// OnError emits error messages during translation.
	OnError func(format string, args ...any)
	// Verbose enables detailed logging.
	Verbose bool
	// LockFile is the lock file for incremental translation.
	// If nil, all entries are translated (no incremental skipping).
	LockFile *lockfile.LockFile
	// LockTarget is the target key in the lock file (e.g. "gettext-ui", "i18next-web").
	// Used to scope checksums per target.
	LockTarget string
	// ForceTranslate if true, ignores lock file and translates everything.
	// Different from RetranslateExisting: that re-translates already-translated
	// entries; this bypasses the lock file's "unchanged source" check.
	ForceTranslate bool
	// LockedKeys lists keys whose existing translations must not be overwritten.
	// Locked keys are skipped even with --retranslate. Use --force to override.
	LockedKeys []string
	// IgnoredKeys lists keys completely excluded from translation.
	IgnoredKeys []string
	// LockedPatterns lists regex patterns; matching keys are treated as locked.
	LockedPatterns []*regexp.Regexp
}

Options controls the translation behavior.

type PropertiesLangTask added in v0.4.0

type PropertiesLangTask struct {
	// Lang is the BCP-47 language code.
	Lang string
	// LangName is the human-readable language name.
	LangName string
	// FilePath is the absolute path to write the translated file.
	FilePath string
	// File is the target .properties file (synced from source).
	File *propfile.File
	// SourceFile is the source .properties file.
	SourceFile *propfile.File
}

PropertiesLangTask holds a single .properties language file ready for translation.

type Provider

type Provider struct {
	// ID is the provider identifier (google, groq, opencode, etc.).
	ID string
	// Name is the display name.
	Name string
	// BaseURL is the API base URL.
	BaseURL string
	// APIKey is the authentication key (empty for local services).
	APIKey string
	// Model is the model identifier.
	Model string
	// Proxy is an optional HTTP/HTTPS proxy URL.
	Proxy string
	// Timeout is the request timeout.
	Timeout time.Duration
	// Temperature controls randomness (0..2). If 0, default is used.
	Temperature float64
}

Provider holds the configuration for an AI translation service.

type VueI18nLangTask added in v0.8.0

type VueI18nLangTask struct {
	Lang       string
	LangName   string
	FilePath   string
	File       *vuei18n.File
	SourceFile *vuei18n.File
}

VueI18nLangTask holds a single vue-i18n JSON language file.

type YAMLLangTask added in v0.4.0

type YAMLLangTask struct {
	// Lang is the BCP-47 language code (e.g. "ru", "de").
	Lang string
	// LangName is the human-readable language name (e.g. "Russian").
	LangName string
	// FilePath is the absolute path to write the translated file.
	FilePath string
	// File is the target YAML file (already synced from source).
	File *yamlfile.File
	// SourceFile is the source (English) YAML file.
	SourceFile *yamlfile.File
}

YAMLLangTask holds a single YAML language file ready for translation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL