From 8b48ea58a435c21dd98985e31da0fc423ff8e1a9 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Fri, 10 Dec 2021 10:28:41 +0000 Subject: [PATCH] update translate --- translate/handler/translate.go | 31 ++++++++--- translate/proto/translate.pb.go | 99 ++++++++++++++++----------------- translate/proto/translate.proto | 46 +++++++-------- translate/publicapi.json | 2 +- 4 files changed, 96 insertions(+), 82 deletions(-) diff --git a/translate/handler/translate.go b/translate/handler/translate.go index 9812d2d..a287b91 100644 --- a/translate/handler/translate.go +++ b/translate/handler/translate.go @@ -3,6 +3,7 @@ package handler import ( "context" + me "github.com/micro/micro/v3/service/errors" "github.com/micro/micro/v3/service/config" "github.com/micro/micro/v3/service/logger" "github.com/pkg/errors" @@ -16,10 +17,10 @@ import ( type translation struct { ApiKey string + Limit int } 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) @@ -30,8 +31,15 @@ func NewTranslation() *translation { 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{ 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") } - 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, Format: translate.Format(req.Format), 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") } - for _, v := range result { - rsp.Translations = append(rsp.Translations, &pb.BasicTranslation{ - Text: v.Text, - Source: v.Source.String(), - Model: v.Model, - }) + if len(result) == 0 { + return nil + } + + rsp.Translation = &pb.Translation{ + Text: result[0].Text, + Source: result[0].Source.String(), + Model: result[0].Model, } return nil diff --git a/translate/proto/translate.pb.go b/translate/proto/translate.pb.go index 21f5e69..49c1de2 100644 --- a/translate/proto/translate.pb.go +++ b/translate/proto/translate.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.27.1 -// protoc v3.19.1 +// protoc v3.15.6 // source: proto/translate.proto package translate @@ -20,7 +20,7 @@ const ( _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) -type BasicTranslation struct { +type Translation struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields @@ -33,8 +33,8 @@ type BasicTranslation struct { Model string `protobuf:"bytes,3,opt,name=model,proto3" json:"model,omitempty"` } -func (x *BasicTranslation) Reset() { - *x = BasicTranslation{} +func (x *Translation) Reset() { + *x = Translation{} if protoimpl.UnsafeEnabled { mi := &file_proto_translate_proto_msgTypes[0] 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) } -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] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) @@ -60,26 +60,26 @@ func (x *BasicTranslation) ProtoReflect() protoreflect.Message { return mi.MessageOf(x) } -// Deprecated: Use BasicTranslation.ProtoReflect.Descriptor instead. -func (*BasicTranslation) Descriptor() ([]byte, []int) { +// Deprecated: Use Translation.ProtoReflect.Descriptor instead. +func (*Translation) Descriptor() ([]byte, []int) { return file_proto_translate_proto_rawDescGZIP(), []int{0} } -func (x *BasicTranslation) GetText() string { +func (x *Translation) GetText() string { if x != nil { return x.Text } return "" } -func (x *BasicTranslation) GetSource() string { +func (x *Translation) GetSource() string { if x != nil { return x.Source } return "" } -func (x *BasicTranslation) GetModel() string { +func (x *Translation) GetModel() string { if x != nil { return x.Model } @@ -93,7 +93,7 @@ type TextRequest struct { unknownFields protoimpl.UnknownFields // 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 // 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"` @@ -139,11 +139,11 @@ func (*TextRequest) Descriptor() ([]byte, []int) { return file_proto_translate_proto_rawDescGZIP(), []int{1} } -func (x *TextRequest) GetContents() []string { +func (x *TextRequest) GetContent() string { if x != nil { - return x.Contents + return x.Content } - return nil + return "" } func (x *TextRequest) GetSource() string { @@ -180,7 +180,7 @@ type TextResponse struct { sizeCache protoimpl.SizeCache 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() { @@ -215,9 +215,9 @@ func (*TextResponse) Descriptor() ([]byte, []int) { return file_proto_translate_proto_rawDescGZIP(), []int{2} } -func (x *TextResponse) GetTranslations() []*BasicTranslation { +func (x *TextResponse) GetTranslation() *Translation { if x != nil { - return x.Translations + return x.Translation } return nil } @@ -227,32 +227,31 @@ 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, + 0x74, 0x65, 0x22, 0x4f, 0x0a, 0x0b, 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, 0x85, 0x01, 0x0a, 0x0b, 0x54, 0x65, 0x78, 0x74, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 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, 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, 0x48, 0x0a, 0x0c, 0x54, + 0x65, 0x78, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x38, 0x0a, 0x0b, 0x74, + 0x72, 0x61, 0x6e, 0x73, 0x6c, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, + 0x32, 0x16, 0x2e, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x6c, 0x61, 0x74, 0x65, 0x2e, 0x54, 0x72, 0x61, + 0x6e, 0x73, 0x6c, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x0b, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x6c, + 0x61, 0x74, 0x69, 0x6f, 0x6e, 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 ( @@ -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_goTypes = []interface{}{ - (*BasicTranslation)(nil), // 0: translate.BasicTranslation - (*TextRequest)(nil), // 1: translate.TextRequest - (*TextResponse)(nil), // 2: translate.TextResponse + (*Translation)(nil), // 0: translate.Translation + (*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 + 0, // 0: translate.TextResponse.translation:type_name -> translate.Translation 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 @@ -291,7 +290,7 @@ func file_proto_translate_proto_init() { } if !protoimpl.UnsafeEnabled { 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: return &v.state case 1: diff --git a/translate/proto/translate.proto b/translate/proto/translate.proto index 6605081..6cc73cd 100644 --- a/translate/proto/translate.proto +++ b/translate/proto/translate.proto @@ -5,37 +5,37 @@ 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 + 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; +message Translation { + // 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; + // The contents to be translated + string content = 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; + Translation translation = 1; } diff --git a/translate/publicapi.json b/translate/publicapi.json index 788a078..283ed70 100644 --- a/translate/publicapi.json +++ b/translate/publicapi.json @@ -4,6 +4,6 @@ "category": "language", "display_name": "Translate", "pricing": { - "Translate.Text" + "Translate.Text": 10000 } }