|
|
|
@ -2,17 +2,49 @@ package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"database/sql"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
|
|
|
|
|
|
translate "cloud.google.com/go/translate/apiv3"
|
|
|
|
|
"cloud.google.com/go/translate/apiv3/translatepb"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const project_id string = "india-translate-testing-452100"
|
|
|
|
|
|
|
|
|
|
type translationStruct struct {
|
|
|
|
|
En string `db:"english" json:"en"`
|
|
|
|
|
Hi string `db:"hindi" json:"hi"`
|
|
|
|
|
Bn string `db:"bengali" json:"bn"`
|
|
|
|
|
Mr string `db:"marathi" json:"mr"`
|
|
|
|
|
Ta string `db:"tamil" json:"ta"`
|
|
|
|
|
Te string `db:"telugu" json:"te"`
|
|
|
|
|
Ml string `db:"malayalam" json:"ml"`
|
|
|
|
|
Kn string `db:"kannada" json:"kn"`
|
|
|
|
|
Gu string `db:"gujarati" json:"gu"`
|
|
|
|
|
Or string `db:"oriya" json:"or"`
|
|
|
|
|
Ur string `db:"urdu" json:"ur"`
|
|
|
|
|
Lus string `db:"mizo" json:"lus"`
|
|
|
|
|
As string `db:"assamese" json:"as"`
|
|
|
|
|
Pa string `db:"punjabi" json:"pa"`
|
|
|
|
|
Mai string `db:"maithili" json:"nai"`
|
|
|
|
|
Mwr string `db:"marwari" json:"mwr"`
|
|
|
|
|
Sat string `db:"santali" json:"sat"`
|
|
|
|
|
Ne string `db:"nepali" json:"ne"`
|
|
|
|
|
Gom string `db:"konkani" json:"gom"`
|
|
|
|
|
Tcy string `db:"tulu" json:"tcy"`
|
|
|
|
|
Bho string `db:"bhojpuri" json:"bho"`
|
|
|
|
|
Doi string `db:"dogri" json:"doi"`
|
|
|
|
|
Mni_mtei string `db:"manipuri" json:"mni_mtei"`
|
|
|
|
|
Sd string `db:"sindhi" json:"sd"`
|
|
|
|
|
Awa string `db:"awadhi" json:"awa"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var lang_codes []string = []string{
|
|
|
|
|
"hi", // Hindi
|
|
|
|
|
"bn", // Bengali
|
|
|
|
@ -40,6 +72,39 @@ var lang_codes []string = []string{
|
|
|
|
|
"awa", // Awadhi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var db *sqlx.DB
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Returns the cached translation from the database, for the given english text. The first parameter
|
|
|
|
|
|
|
|
|
|
indicates whether or not the translation exists.
|
|
|
|
|
*/
|
|
|
|
|
func getCachedTranslation(data string) (bool, translationStruct) {
|
|
|
|
|
prepared, err := db.Preparex("SELECT * from TRANSLATIONS WHERE english = ? COLLATE NOCASE")
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
translations := translationStruct{}
|
|
|
|
|
err = prepared.Get(&translations, data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
|
return false, translations
|
|
|
|
|
} else {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return true, translations
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func addToDatabase(translation translationStruct) {
|
|
|
|
|
_, err := db.NamedExec(`INSERT INTO translations VALUES (:english, :hindi, :bengali, :marathi, :tamil, :telugu, :kannada, :malayalam, :oriya, :gujarati, :marwari, :urdu, :mizo, :assamese, :punjabi, :maithili, :santali, :nepali, :konkani, :tulu, :bhojpuri, :dogri, :manipuri, :sindhi, :awadhi)`, &translation)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func translateText(text string, targetLang string) (result string, err error) {
|
|
|
|
|
return translateTextHelper(project_id, "en-US", targetLang, text)
|
|
|
|
|
}
|
|
|
|
@ -80,22 +145,39 @@ 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 ok, translation := getCachedTranslation(toTranslate); ok {
|
|
|
|
|
translationJson, _ := json.Marshal(translation)
|
|
|
|
|
fmt.Fprintf(w, "%v", string(translationJson))
|
|
|
|
|
} else {
|
|
|
|
|
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)
|
|
|
|
|
translation := translationStruct{}
|
|
|
|
|
err := json.Unmarshal(langToTranslationJson, &translation)
|
|
|
|
|
translation.En = toTranslate // langToTranslation doesn't contain the english value
|
|
|
|
|
addToDatabase(translation)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
langToTranslation[lang_code] = translation
|
|
|
|
|
fmt.Fprintf(w, "%v", string(langToTranslationJson))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
langToTranslationJson, _ := json.Marshal(langToTranslation)
|
|
|
|
|
fmt.Fprintf(w, "%v", string(langToTranslationJson))
|
|
|
|
|
|
|
|
|
|
// fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
fmt.Printf("Starting server...")
|
|
|
|
|
var err error
|
|
|
|
|
db, err = sqlx.Connect("sqlite3", "translations.db")
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
|
|
http.HandleFunc("/", handler)
|
|
|
|
|
log.Fatal(http.ListenAndServe(":9090", nil))
|
|
|
|
|
}
|
|
|
|
|