There are now 2 translators!

This commit is contained in:
Daniel Goc
2026-03-16 11:36:06 +01:00
parent 7aa16c644f
commit 0da596826e
7 changed files with 353 additions and 169 deletions

View File

@@ -17,9 +17,10 @@ import (
"git.ma-al.com/goc_daniel/b2b/app/model"
"git.ma-al.com/goc_daniel/b2b/app/service/langsService"
"git.ma-al.com/goc_daniel/b2b/app/utils/responseErrors"
"github.com/openai/openai-go/responses"
"github.com/openai/openai-go/v3"
"google.golang.org/api/option"
"github.com/openai/openai-go/v3/option"
"github.com/openai/openai-go/v3/responses"
googleopt "google.golang.org/api/option"
"gorm.io/gorm"
// [START translate_v3_import_client_library]
@@ -70,7 +71,7 @@ func New() *ProductDescriptionService {
log.Fatalf("productDescriptionService: cannot build Google credentials: %v", err)
}
googleCli, err := translate.NewTranslationClient(ctx, option.WithAuthCredentials(creds))
googleCli, err := translate.NewTranslationClient(ctx, googleopt.WithAuthCredentials(creds))
if err != nil {
log.Fatalf("productDescriptionService: cannot create Translation client: %v", err)
}
@@ -161,7 +162,7 @@ func (s *ProductDescriptionService) SaveProductDescription(userID uint, productI
//
// The Google Cloud project must have the Cloud Translation API enabled and the
// service account must hold the "Cloud Translation API User" role.
func (s *ProductDescriptionService) TranslateProductDescription(userID uint, productID uint, productShopID uint, productFromLangID uint, productToLangID uint, model string) (*model.ProductDescription, error) {
func (s *ProductDescriptionService) TranslateProductDescription(userID uint, productID uint, productShopID uint, productFromLangID uint, productToLangID uint, aiModel string) (*model.ProductDescription, error) {
var ProductDescription model.ProductDescription
err := s.db.
@@ -199,7 +200,7 @@ func (s *ProductDescriptionService) TranslateProductDescription(userID uint, pro
}
request := ""
if model == "OpenAI" {
if aiModel == "OpenAI" {
request = "Translate to " + lang.ISOCode + " without changing the html structure.\n"
}
for i := 0; i < len(keys); i++ {
@@ -207,11 +208,11 @@ func (s *ProductDescriptionService) TranslateProductDescription(userID uint, pro
request += *fields[i]
request += "</" + keys[i] + ">\n"
}
if model == "OpenAI" {
if aiModel == "OpenAI" {
request = cleanForPrompt(request)
}
if model == "OpenAI" {
if aiModel == "OpenAI" {
openai_response, _ := s.client.Responses.New(context.Background(), responses.ResponseNewParams{
Input: responses.ResponseNewParamsInputUnion{OfString: openai.String(request)},
Model: openai.ChatModelGPT4_1Mini,
@@ -228,9 +229,11 @@ func (s *ProductDescriptionService) TranslateProductDescription(userID uint, pro
return nil, responseErrors.ErrAIBadOutput
}
*fields[i] = resolution
fmt.Println(resolution)
}
} else if model == "Google" {
} else if aiModel == "Google" {
// TranslateText is the standard Cloud Translation v3 endpoint.
req := &translatepb.TranslateTextRequest{
Parent: fmt.Sprintf("projects/%s/locations/global", s.projectID),
@@ -255,6 +258,8 @@ func (s *ProductDescriptionService) TranslateProductDescription(userID uint, pro
return nil, responseErrors.ErrAIBadOutput
}
*fields[i] = match
fmt.Println(match)
}
}