- 0.117.0 (latest)
- 0.116.0
- 0.115.1
- 0.114.0
- 0.113.0
- 0.112.2
- 0.111.0
- 0.110.10
- 0.109.0
- 0.108.0
- 0.107.0
- 0.106.0
- 0.105.0
- 0.104.0
- 0.103.0
- 0.102.1
- 0.101.1
- 0.100.2
- 0.99.0
- 0.98.0
- 0.97.0
- 0.96.0
- 0.95.0
- 0.94.1
- 0.93.3
- 0.92.3
- 0.91.1
- 0.90.0
- 0.89.0
- 0.88.0
- 0.87.0
- 0.86.0
- 0.85.0
- 0.84.0
- 0.83.0
- 0.82.0
- 0.81.0
- 0.80.0
- 0.79.0
- 0.78.0
- 0.77.0
- 0.76.0
- 0.75.0
Package translate is the v2 client for the Google Translation API.
PLEASE NOTE: We recommend using the new v3 client for new projects: https://cloud.google.com/go/translate/apiv3.
See https://cloud.google.com/translation for details.
Constants
Scope
const Scope = raw.CloudPlatformScope
Scope is the OAuth2 scope required by the Google Cloud Vision API.
Client
type Client struct {
// contains filtered or unexported fields
}
Client is a client for the translate API.
func NewClient
NewClient constructs a new Client that can perform Translation operations.
You can find or create API key for your project from the Credentials page of the Developers Console (console.developers.google.com).
Example
package main
import (
"context"
"cloud.google.com/go/translate"
)
func main() {
ctx := context.Background()
client, err := translate.NewClient(ctx)
if err != nil {
// TODO: handle error.
}
// Use the client.
// Close the client when finished.
if err := client.Close(); err != nil {
// TODO: handle error.
}
}
func (*Client) Close
Close closes any resources held by the client. Close should be called when the client is no longer needed. It need not be called at program exit.
func (*Client) DetectLanguage
DetectLanguage attempts to determine the language of the inputs. Each input string may be in a different language.
Each slice of Detections in the return value corresponds with one input string. A slice of Detections holds multiple hypotheses for the language of a single input string.
Example
package main
import (
"context"
"fmt"
"cloud.google.com/go/translate"
)
func main() {
ctx := context.Background()
client, err := translate.NewClient(ctx)
if err != nil {
// TODO: handle error.
}
ds, err := client.DetectLanguage(ctx, []string{"Today is Monday"})
if err != nil {
// TODO: handle error.
}
fmt.Println(ds)
}
func (*Client) SupportedLanguages
SupportedLanguages returns a list of supported languages for translation. The target parameter is the language to use to return localized, human readable names of supported languages.
Example
package main
import (
"context"
"fmt"
"cloud.google.com/go/translate"
"golang.org/x/text/language"
)
func main() {
ctx := context.Background()
client, err := translate.NewClient(ctx)
if err != nil {
// TODO: handle error.
}
langs, err := client.SupportedLanguages(ctx, language.English)
if err != nil {
// TODO: handle error.
}
fmt.Println(langs)
}
func (*Client) Translate
func (c *Client) Translate(ctx context.Context, inputs []string, target language.Tag, opts *Options) ([]Translation, error)
Translate one or more strings of text from a source language to a target language. All inputs must be in the same language.
The target parameter supplies the language to translate to. The supported languages are listed at https://cloud.google.com/translation/v2/translate-reference#supported_languages. You can also call the SupportedLanguages method.
The returned Translations appear in the same order as the inputs.
Example
package main
import (
"context"
"fmt"
"cloud.google.com/go/translate"
"golang.org/x/text/language"
)
func main() {
ctx := context.Background()
client, err := translate.NewClient(ctx)
if err != nil {
// TODO: handle error.
}
translations, err := client.Translate(ctx,
[]string{"Le singe est sur la branche"}, language.English,
&translate.Options{
Source: language.French,
Format: translate.Text,
})
if err != nil {
// TODO: handle error.
}
fmt.Println(translations[0].Text)
}
Detection
type Detection struct {
// Language is the code of the language detected.
Language language.Tag
// Confidence is a number from 0 to 1, with higher numbers indicating more
// confidence in the detection.
Confidence float64
// IsReliable indicates whether the language detection result is reliable.
IsReliable bool
}
Detection represents information about a language detected in an input.
Format
type Format string
Format is the format of the input text. Used in Options.Format.
HTML, Text
Constants for Options.Format.
Language
type Language struct {
// Name is the human-readable name of the language.
Name string
// Tag is a standard code for the language.
Tag language.Tag
}
A Language describes a language supported for translation.
Options
type Options struct {
// Source is the language of the input strings. If empty, the service will
// attempt to identify the source language automatically and return it within
// the response.
Source language.Tag
// Format describes the format of the input texts. The choices are HTML or
// Text. The default is HTML.
Format Format
// The model to use for translation. The choices are "nmt" or "base". The
// default is "base".
Model string
}
Options contains options for Translate.
Translation
type Translation struct {
// Text is the input text translated into the target language.
Text string
// Source is the detected language of the input text, if source was
// not supplied to Client.Translate. If source was supplied, this field
// will be empty.
Source language.Tag
// Model is the model that was used for translation.
// It may not match the model provided as an option to Client.Translate.
Model string
}
Translation contains the results of translating a piece of text.