1// Copyright 2021 Google LLC. 2// Use of this source code is governed by a BSD-style 3// license that can be found in the LICENSE file. 4 5// Package fake demonstrates how to use concrete services and fake the 6// interactions with them in tests. 7package fake 8 9import ( 10 "fmt" 11 "os" 12 13 "google.golang.org/api/translate/v3" 14) 15 16// TranslateText translates text to the given language using the provided 17// service. 18func TranslateText(service *translate.Service, text, language string) (string, error) { 19 parent := fmt.Sprintf("projects/%s/locations/global", os.Getenv("GOOGLE_CLOUD_PROJECT")) 20 req := &translate.TranslateTextRequest{ 21 TargetLanguageCode: language, 22 Contents: []string{text}, 23 } 24 resp, err := service.Projects.Locations.TranslateText(parent, req).Do() 25 if err != nil { 26 return "", fmt.Errorf("unable to translate text: %v", err) 27 } 28 return resp.Translations[0].TranslatedText, nil 29} 30