Added Go translation server - send requests to Google Translate API
This commit is contained in:
101
golangServer/testing.go
Normal file
101
golangServer/testing.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
translate "cloud.google.com/go/translate/apiv3"
|
||||
"cloud.google.com/go/translate/apiv3/translatepb"
|
||||
)
|
||||
|
||||
const project_id string = "india-translate-testing-452100"
|
||||
|
||||
var lang_codes []string = []string{
|
||||
"hi", // Hindi
|
||||
"bn", // Bengali
|
||||
"mr", // Marathi
|
||||
"ta", // Tamil
|
||||
"te", // Telugu
|
||||
"ml", // Malayalam
|
||||
"kn", // Kannada
|
||||
"gu", // Gujarati
|
||||
"or", // Oriya
|
||||
"ur", // Urdu
|
||||
"lus", // Mizo
|
||||
"as", // Assamese
|
||||
"pa", // Punjabi
|
||||
"mai", // Maithili
|
||||
"mwr", // Marwari
|
||||
"sat", // Santali
|
||||
"ne", // Nepali
|
||||
"gom", // Konkani
|
||||
"tcy", // Tulu
|
||||
"bho", // Bhojpuri
|
||||
"doi", // Dogri
|
||||
"mni-Mtei", // Manipuri
|
||||
"sd", // Sindhi
|
||||
"awa", // Awadhi
|
||||
}
|
||||
|
||||
func translateText(text string, targetLang string) (result string, err error) {
|
||||
return translateTextHelper(project_id, "en-US", targetLang, text)
|
||||
}
|
||||
|
||||
func translateTextHelper(projectID string, sourceLang string, targetLang string, text string) (string, error) {
|
||||
// projectID := "my-project-id"
|
||||
// sourceLang := "en-US"
|
||||
// targetLang := "fr"
|
||||
// text := "Text you wish to translate"
|
||||
|
||||
// Instantiates a client
|
||||
ctx := context.Background()
|
||||
client, err := translate.NewTranslationClient(ctx)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("NewTranslationClient: %w", err)
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
// Construct request
|
||||
req := &translatepb.TranslateTextRequest{
|
||||
Parent: fmt.Sprintf("projects/%s/locations/global", projectID),
|
||||
SourceLanguageCode: sourceLang,
|
||||
TargetLanguageCode: targetLang,
|
||||
MimeType: "text/plain", // Mime types: "text/plain", "text/html"
|
||||
Contents: []string{text},
|
||||
}
|
||||
|
||||
resp, err := client.TranslateText(ctx, req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("TranslateText: %w", err)
|
||||
}
|
||||
|
||||
// Display the translation for each input text provided
|
||||
return resp.GetTranslations()[0].GetTranslatedText(), nil
|
||||
}
|
||||
|
||||
func handler(w http.ResponseWriter, r *http.Request) {
|
||||
queries := r.URL.Query()
|
||||
toTranslate := queries["query"][0]
|
||||
|
||||
langToTranslation := make(map[string]string)
|
||||
for _, lang_code := range lang_codes {
|
||||
translation, err := translateText(toTranslate, lang_code)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
langToTranslation[lang_code] = translation
|
||||
}
|
||||
|
||||
langToTranslationJson, _ := json.Marshal(langToTranslation)
|
||||
fmt.Fprintf(w, "%v", string(langToTranslationJson))
|
||||
|
||||
// fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", handler)
|
||||
log.Fatal(http.ListenAndServe(":9090", nil))
|
||||
}
|
Reference in New Issue
Block a user