GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - st3v/translator: Go client for Microsoft Text Translation API and Google Cloud Translation API

BranchesTags
Open more actions menu

Latest commit

 

History

78 Commits
78 Commits

Folders and files

NameName
Last commit message
Last commit date
example
example
 
 
google
google
 
 
http
http
 
 
microsoft
microsoft
 
 
.gitignore
.gitignore
 
 
.travis.yml
.travis.yml
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
acceptance_test.go
acceptance_test.go
 
 
translator.go
translator.go
 
 
translator_test.go
translator_test.go
 
 

Repository files navigation

Build Status GoDoc

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.

About

Go client for Microsoft Text Translation API and Google Cloud Translation API

Topics

google-apigoogle-cloudgoogle-cloud-translation-apilanguagemicrosoft-apimicrosoft-azuremicrosoft-text-translation-apitexttranslationtranslation-api

Resources

Readme
Apache-2.0 license
Activity

Stars

20 stars

Watchers

1 watching

Forks

6 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.