translator
translator copied to clipboard
Go client for Microsoft Text Translation API and Google Cloud Translation API
Translator
Go package for easy access to Microsoft Text Translation API and Google Translate API.
Installation
go get github.com/st3v/translator
Instantiation
Microsoft Text Translation API
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"
)
func main() {
translator := microsoft.NewTranslator("YOUR-SUBSCRIPTION-KEY")
translation, err := translator.Translate("Hello World!", "en", "de")
if err != nil {
log.Panicf("Error during translation: %s", err.Error())
}
fmt.Println(translation)
}
Google Translate API
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"
)
func main() {
translator := google.NewTranslator("YOUR-GOOGLE-API-KEY")
translation, err := translator.Translate("Hello World!", "en", "de")
if err != nil {
log.Panicf("Error during translation: %s", err.Error())
}
fmt.Println(translation)
}
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, to string) (string, error)
Usage
translation, err := translator.Translate("Hello World!", "en", "de")
if err != nil {
log.Panicf("Error during translation: %s", err.Error())
}
fmt.Printf("Translation: %s\n", translation)
Language Detection
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(text string) (string, error)
Usage
languageCode, err := translator.Detect("¿cómo está?")
if err != nil {
log.Panicf("Error detecting language: %s", err.Error())
}
fmt.Printf("Detected language code: %s", languageCode)
Supported Languages
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()
if err != nil {
log.Panicf("Error getting supported languages: %s", err.Error())
}
for _, language := range languages {
fmt.Printf("%s (%s)\n", language.Name, language.Code)
}
Licensing
Translator is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.