update translate

This commit is contained in:
Asim Aslam
2021-12-10 10:28:41 +00:00
parent 4ec7a7aa5c
commit 8b48ea58a4
4 changed files with 96 additions and 82 deletions

View File

@@ -3,6 +3,7 @@ package handler
import ( import (
"context" "context"
me "github.com/micro/micro/v3/service/errors"
"github.com/micro/micro/v3/service/config" "github.com/micro/micro/v3/service/config"
"github.com/micro/micro/v3/service/logger" "github.com/micro/micro/v3/service/logger"
"github.com/pkg/errors" "github.com/pkg/errors"
@@ -16,10 +17,10 @@ import (
type translation struct { type translation struct {
ApiKey string ApiKey string
Limit int
} }
func NewTranslation() *translation { func NewTranslation() *translation {
v, err := config.Get("translate.google.api_key") v, err := config.Get("translate.google.api_key")
if err != nil { if err != nil {
logger.Fatalf("translate.google.api_key config not found: %v", err) logger.Fatalf("translate.google.api_key config not found: %v", err)
@@ -30,8 +31,15 @@ func NewTranslation() *translation {
logger.Fatalf("translate.google.api_key config can not be an empty string") logger.Fatalf("translate.google.api_key config can not be an empty string")
} }
v, err = config.Get("translate.text.char_limit")
if err != nil {
logger.Fatalf("translate.text.char_limit config not found: %v", err)
}
limit := v.Int(0)
return &translation{ return &translation{
ApiKey: key, ApiKey: key,
Limit: limit,
} }
} }
@@ -54,7 +62,12 @@ func (t *translation) Text(ctx context.Context, req *pb.TextRequest, rsp *pb.Tex
return errors.Wrap(err, "google translation parse target language error") return errors.Wrap(err, "google translation parse target language error")
} }
result, err := client.Translate(ctx, req.Contents, target, &translate.Options{ // TODO: configurable char limit
if t.Limit > 0 && len(req.Content) > t.Limit {
return me.BadRequest("google.translate", "Exceeds char limit %d", t.Limit)
}
result, err := client.Translate(ctx, []string{req.Content}, target, &translate.Options{
Source: source, Source: source,
Format: translate.Format(req.Format), Format: translate.Format(req.Format),
Model: req.Model, Model: req.Model,
@@ -64,12 +77,14 @@ func (t *translation) Text(ctx context.Context, req *pb.TextRequest, rsp *pb.Tex
return errors.Wrap(err, "get google translation error") return errors.Wrap(err, "get google translation error")
} }
for _, v := range result { if len(result) == 0 {
rsp.Translations = append(rsp.Translations, &pb.BasicTranslation{ return nil
Text: v.Text, }
Source: v.Source.String(),
Model: v.Model, rsp.Translation = &pb.Translation{
}) Text: result[0].Text,
Source: result[0].Source.String(),
Model: result[0].Model,
} }
return nil return nil

View File

@@ -1,7 +1,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT. // Code generated by protoc-gen-go. DO NOT EDIT.
// versions: // versions:
// protoc-gen-go v1.27.1 // protoc-gen-go v1.27.1
// protoc v3.19.1 // protoc v3.15.6
// source: proto/translate.proto // source: proto/translate.proto
package translate package translate
@@ -20,7 +20,7 @@ const (
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
) )
type BasicTranslation struct { type Translation struct {
state protoimpl.MessageState state protoimpl.MessageState
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
@@ -33,8 +33,8 @@ type BasicTranslation struct {
Model string `protobuf:"bytes,3,opt,name=model,proto3" json:"model,omitempty"` Model string `protobuf:"bytes,3,opt,name=model,proto3" json:"model,omitempty"`
} }
func (x *BasicTranslation) Reset() { func (x *Translation) Reset() {
*x = BasicTranslation{} *x = Translation{}
if protoimpl.UnsafeEnabled { if protoimpl.UnsafeEnabled {
mi := &file_proto_translate_proto_msgTypes[0] mi := &file_proto_translate_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
@@ -42,13 +42,13 @@ func (x *BasicTranslation) Reset() {
} }
} }
func (x *BasicTranslation) String() string { func (x *Translation) String() string {
return protoimpl.X.MessageStringOf(x) return protoimpl.X.MessageStringOf(x)
} }
func (*BasicTranslation) ProtoMessage() {} func (*Translation) ProtoMessage() {}
func (x *BasicTranslation) ProtoReflect() protoreflect.Message { func (x *Translation) ProtoReflect() protoreflect.Message {
mi := &file_proto_translate_proto_msgTypes[0] mi := &file_proto_translate_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil { if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
@@ -60,26 +60,26 @@ func (x *BasicTranslation) ProtoReflect() protoreflect.Message {
return mi.MessageOf(x) return mi.MessageOf(x)
} }
// Deprecated: Use BasicTranslation.ProtoReflect.Descriptor instead. // Deprecated: Use Translation.ProtoReflect.Descriptor instead.
func (*BasicTranslation) Descriptor() ([]byte, []int) { func (*Translation) Descriptor() ([]byte, []int) {
return file_proto_translate_proto_rawDescGZIP(), []int{0} return file_proto_translate_proto_rawDescGZIP(), []int{0}
} }
func (x *BasicTranslation) GetText() string { func (x *Translation) GetText() string {
if x != nil { if x != nil {
return x.Text return x.Text
} }
return "" return ""
} }
func (x *BasicTranslation) GetSource() string { func (x *Translation) GetSource() string {
if x != nil { if x != nil {
return x.Source return x.Source
} }
return "" return ""
} }
func (x *BasicTranslation) GetModel() string { func (x *Translation) GetModel() string {
if x != nil { if x != nil {
return x.Model return x.Model
} }
@@ -93,7 +93,7 @@ type TextRequest struct {
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
// The contents to be translated // The contents to be translated
Contents []string `protobuf:"bytes,1,rep,name=contents,proto3" json:"contents,omitempty"` Content string `protobuf:"bytes,1,opt,name=content,proto3" json:"content,omitempty"`
// Source language, format in ISO-639-1 codes // Source language, format in ISO-639-1 codes
// See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes for more information // 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"` Source string `protobuf:"bytes,2,opt,name=source,proto3" json:"source,omitempty"`
@@ -139,11 +139,11 @@ func (*TextRequest) Descriptor() ([]byte, []int) {
return file_proto_translate_proto_rawDescGZIP(), []int{1} return file_proto_translate_proto_rawDescGZIP(), []int{1}
} }
func (x *TextRequest) GetContents() []string { func (x *TextRequest) GetContent() string {
if x != nil { if x != nil {
return x.Contents return x.Content
} }
return nil return ""
} }
func (x *TextRequest) GetSource() string { func (x *TextRequest) GetSource() string {
@@ -180,7 +180,7 @@ type TextResponse struct {
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
Translations []*BasicTranslation `protobuf:"bytes,1,rep,name=translations,proto3" json:"translations,omitempty"` Translation *Translation `protobuf:"bytes,1,opt,name=translation,proto3" json:"translation,omitempty"`
} }
func (x *TextResponse) Reset() { func (x *TextResponse) Reset() {
@@ -215,9 +215,9 @@ func (*TextResponse) Descriptor() ([]byte, []int) {
return file_proto_translate_proto_rawDescGZIP(), []int{2} return file_proto_translate_proto_rawDescGZIP(), []int{2}
} }
func (x *TextResponse) GetTranslations() []*BasicTranslation { func (x *TextResponse) GetTranslation() *Translation {
if x != nil { if x != nil {
return x.Translations return x.Translation
} }
return nil return nil
} }
@@ -227,32 +227,31 @@ var File_proto_translate_proto protoreflect.FileDescriptor
var file_proto_translate_proto_rawDesc = []byte{ var file_proto_translate_proto_rawDesc = []byte{
0x0a, 0x15, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x6c, 0x61, 0x74, 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, 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, 0x74, 0x65, 0x22, 0x4f, 0x0a, 0x0b, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x6c, 0x61, 0x74, 0x69, 0x6f,
0x6c, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x65, 0x78, 0x74, 0x18, 0x01, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x65, 0x78, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52,
0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x6f, 0x04, 0x74, 0x65, 0x78, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x18,
0x75, 0x72, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x6f, 0x75, 0x72, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x12, 0x14, 0x0a,
0x63, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x6d, 0x6f,
0x09, 0x52, 0x05, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x22, 0x87, 0x01, 0x0a, 0x0b, 0x54, 0x65, 0x78, 0x64, 0x65, 0x6c, 0x22, 0x85, 0x01, 0x0a, 0x0b, 0x54, 0x65, 0x78, 0x74, 0x52, 0x65, 0x71, 0x75,
0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x18, 0x01,
0x65, 0x6e, 0x74, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x09, 0x52, 0x08, 0x63, 0x6f, 0x6e, 0x74, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x12, 0x16, 0x0a,
0x65, 0x6e, 0x74, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x18, 0x02, 0x06, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73,
0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x18,
0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x74, 0x61, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x12, 0x16, 0x0a,
0x72, 0x67, 0x65, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x18, 0x04, 0x06, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x66,
0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x18, 0x05,
0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x6d, 0x6f, 0x64, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x22, 0x48, 0x0a, 0x0c, 0x54,
0x65, 0x6c, 0x22, 0x4f, 0x0a, 0x0c, 0x54, 0x65, 0x78, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x65, 0x78, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x38, 0x0a, 0x0b, 0x74,
0x73, 0x65, 0x12, 0x3f, 0x0a, 0x0c, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x6c, 0x61, 0x74, 0x69, 0x6f, 0x72, 0x61, 0x6e, 0x73, 0x6c, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b,
0x6e, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1b, 0x2e, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x32, 0x16, 0x2e, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x6c, 0x61, 0x74, 0x65, 0x2e, 0x54, 0x72, 0x61,
0x6c, 0x61, 0x74, 0x65, 0x2e, 0x42, 0x61, 0x73, 0x69, 0x63, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x6c, 0x6e, 0x73, 0x6c, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x0b, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x6c,
0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x0c, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x6c, 0x61, 0x74, 0x69, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x32, 0x46, 0x0a, 0x09, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x6c, 0x61,
0x6f, 0x6e, 0x73, 0x32, 0x46, 0x0a, 0x09, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x6c, 0x61, 0x74, 0x65, 0x74, 0x65, 0x12, 0x39, 0x0a, 0x04, 0x54, 0x65, 0x78, 0x74, 0x12, 0x16, 0x2e, 0x74, 0x72, 0x61,
0x12, 0x39, 0x0a, 0x04, 0x54, 0x65, 0x78, 0x74, 0x12, 0x16, 0x2e, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x6e, 0x73, 0x6c, 0x61, 0x74, 0x65, 0x2e, 0x54, 0x65, 0x78, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65,
0x6c, 0x61, 0x74, 0x65, 0x2e, 0x54, 0x65, 0x78, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x6c, 0x61, 0x74, 0x65, 0x2e, 0x54,
0x1a, 0x17, 0x2e, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x6c, 0x61, 0x74, 0x65, 0x2e, 0x54, 0x65, 0x78, 0x65, 0x78, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x13, 0x5a,
0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x13, 0x5a, 0x11, 0x2e, 0x11, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x6c, 0x61,
0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x6c, 0x61, 0x74, 0x65, 0x74, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
} }
var ( var (
@@ -269,12 +268,12 @@ func file_proto_translate_proto_rawDescGZIP() []byte {
var file_proto_translate_proto_msgTypes = make([]protoimpl.MessageInfo, 3) var file_proto_translate_proto_msgTypes = make([]protoimpl.MessageInfo, 3)
var file_proto_translate_proto_goTypes = []interface{}{ var file_proto_translate_proto_goTypes = []interface{}{
(*BasicTranslation)(nil), // 0: translate.BasicTranslation (*Translation)(nil), // 0: translate.Translation
(*TextRequest)(nil), // 1: translate.TextRequest (*TextRequest)(nil), // 1: translate.TextRequest
(*TextResponse)(nil), // 2: translate.TextResponse (*TextResponse)(nil), // 2: translate.TextResponse
} }
var file_proto_translate_proto_depIdxs = []int32{ var file_proto_translate_proto_depIdxs = []int32{
0, // 0: translate.TextResponse.translations:type_name -> translate.BasicTranslation 0, // 0: translate.TextResponse.translation:type_name -> translate.Translation
1, // 1: translate.Translate.Text:input_type -> translate.TextRequest 1, // 1: translate.Translate.Text:input_type -> translate.TextRequest
2, // 2: translate.Translate.Text:output_type -> translate.TextResponse 2, // 2: translate.Translate.Text:output_type -> translate.TextResponse
2, // [2:3] is the sub-list for method output_type 2, // [2:3] is the sub-list for method output_type
@@ -291,7 +290,7 @@ func file_proto_translate_proto_init() {
} }
if !protoimpl.UnsafeEnabled { if !protoimpl.UnsafeEnabled {
file_proto_translate_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { file_proto_translate_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*BasicTranslation); i { switch v := v.(*Translation); i {
case 0: case 0:
return &v.state return &v.state
case 1: case 1:

View File

@@ -5,37 +5,37 @@ package translate;
option go_package = "./proto;translate"; option go_package = "./proto;translate";
service Translate { service Translate {
rpc Text(TextRequest) returns (TextResponse) {} rpc Text(TextRequest) returns (TextResponse) {}
// TODO: implement Google Translation Advanced Edition, See https://cloud.google.com/translate/docs/advanced/translating-text-v3 // TODO: implement Google Translation Advanced Edition, See https://cloud.google.com/translate/docs/advanced/translating-text-v3
} }
message BasicTranslation { message Translation {
// The translation result // The translation result
string text = 1; string text = 1;
// The source of the query string // The source of the query string
string source = 2; string source = 2;
// The model used in translation // The model used in translation
string model = 3; string model = 3;
} }
// TextRequest is the basic edition request // TextRequest is the basic edition request
message TextRequest { message TextRequest {
// The contents to be translated // The contents to be translated
repeated string contents = 1; string content = 1;
// Source language, format in ISO-639-1 codes // Source language, format in ISO-639-1 codes
// See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes for more information // See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes for more information
string source = 2; string source = 2;
// Target language, format in ISO-639-1 codes // Target language, format in ISO-639-1 codes
// See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes for more information // See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes for more information
string target = 3; string target = 3;
// The string format, `text` or `html` // The string format, `text` or `html`
string format = 4; string format = 4;
// The model to use for translation, `nmt` or `base`, // 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 // See https://cloud.google.com/translate/docs/advanced/translating-text-v3#comparing-models for more information
string model = 5; string model = 5;
} }
// TextResponse is the basic edition response // TextResponse is the basic edition response
message TextResponse { message TextResponse {
repeated BasicTranslation translations = 1; Translation translation = 1;
} }

View File

@@ -4,6 +4,6 @@
"category": "language", "category": "language",
"display_name": "Translate", "display_name": "Translate",
"pricing": { "pricing": {
"Translate.Text" "Translate.Text": 10000
} }
} }