Go package for easy access to Microsoft Text Translation API and Google Translate API.
go get github.com/st3v/translator
Register for Microsoft Text Translation API (see instructions). Use the obtained subscription key to instantiate a translator as shown below.
package main
import (
"fmt""log""github.com/st3v/translator/microsoft"
)
funcmain() {
translator:=microsoft.NewTranslator("YOUR-SUBSCRIPTION-KEY")
translation, err:=translator.Translate("Hello World!", "en", "de")
iferr!=nil {
log.Panicf("Error during translation: %s", err.Error())
}
fmt.Println(translation)
}Sign-up for Google Developers Console and enable the Translate API ([see instructions] (https://cloud.google.com/translate/v2/getting_started#setup)). Obtain the API key for your application and use it to instantiate a translator as show below.
package main
import (
"fmt""log""github.com/st3v/translator/google"
)
funcmain() {
translator:=google.NewTranslator("YOUR-GOOGLE-API-KEY")
translation, err:=translator.Translate("Hello World!", "en", "de")
iferr!=nil {
log.Panicf("Error during translation: %s", err.Error())
}
fmt.Println(translation)
}Use the Translate function to translate text from one language to another. The
function expects the caller to use API-specific language codes to specify the source
and target language for the translation.
See Microsoft's or
Google's
documentation for a list of supported languages and their corresponding codes.
Or use the Languages function to programmatically obtain the list of
supported languages and their codes.
Signature
// Translate takes a string in a given language and returns its translation// to another language. Source and destination languages are specified by their// corresponding language codes.Translate(text, from, tostring) (string, error)Usage
translation, err:=translator.Translate("Hello World!", "en", "de")
iferr!=nil {
log.Panicf("Error during translation: %s", err.Error())
}
fmt.Printf("Translation: %s\n", translation)You can use the Detect function to detect the language of a give word or sentence.
Signature
// Detect identifies the language of the given text and returns the// corresponding language code.Detect(textstring) (string, error)Usage
languageCode, err:=translator.Detect("¿cómo está?")
iferr!=nil {
log.Panicf("Error detecting language: %s", err.Error())
}
fmt.Printf("Detected language code: %s", languageCode)The Languages function returns a list of all languages supported
by the API you are using. The function will provide you with the english
name and API-specific code for each language.
Signature
// Languages returns a slice of language structs that are supported// by the given translator.Languages() ([]Language, error)Usage
languages, err:=translator.Languages()
iferr!=nil {
log.Panicf("Error getting supported languages: %s", err.Error())
}
for_, language:=rangelanguages {
fmt.Printf("%s (%s)\n", language.Name, language.Code)
}Translator is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.
