Documentation
¶
Index ¶
Constants ¶
const (
DefaultUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
)
DefaultUserAgent matches the JS default (ensures compatibility with Microsoft's API)
Variables ¶
var LangMap map[string]string
LangMap maps language codes to names (e.g., "zh-Hans": "Chinese Simplified")
Functions ¶
func GetLangCode ¶
GetLangCode normalizes language input (e.g., "zh" → "zh-Hans", "english" → "en")
func IsLangSupported ¶
IsLangSupported checks if a language code/name is supported
Types ¶
type Client ¶
type Client struct {
ManualToken string // 手动设置的 Token(优先级高于自动获取)
// contains filtered or unexported fields
}
Client is the main struct for Microsoft Translator (thread-safe)
func (*Client) SetManualToken ¶
SetManualToken 方法,用于手动设置 Token
func (*Client) SetUserAgent ¶
SetUserAgent overrides the default User-Agent (for customization)
func (*Client) Translate ¶
func (c *Client) Translate(text any, from, to string) ([]TranslationResult, error)
Translate is the core API: translates text (single/multiple) between languages - text: string or []string (content to translate) - from: source language (use "" or "auto-detect" for auto-detection) - to: target language (e.g., "zh-Hans", "en")
type TranslationResult ¶
type TranslationResult struct {
DetectedLanguage struct {
Language string `json:"language"`
Score float64 `json:"score"`
} `json:"detectedLanguage,omitempty"`
Translations []struct {
Text string `json:"text"`
To string `json:"to"`
Transliteration struct {
Text string `json:"text,omitempty"`
} `json:"transliteration,omitempty"`
} `json:"translations"`
}
TranslationResult represents a single translation result (matches MS API response)