Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ReTkk = regexp.MustCompile(`tkk:'(.+?)'`)
Functions ¶
func GetDefaultServiceUrls ¶
func GetDefaultServiceUrls() []string
func GetValidLanguageKey ¶ added in v0.0.3
GetValidLanguageKey is a method of the Translator struct that validates and returns the corresponding valid language key for a given language. It checks whether the provided language is a valid language code or language name in the 'languages' map.
Parameters: - lang: The language code or language name to be validated.
Returns: - string: The valid language code (key) corresponding to the provided language. - error: An error if the provided language is not a valid language code or language name in the 'languages' map.
Example Usage:
language := "es" validLang, err := translator.GetValidLanguageKey(language) if err != nil { fmt.Println("Error:", err) return } fmt.Println("Valid Language Code:", validLang)
Types ¶
type LDResponse ¶
type LDResponse struct { Sentences []sentence `json:"sentences"` Src string `json:"src,omitempty"` Spell interface{} `json:"spell,omitempty"` Confidence float64 `json:"confidence,omitempty"` LdResult LDResult `json:"ld_result,omitempty"` }
Language detection (LD) response
type LDResult ¶
type LDResult struct { Srclangs []string `json:"srclangs,omitempty"` SrclangsConfidences []float64 `json:"srclangs_confidences,omitempty"` ExtendedSrclangs []string `json:"extended_srclangs,omitempty"` }
Language detection (LD) result
type Translated ¶
type Translated struct { Src string // source language Dest string // destination language Origin string // original text Text string // translated text }
Translated result object.
type Translator ¶
type Translator struct {
// contains filtered or unexported fields
}
func New ¶
func New(config ...Config) *Translator
New is a function that creates a new instance of the Translator with the specified configuration. If no configuration is provided, default values are used.
Parameters: - config: Variadic parameter that accepts optional configurations for the Translator.
- ServiceUrls: A slice of service URLs used for making API requests. If not provided, defaultServiceUrls will be used.
- UserAgent: A slice of user agent strings used in the request headers. If not provided, defaultUserAgent will be used.
- Proxy: The proxy URL to be used for making API requests. It should be in the format "http://proxy.example.com:8080". If not provided, no proxy will be used.
Returns: - *Translator: A new instance of the Translator with the specified configurations.
Example Usage:
config := Config{ ServiceUrls: []string{"https://api.example.com/translate", "https://api.example.org/translate"}, UserAgent: []string{"MyApp/1.0", "MyOtherApp/2.0"}, Proxy: "http://proxy.example.com:8080", } translator := New(config)
func (*Translator) DetectLanguage ¶
func (a *Translator) DetectLanguage(origin, dest string) (LDResponse, error)
DetectLanguage is a public method of the Translator struct that allows users to detect the language of a given text and obtain its translation.
Parameters: - origin: The language code or "auto" to automatically detect the language of the input text. - dest: The language code for the desired translation output. (e.g., "es" for Spanish, "fr" for French)
Returns: - LDResponse: The detected language and translated text as a result of the language detection. - error: An error if there is any issue with the language detection or HTTP request.
Example Usage:
translator := New() originText := "Hello, how are you?" destinationLanguage := "es" detected, err := translator.DetectLanguage("auto", destinationLanguage) if err != nil { fmt.Println("Error:", err) return } fmt.Println("Detected Language:", detected.Lang) fmt.Println("Translated Text:", detected.Trans)
func (*Translator) GetAvaliableLanguages ¶
func (a *Translator) GetAvaliableLanguages() map[string]string
func (*Translator) Translate ¶
func (a *Translator) Translate(origin, src, dest string) (*Translated, error)
Translate is a public method of the Translator struct that allows users to translate text from one language to another using the Google Translate API.
Parameters: - origin: The text to be translated. - src: The language code of the source text. (e.g., "en" for English, "es" for Spanish) - dest: The language code for the desired translation output. (e.g., "es" for Spanish, "fr" for French)
Returns: - *Translated: A struct containing the translation result, including the source language, destination language, original text, and translated text. - error: An error if there is any issue with the translation or HTTP request.
Example Usage:
translator := NewTranslator("YOUR_API_KEY") originText := "Hello, how are you?" sourceLanguage := "en" destinationLanguage := "es" translated, err := translator.Translate(originText, sourceLanguage, destinationLanguage) if err != nil { fmt.Println("Error:", err) return } fmt.Println("Source Language:", translated.Src) fmt.Println("Destination Language:", translated.Dest) fmt.Println("Original Text:", translated.Origin) fmt.Println("Translated Text:", translated.Text)