translator

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2025 License: MIT Imports: 21 Imported by: 0

Documentation

Index

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

type BaiduTranslator struct {
	AppID     string
	AppSecret string
	Client    *resty.Client
}

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

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

type DeepLTranslator struct {
	APIKey string
	IsFree bool
	Client *resty.Client
}

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

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

type GoogleTranslator struct {
	APIKey string
	Client *resty.Client
}

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

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

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.

Jump to

Keyboard shortcuts

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