feat: add translation API (#297)

* feat: add translation

* feat: resurface translation by Google Translation

* feat: renamed the service as `translate` to make more readable

* fix: renamed response and request name to match rpc name

* chore: delete useless blank line

* Update publicapi.json

* Update publicapi.json

* Update README.md

Co-authored-by: Asim Aslam <asim@aslam.me>
This commit is contained in:
zhaoyang
2021-12-10 18:15:57 +08:00
committed by GitHub
parent 1d6234155a
commit 4ec7a7aa5c
15 changed files with 674 additions and 0 deletions

View File

@@ -0,0 +1,76 @@
package handler
import (
"context"
"github.com/micro/micro/v3/service/config"
"github.com/micro/micro/v3/service/logger"
"github.com/pkg/errors"
"golang.org/x/text/language"
"google.golang.org/api/option"
pb "github.com/micro/services/translate/proto"
"cloud.google.com/go/translate"
)
type translation struct {
ApiKey string
}
func NewTranslation() *translation {
v, err := config.Get("translate.google.api_key")
if err != nil {
logger.Fatalf("translate.google.api_key config not found: %v", err)
}
key := v.String("")
if key == "" {
logger.Fatalf("translate.google.api_key config can not be an empty string")
}
return &translation{
ApiKey: key,
}
}
// Text calls Google Cloud Translation Basic edition API
// For more information: https://cloud.google.com/translate/docs/samples/translate-text-with-model
func (t *translation) Text(ctx context.Context, req *pb.TextRequest, rsp *pb.TextResponse) error {
client, err := translate.NewClient(ctx, option.WithAPIKey(t.ApiKey))
if err != nil {
return errors.Wrap(err, "new google translation client error")
}
defer client.Close()
source, err := language.Parse(req.Source)
if err != nil {
return errors.Wrap(err, "google translation parse source language error")
}
target, err := language.Parse(req.Target)
if err != nil {
return errors.Wrap(err, "google translation parse target language error")
}
result, err := client.Translate(ctx, req.Contents, target, &translate.Options{
Source: source,
Format: translate.Format(req.Format),
Model: req.Model,
})
if err != nil {
return errors.Wrap(err, "get google translation error")
}
for _, v := range result {
rsp.Translations = append(rsp.Translations, &pb.BasicTranslation{
Text: v.Text,
Source: v.Source.String(),
Model: v.Model,
})
}
return nil
}