Documentation
¶
Index ¶
- func ApplyTranslations(xcstrings *model.XCStrings, responses []model.TranslationResponse)
- func CreateTranslationRequests(xcstrings *model.XCStrings, targetLanguages []string) []model.TranslationRequest
- func CreateTranslationRequestsForLanguage(xcstrings *model.XCStrings, targetLanguage string) []model.TranslationRequest
- func TranslatePerLanguage(ctx context.Context, xcstrings *model.XCStrings, targetLanguages []string, ...) ([]model.TranslationResponse, error)
- type BaiduTranslateResponse
- type BaiduTranslator
- type DeepLTranslateRequest
- type DeepLTranslateResponse
- type DeepLTranslator
- type GlossaryConfig
- type GoogleTranslateRequest
- type GoogleTranslateResponse
- type GoogleTranslator
- type OpenAIChatRequest
- type OpenAIChatResponse
- type OpenAIStreamOptions
- type OpenAITranslator
- type ProgressReporter
- type TranslationService
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyTranslations ¶
func ApplyTranslations(xcstrings *model.XCStrings, responses []model.TranslationResponse)
ApplyTranslations applies translated responses to the xcstrings data
func CreateTranslationRequests ¶
func CreateTranslationRequests(xcstrings *model.XCStrings, targetLanguages []string) []model.TranslationRequest
CreateTranslationRequests creates translation requests from xcstrings data
func CreateTranslationRequestsForLanguage ¶
func CreateTranslationRequestsForLanguage(xcstrings *model.XCStrings, targetLanguage string) []model.TranslationRequest
CreateTranslationRequestsForLanguage builds requests only for the given target language.
func TranslatePerLanguage ¶
func TranslatePerLanguage( ctx context.Context, xcstrings *model.XCStrings, targetLanguages []string, service *TranslationService, progressBuilder func(target string, total int) ProgressReporter, ) ([]model.TranslationResponse, error)
TranslatePerLanguage runs translations language by language to avoid building a massive request list. The progressBuilder can be nil; when provided it produces a ProgressReporter for each language.
Types ¶
type BaiduTranslateResponse ¶
type BaiduTranslateResponse struct {
From string `json:"from"`
To string `json:"to"`
TransResult []struct {
Src string `json:"src"`
Dst string `json:"dst"`
} `json:"trans_result"`
ErrorCode string `json:"error_code,omitempty"`
ErrorMsg string `json:"error_msg,omitempty"`
}
BaiduTranslateResponse represents the response from Baidu Translate API
type BaiduTranslator ¶
BaiduTranslator implements the TranslationProvider interface for Baidu Translate API
func NewBaiduTranslator ¶
func NewBaiduTranslator(appID, appSecret string) *BaiduTranslator
NewBaiduTranslator creates a new Baidu Translator instance
func (*BaiduTranslator) Translate ¶
func (b *BaiduTranslator) Translate(ctx context.Context, req model.TranslationRequest) (model.TranslationResponse, error)
Translate translates a string using Baidu Translate API
type DeepLTranslateRequest ¶
type DeepLTranslateRequest struct {
Text []string `json:"text"`
TargetLang string `json:"target_lang"`
SourceLang string `json:"source_lang,omitempty"`
SplitSentences string `json:"split_sentences,omitempty"`
PreserveFormatting bool `json:"preserve_formatting,omitempty"`
Formality string `json:"formality,omitempty"`
}
DeepLTranslateRequest represents the request body for DeepL API
type DeepLTranslateResponse ¶
type DeepLTranslateResponse struct {
Translations []struct {
DetectedSourceLanguage string `json:"detected_source_language"`
Text string `json:"text"`
} `json:"translations"`
}
DeepLTranslateResponse represents the response from DeepL API
type DeepLTranslator ¶
DeepLTranslator implements the TranslationProvider interface for DeepL API
func NewDeepLTranslator ¶
func NewDeepLTranslator(apiKey string, isFree bool) *DeepLTranslator
NewDeepLTranslator creates a new DeepL Translator instance
func (*DeepLTranslator) Translate ¶
func (d *DeepLTranslator) Translate(ctx context.Context, req model.TranslationRequest) (model.TranslationResponse, error)
Translate translates a string using DeepL API
type GlossaryConfig ¶
type GlossaryConfig struct {
Glossary string `json:"glossary"`
}
GlossaryConfig represents the glossary configuration for Google Translate API
type GoogleTranslateRequest ¶
type GoogleTranslateRequest struct {
Contents []string `json:"contents"`
SourceLanguage string `json:"sourceLanguageCode,omitempty"`
TargetLanguage string `json:"targetLanguageCode"`
MimeType string `json:"mimeType,omitempty"`
Model string `json:"model,omitempty"`
GlossaryConfig *GlossaryConfig `json:"glossaryConfig,omitempty"`
}
GoogleTranslateRequest represents the request body for Google Translate API
type GoogleTranslateResponse ¶
type GoogleTranslateResponse struct {
Translations []struct {
TranslatedText string `json:"translatedText"`
DetectedSourceLanguage string `json:"detectedSourceLanguage,omitempty"`
} `json:"translations"`
}
GoogleTranslateResponse represents the response from Google Translate API
type GoogleTranslator ¶
GoogleTranslator implements the TranslationProvider interface for Google Translate API
func NewGoogleTranslator ¶
func NewGoogleTranslator(apiKey string) *GoogleTranslator
NewGoogleTranslator creates a new Google Translator instance
func (*GoogleTranslator) Translate ¶
func (g *GoogleTranslator) Translate(ctx context.Context, req model.TranslationRequest) (model.TranslationResponse, error)
Translate translates a string using Google Translate API
type OpenAIChatRequest ¶
type OpenAIChatRequest struct {
Model string `json:"model"`
Messages []struct {
Role string `json:"role"`
Content string `json:"content"`
} `json:"messages"`
Temperature float64 `json:"temperature,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
TopP float64 `json:"top_p,omitempty"`
FrequencyPenalty float64 `json:"frequency_penalty,omitempty"`
PresencePenalty float64 `json:"presence_penalty,omitempty"`
Stream bool `json:"stream"`
StreamOptions *OpenAIStreamOptions `json:"stream_options,omitempty"`
}
OpenAIChatRequest represents the request body for OpenAI Chat API
type OpenAIChatResponse ¶
type OpenAIChatResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []struct {
Message struct {
Role string `json:"role"`
Content json.RawMessage `json:"content"`
} `json:"message"`
FinishReason string `json:"finish_reason"`
Index int `json:"index"`
} `json:"choices"`
Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
} `json:"usage"`
Error *struct {
Message string `json:"message"`
Type string `json:"type"`
Param string `json:"param,omitempty"`
Code string `json:"code,omitempty"`
} `json:"error,omitempty"`
}
OpenAIChatResponse represents the response from OpenAI Chat API
type OpenAIStreamOptions ¶
type OpenAIStreamOptions struct {
IncludeUsage bool `json:"include_usage"`
}
OpenAIStreamOptions represents stream_options to satisfy APIs that require it alongside the stream flag, even when streaming is disabled.
type OpenAITranslator ¶
type OpenAITranslator struct {
APIKey string
APIBaseURL string
Model string
Client *resty.Client
Temperature float64
MaxTokens int
}
OpenAITranslator implements the TranslationProvider interface for OpenAI compatible APIs
func NewOpenAITranslator ¶
func NewOpenAITranslator(apiKey, apiBaseURL, model string, temperature float64, maxTokens int) *OpenAITranslator
NewOpenAITranslator creates a new OpenAI Translator instance
func (*OpenAITranslator) Translate ¶
func (o *OpenAITranslator) Translate(ctx context.Context, req model.TranslationRequest) (model.TranslationResponse, error)
Translate translates a string using OpenAI Chat API
type ProgressReporter ¶
type ProgressReporter func(done, total int, last model.TranslationResponse)
ProgressReporter reports translation progress as responses are produced. done is the number of completed requests, total is the total number of requests, and last is the most recent response.
func NewVerboseProgressReporter ¶
func NewVerboseProgressReporter(target string, total int, verbose bool) ProgressReporter
NewVerboseProgressReporter prints coarse progress for a single language when verbose is true.
type TranslationService ¶
type TranslationService struct {
Provider model.TranslationProvider
Concurrency int
Timeout time.Duration
}
TranslationService manages the translation process with concurrency
func NewTranslationService ¶
func NewTranslationService(provider model.TranslationProvider, concurrency int, timeout time.Duration) *TranslationService
NewTranslationService creates a new TranslationService instance
func (*TranslationService) TranslateBatch ¶
func (s *TranslationService) TranslateBatch(ctx context.Context, requests []model.TranslationRequest, progress ProgressReporter) ([]model.TranslationResponse, error)
TranslateBatch translates multiple strings concurrently with optional progress reporting.