translator

package
v1.0.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2025 License: MIT Imports: 14 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

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"`
}

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 string `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 OpenAITranslator

type OpenAITranslator struct {
	APIKey     string
	APIBaseURL string
	Model      string
	Client     *resty.Client
}

OpenAITranslator implements the TranslationProvider interface for OpenAI compatible APIs

func NewOpenAITranslator

func NewOpenAITranslator(apiKey, apiBaseURL, model string) *OpenAITranslator

NewOpenAITranslator creates a new OpenAI Translator instance

func (*OpenAITranslator) Translate

Translate translates a string using OpenAI Chat API

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) ([]model.TranslationResponse, error)

TranslateBatch translates multiple strings concurrently

Jump to

Keyboard shortcuts

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