From 4ec7a7aa5c0a42dd794cfb2a0f07f35644d8f3c9 Mon Sep 17 00:00:00 2001 From: zhaoyang Date: Fri, 10 Dec 2021 18:15:57 +0800 Subject: [PATCH] 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 --- go.mod | 3 + go.sum | 3 + translate/.gitignore | 2 + translate/Dockerfile | 3 + translate/Makefile | 28 +++ translate/README.md | 6 + translate/examples.json | 33 +++ translate/generate.go | 3 + translate/handler/translate.go | 76 ++++++ translate/main.go | 25 ++ translate/micro.mu | 1 + translate/proto/translate.pb.go | 348 ++++++++++++++++++++++++++ translate/proto/translate.pb.micro.go | 93 +++++++ translate/proto/translate.proto | 41 +++ translate/publicapi.json | 9 + 15 files changed, 674 insertions(+) create mode 100644 translate/.gitignore create mode 100644 translate/Dockerfile create mode 100644 translate/Makefile create mode 100644 translate/README.md create mode 100644 translate/examples.json create mode 100644 translate/generate.go create mode 100644 translate/handler/translate.go create mode 100644 translate/main.go create mode 100644 translate/micro.mu create mode 100644 translate/proto/translate.pb.go create mode 100644 translate/proto/translate.pb.micro.go create mode 100644 translate/proto/translate.proto create mode 100644 translate/publicapi.json diff --git a/go.mod b/go.mod index fc4125e..9bffceb 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/micro/services go 1.15 require ( + cloud.google.com/go/translate v1.0.0 github.com/PuerkitoBio/goquery v1.6.1 github.com/SlyMarbo/rss v1.0.1 github.com/Teamwork/spamc v0.0.0-20200109085853-a4e0c5c3f7a0 @@ -55,7 +56,9 @@ require ( golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 golang.org/x/net v0.0.0-20210614182718-04defd469f4e golang.org/x/oauth2 v0.0.0-20211005180243-6b3c2da341f1 + golang.org/x/text v0.3.6 google.golang.org/api v0.59.0 + google.golang.org/genproto v0.0.0-20211008145708-270636b82663 // indirect google.golang.org/grpc/examples v0.0.0-20211103202053-3b94303f3754 // indirect google.golang.org/protobuf v1.27.1 googlemaps.github.io/maps v1.3.1 diff --git a/go.sum b/go.sum index 7677398..433a0be 100644 --- a/go.sum +++ b/go.sum @@ -43,6 +43,8 @@ cloud.google.com/go/storage v1.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0Zeo cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohlUTyfDhBk= cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs= cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= +cloud.google.com/go/translate v1.0.0 h1:4KHxaDCkc+iV+jaED9omlm8EZHYq1AFYbgErLFfDRjI= +cloud.google.com/go/translate v1.0.0/go.mod h1:Rquxp1ipbzZpg64DuTFXrihXGmz+2IPrP8FC2lsvUQ4= contrib.go.opencensus.io/exporter/ocagent v0.4.12/go.mod h1:450APlNTSR6FrvC3CTRqYosuDstRB9un7SOx2k/9ckA= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/Azure/azure-sdk-for-go v32.4.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= @@ -1122,6 +1124,7 @@ google.golang.org/genproto v0.0.0-20210828152312-66f60bf46e71/go.mod h1:eFjDcFEc google.golang.org/genproto v0.0.0-20210831024726-fe130286e0e2/go.mod h1:eFjDcFEctNawg4eG61bRv87N7iHBWyVhJu7u1kqDUXY= google.golang.org/genproto v0.0.0-20210903162649-d08c68adba83/go.mod h1:eFjDcFEctNawg4eG61bRv87N7iHBWyVhJu7u1kqDUXY= google.golang.org/genproto v0.0.0-20210909211513-a8c4777a87af/go.mod h1:eFjDcFEctNawg4eG61bRv87N7iHBWyVhJu7u1kqDUXY= +google.golang.org/genproto v0.0.0-20210921142501-181ce0d877f6/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc= google.golang.org/genproto v0.0.0-20210924002016-3dee208752a0/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc= google.golang.org/genproto v0.0.0-20211008145708-270636b82663 h1:33YYJanAOLxgS7pGjzC1IUrFLpwN//bbgtnKYikOmQU= google.golang.org/genproto v0.0.0-20211008145708-270636b82663/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc= diff --git a/translate/.gitignore b/translate/.gitignore new file mode 100644 index 0000000..5f2df16 --- /dev/null +++ b/translate/.gitignore @@ -0,0 +1,2 @@ + +translate diff --git a/translate/Dockerfile b/translate/Dockerfile new file mode 100644 index 0000000..832e068 --- /dev/null +++ b/translate/Dockerfile @@ -0,0 +1,3 @@ +FROM alpine +ADD translate /translate +ENTRYPOINT [ "/translate" ] diff --git a/translate/Makefile b/translate/Makefile new file mode 100644 index 0000000..9ced1ff --- /dev/null +++ b/translate/Makefile @@ -0,0 +1,28 @@ + +GOPATH:=$(shell go env GOPATH) +.PHONY: init +init: + go get -u github.com/golang/protobuf/proto + go get -u github.com/golang/protobuf/protoc-gen-go + go get github.com/micro/micro/v3/cmd/protoc-gen-micro + go get github.com/micro/micro/v3/cmd/protoc-gen-openapi + +.PHONY: api +api: + protoc --openapi_out=. --proto_path=. proto/translate.proto + +.PHONY: proto +proto: + protoc --proto_path=. --micro_out=. --go_out=:. proto/translate.proto + +.PHONY: build +build: + go build -o translate*.go + +.PHONY: test +test: + go test -v ./... -cover + +.PHONY: docker +docker: + docker build . -t translate:latest diff --git a/translate/README.md b/translate/README.md new file mode 100644 index 0000000..4093ea1 --- /dev/null +++ b/translate/README.md @@ -0,0 +1,6 @@ +Language translation service + +# Translate Service + +Translate basic text from any language. A simple API powered by Google Translate. +Translation text character limit is 500. diff --git a/translate/examples.json b/translate/examples.json new file mode 100644 index 0000000..f5f0f75 --- /dev/null +++ b/translate/examples.json @@ -0,0 +1,33 @@ +{ + "text": [ + { + "title": "Translate String", + "run_check": false, + "description": "Google Basic Edition Translation", + "request": { + "contents": [ + "pen", + "pencil" + ], + "model": "nmt", + "format": "text", + "source": "en", + "target": "zh" + }, + "response": { + "translations": [ + { + "text": "钢笔", + "source": "und", + "model": "nmt" + }, + { + "text": "铅笔", + "source": "und", + "model": "nmt" + } + ] + } + } + ] +} diff --git a/translate/generate.go b/translate/generate.go new file mode 100644 index 0000000..7d9db91 --- /dev/null +++ b/translate/generate.go @@ -0,0 +1,3 @@ +package main + +//go:generate make proto diff --git a/translate/handler/translate.go b/translate/handler/translate.go new file mode 100644 index 0000000..9812d2d --- /dev/null +++ b/translate/handler/translate.go @@ -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 +} diff --git a/translate/main.go b/translate/main.go new file mode 100644 index 0000000..6c91602 --- /dev/null +++ b/translate/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "github.com/micro/services/translate/handler" + pb "github.com/micro/services/translate/proto" + + "github.com/micro/micro/v3/service" + "github.com/micro/micro/v3/service/logger" +) + +func main() { + // Create service + srv := service.New( + service.Name("translate"), + service.Version("latest"), + ) + + // Register handler + pb.RegisterTranslateHandler(srv.Server(), handler.NewTranslation()) + + // Run service + if err := srv.Run(); err != nil { + logger.Fatal(err) + } +} diff --git a/translate/micro.mu b/translate/micro.mu new file mode 100644 index 0000000..fc33d54 --- /dev/null +++ b/translate/micro.mu @@ -0,0 +1 @@ +service translate diff --git a/translate/proto/translate.pb.go b/translate/proto/translate.pb.go new file mode 100644 index 0000000..21f5e69 --- /dev/null +++ b/translate/proto/translate.pb.go @@ -0,0 +1,348 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.27.1 +// protoc v3.19.1 +// source: proto/translate.proto + +package translate + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type BasicTranslation struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // The translation result + Text string `protobuf:"bytes,1,opt,name=text,proto3" json:"text,omitempty"` + // The source of the query string + Source string `protobuf:"bytes,2,opt,name=source,proto3" json:"source,omitempty"` + // The model used in translation + Model string `protobuf:"bytes,3,opt,name=model,proto3" json:"model,omitempty"` +} + +func (x *BasicTranslation) Reset() { + *x = BasicTranslation{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_translate_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *BasicTranslation) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*BasicTranslation) ProtoMessage() {} + +func (x *BasicTranslation) ProtoReflect() protoreflect.Message { + mi := &file_proto_translate_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use BasicTranslation.ProtoReflect.Descriptor instead. +func (*BasicTranslation) Descriptor() ([]byte, []int) { + return file_proto_translate_proto_rawDescGZIP(), []int{0} +} + +func (x *BasicTranslation) GetText() string { + if x != nil { + return x.Text + } + return "" +} + +func (x *BasicTranslation) GetSource() string { + if x != nil { + return x.Source + } + return "" +} + +func (x *BasicTranslation) GetModel() string { + if x != nil { + return x.Model + } + return "" +} + +// TextRequest is the basic edition request +type TextRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // The contents to be translated + Contents []string `protobuf:"bytes,1,rep,name=contents,proto3" json:"contents,omitempty"` + // Source language, format in ISO-639-1 codes + // See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes for more information + Source string `protobuf:"bytes,2,opt,name=source,proto3" json:"source,omitempty"` + // Target language, format in ISO-639-1 codes + // See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes for more information + Target string `protobuf:"bytes,3,opt,name=target,proto3" json:"target,omitempty"` + // The string format, `text` or `html` + Format string `protobuf:"bytes,4,opt,name=format,proto3" json:"format,omitempty"` + // The model to use for translation, `nmt` or `base`, + // See https://cloud.google.com/translate/docs/advanced/translating-text-v3#comparing-models for more information + Model string `protobuf:"bytes,5,opt,name=model,proto3" json:"model,omitempty"` +} + +func (x *TextRequest) Reset() { + *x = TextRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_translate_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *TextRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*TextRequest) ProtoMessage() {} + +func (x *TextRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_translate_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use TextRequest.ProtoReflect.Descriptor instead. +func (*TextRequest) Descriptor() ([]byte, []int) { + return file_proto_translate_proto_rawDescGZIP(), []int{1} +} + +func (x *TextRequest) GetContents() []string { + if x != nil { + return x.Contents + } + return nil +} + +func (x *TextRequest) GetSource() string { + if x != nil { + return x.Source + } + return "" +} + +func (x *TextRequest) GetTarget() string { + if x != nil { + return x.Target + } + return "" +} + +func (x *TextRequest) GetFormat() string { + if x != nil { + return x.Format + } + return "" +} + +func (x *TextRequest) GetModel() string { + if x != nil { + return x.Model + } + return "" +} + +// TextResponse is the basic edition response +type TextResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Translations []*BasicTranslation `protobuf:"bytes,1,rep,name=translations,proto3" json:"translations,omitempty"` +} + +func (x *TextResponse) Reset() { + *x = TextResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_translate_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *TextResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*TextResponse) ProtoMessage() {} + +func (x *TextResponse) ProtoReflect() protoreflect.Message { + mi := &file_proto_translate_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use TextResponse.ProtoReflect.Descriptor instead. +func (*TextResponse) Descriptor() ([]byte, []int) { + return file_proto_translate_proto_rawDescGZIP(), []int{2} +} + +func (x *TextResponse) GetTranslations() []*BasicTranslation { + if x != nil { + return x.Translations + } + return nil +} + +var File_proto_translate_proto protoreflect.FileDescriptor + +var file_proto_translate_proto_rawDesc = []byte{ + 0x0a, 0x15, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x6c, 0x61, 0x74, + 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x09, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x6c, 0x61, + 0x74, 0x65, 0x22, 0x54, 0x0a, 0x10, 0x42, 0x61, 0x73, 0x69, 0x63, 0x54, 0x72, 0x61, 0x6e, 0x73, + 0x6c, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x65, 0x78, 0x74, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x6f, + 0x75, 0x72, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x6f, 0x75, 0x72, + 0x63, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x05, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x22, 0x87, 0x01, 0x0a, 0x0b, 0x54, 0x65, 0x78, + 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x63, 0x6f, 0x6e, 0x74, + 0x65, 0x6e, 0x74, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x09, 0x52, 0x08, 0x63, 0x6f, 0x6e, 0x74, + 0x65, 0x6e, 0x74, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x18, 0x02, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x12, 0x16, 0x0a, 0x06, + 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x74, 0x61, + 0x72, 0x67, 0x65, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x18, 0x04, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x12, 0x14, 0x0a, 0x05, + 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x6d, 0x6f, 0x64, + 0x65, 0x6c, 0x22, 0x4f, 0x0a, 0x0c, 0x54, 0x65, 0x78, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, + 0x73, 0x65, 0x12, 0x3f, 0x0a, 0x0c, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x6c, 0x61, 0x74, 0x69, 0x6f, + 0x6e, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1b, 0x2e, 0x74, 0x72, 0x61, 0x6e, 0x73, + 0x6c, 0x61, 0x74, 0x65, 0x2e, 0x42, 0x61, 0x73, 0x69, 0x63, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x6c, + 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x0c, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x6c, 0x61, 0x74, 0x69, + 0x6f, 0x6e, 0x73, 0x32, 0x46, 0x0a, 0x09, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x6c, 0x61, 0x74, 0x65, + 0x12, 0x39, 0x0a, 0x04, 0x54, 0x65, 0x78, 0x74, 0x12, 0x16, 0x2e, 0x74, 0x72, 0x61, 0x6e, 0x73, + 0x6c, 0x61, 0x74, 0x65, 0x2e, 0x54, 0x65, 0x78, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x1a, 0x17, 0x2e, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x6c, 0x61, 0x74, 0x65, 0x2e, 0x54, 0x65, 0x78, + 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x13, 0x5a, 0x11, 0x2e, + 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x6c, 0x61, 0x74, 0x65, + 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_proto_translate_proto_rawDescOnce sync.Once + file_proto_translate_proto_rawDescData = file_proto_translate_proto_rawDesc +) + +func file_proto_translate_proto_rawDescGZIP() []byte { + file_proto_translate_proto_rawDescOnce.Do(func() { + file_proto_translate_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_translate_proto_rawDescData) + }) + return file_proto_translate_proto_rawDescData +} + +var file_proto_translate_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_proto_translate_proto_goTypes = []interface{}{ + (*BasicTranslation)(nil), // 0: translate.BasicTranslation + (*TextRequest)(nil), // 1: translate.TextRequest + (*TextResponse)(nil), // 2: translate.TextResponse +} +var file_proto_translate_proto_depIdxs = []int32{ + 0, // 0: translate.TextResponse.translations:type_name -> translate.BasicTranslation + 1, // 1: translate.Translate.Text:input_type -> translate.TextRequest + 2, // 2: translate.Translate.Text:output_type -> translate.TextResponse + 2, // [2:3] is the sub-list for method output_type + 1, // [1:2] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name +} + +func init() { file_proto_translate_proto_init() } +func file_proto_translate_proto_init() { + if File_proto_translate_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_proto_translate_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*BasicTranslation); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_translate_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*TextRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_translate_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*TextResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_proto_translate_proto_rawDesc, + NumEnums: 0, + NumMessages: 3, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_proto_translate_proto_goTypes, + DependencyIndexes: file_proto_translate_proto_depIdxs, + MessageInfos: file_proto_translate_proto_msgTypes, + }.Build() + File_proto_translate_proto = out.File + file_proto_translate_proto_rawDesc = nil + file_proto_translate_proto_goTypes = nil + file_proto_translate_proto_depIdxs = nil +} diff --git a/translate/proto/translate.pb.micro.go b/translate/proto/translate.pb.micro.go new file mode 100644 index 0000000..7a9cc3a --- /dev/null +++ b/translate/proto/translate.pb.micro.go @@ -0,0 +1,93 @@ +// Code generated by protoc-gen-micro. DO NOT EDIT. +// source: proto/translate.proto + +package translate + +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) + +import ( + context "context" + api "github.com/micro/micro/v3/service/api" + client "github.com/micro/micro/v3/service/client" + server "github.com/micro/micro/v3/service/server" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package + +// Reference imports to suppress errors if they are not otherwise used. +var _ api.Endpoint +var _ context.Context +var _ client.Option +var _ server.Option + +// Api Endpoints for Translate service + +func NewTranslateEndpoints() []*api.Endpoint { + return []*api.Endpoint{} +} + +// Client API for Translate service + +type TranslateService interface { + Text(ctx context.Context, in *TextRequest, opts ...client.CallOption) (*TextResponse, error) +} + +type translateService struct { + c client.Client + name string +} + +func NewTranslateService(name string, c client.Client) TranslateService { + return &translateService{ + c: c, + name: name, + } +} + +func (c *translateService) Text(ctx context.Context, in *TextRequest, opts ...client.CallOption) (*TextResponse, error) { + req := c.c.NewRequest(c.name, "Translate.Text", in) + out := new(TextResponse) + err := c.c.Call(ctx, req, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// Server API for Translate service + +type TranslateHandler interface { + Text(context.Context, *TextRequest, *TextResponse) error +} + +func RegisterTranslateHandler(s server.Server, hdlr TranslateHandler, opts ...server.HandlerOption) error { + type translate interface { + Text(ctx context.Context, in *TextRequest, out *TextResponse) error + } + type Translate struct { + translate + } + h := &translateHandler{hdlr} + return s.Handle(s.NewHandler(&Translate{h}, opts...)) +} + +type translateHandler struct { + TranslateHandler +} + +func (h *translateHandler) Text(ctx context.Context, in *TextRequest, out *TextResponse) error { + return h.TranslateHandler.Text(ctx, in, out) +} diff --git a/translate/proto/translate.proto b/translate/proto/translate.proto new file mode 100644 index 0000000..6605081 --- /dev/null +++ b/translate/proto/translate.proto @@ -0,0 +1,41 @@ +syntax = "proto3"; + +package translate; + +option go_package = "./proto;translate"; + +service Translate { + rpc Text(TextRequest) returns (TextResponse) {} + // TODO: implement Google Translation Advanced Edition, See https://cloud.google.com/translate/docs/advanced/translating-text-v3 +} + +message BasicTranslation { + // The translation result + string text = 1; + // The source of the query string + string source = 2; + // The model used in translation + string model = 3; +} + +// TextRequest is the basic edition request +message TextRequest { + // The contents to be translated + repeated string contents = 1; + // Source language, format in ISO-639-1 codes + // See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes for more information + string source = 2; + // Target language, format in ISO-639-1 codes + // See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes for more information + string target = 3; + // The string format, `text` or `html` + string format = 4; + // The model to use for translation, `nmt` or `base`, + // See https://cloud.google.com/translate/docs/advanced/translating-text-v3#comparing-models for more information + string model = 5; +} + +// TextResponse is the basic edition response +message TextResponse { + repeated BasicTranslation translations = 1; +} diff --git a/translate/publicapi.json b/translate/publicapi.json new file mode 100644 index 0000000..788a078 --- /dev/null +++ b/translate/publicapi.json @@ -0,0 +1,9 @@ +{ + "name": "translate", + "icon": "📖", + "category": "language", + "display_name": "Translate", + "pricing": { + "Translate.Text" + } +}