From 16922ef0fc1c0d6069fbab08661829ad57975347 Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Fri, 21 Sep 2018 12:48:42 +0300 Subject: [PATCH] add bots localization --- src/locale.go | 1 + src/models.go | 1 + src/routing.go | 37 ++++++++++++++++++++++++++++++------- src/run.go | 1 + static/script.js | 24 ++++++++++++++++++++++++ static/style.css | 9 +++++++++ templates/form.html | 12 ++++++++++++ templates/layout.html | 4 ++-- translate/translate.en.yml | 1 + translate/translate.es.yml | 1 + translate/translate.ru.yml | 1 + 11 files changed, 83 insertions(+), 9 deletions(-) diff --git a/src/locale.go b/src/locale.go index 8945c9d..cf275c1 100644 --- a/src/locale.go +++ b/src/locale.go @@ -59,6 +59,7 @@ func getLocale() map[string]interface{} { "AddBot": getLocalizedMessage("add_bot"), "TableDelete": getLocalizedMessage("table_delete"), "Title": getLocalizedMessage("title"), + "Language": getLocalizedMessage("language"), "InfoBot": template.HTML(getLocalizedMessage("info_bot")), "CRMLink": template.HTML(getLocalizedMessage("crm_link")), "DocLink": template.HTML(getLocalizedMessage("doc_link")), diff --git a/src/models.go b/src/models.go index 4d9622f..b7f5310 100644 --- a/src/models.go +++ b/src/models.go @@ -24,6 +24,7 @@ type Bot struct { ChannelSettingsHash string `gorm:"channel_settings_hash type:varchar(70)"` Token string `gorm:"token type:varchar(100);not null;unique" json:"token,omitempty"` Name string `gorm:"name type:varchar(40)" json:"name,omitempty"` + Lang string `gorm:"lang type:varchar(2)" json:"lang,omitempty"` CreatedAt time.Time UpdatedAt time.Time } diff --git a/src/routing.go b/src/routing.go index cf9cfb3..2fa7f5b 100644 --- a/src/routing.go +++ b/src/routing.go @@ -117,15 +117,17 @@ func settingsHandler(c *gin.Context) { bots := p.getBotsByClientID() res := struct { - Conn *Connection - Bots Bots - Locale map[string]interface{} - Year int + Conn *Connection + Bots Bots + Locale map[string]interface{} + Year int + LangCode []string }{ p, bots, getLocale(), time.Now().Year(), + []string{"en", "ru", "es"}, } c.HTML(http.StatusOK, "form", &res) @@ -246,6 +248,25 @@ func activityHandler(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"success": true}) } +func setLangBotHandler(c *gin.Context) { + b := c.MustGet("bot").(Bot) + cl, err := getBotByToken(b.Token) + if err != nil { + c.Error(err) + return + } + + cl.Lang = b.Lang + + err = cl.save() + if err != nil { + c.Error(err) + return + } + + c.JSON(http.StatusOK, gin.H{}) +} + func getIntegrationModule(clientId string) v5.IntegrationModule { return v5.IntegrationModule{ Code: config.TransportInfo.Code, @@ -559,6 +580,8 @@ func mgWebhookHandler(c *gin.Context) { return } + setLocale(b.Lang) + switch msg.Type { case "message_sent": var mb string @@ -568,13 +591,13 @@ func mgWebhookHandler(c *gin.Context) { if msg.Data.Product.Cost != nil && msg.Data.Product.Cost.Value != 0 { mb += fmt.Sprintf( - "\n%s: %s", + "\n%s: %s\n", getLocalizedMessage("item_cost"), getLocalizedTemplateMessage( "cost_currency", map[string]interface{}{ - "CostValue": msg.Data.Product.Cost.Value, - "CostCurrency": currency[strings.ToLower(msg.Data.Product.Cost.Currency)], + "Amount": msg.Data.Product.Cost.Value, + "Currency": currency[strings.ToLower(msg.Data.Product.Cost.Currency)], }, ), ) diff --git a/src/run.go b/src/run.go index 577769a..4e8e790 100644 --- a/src/run.go +++ b/src/run.go @@ -90,6 +90,7 @@ func setup() *gin.Engine { r.POST("/create/", checkConnectionForRequest(), createHandler) r.POST("/add-bot/", checkBotForRequest(), addBotHandler) r.POST("/delete-bot/", checkBotForRequest(), deleteBotHandler) + r.POST("/set-lang/", checkBotForRequest(), setLangBotHandler) r.POST("/actions/activity", activityHandler) r.POST("/telegram/:token", telegramWebhookHandler) r.POST("/webhook/", mgWebhookHandler) diff --git a/static/script.js b/static/script.js index 4acb1ea..a2c4cef 100644 --- a/static/script.js +++ b/static/script.js @@ -1,3 +1,16 @@ +$(document).on("change", "select", function(e) { + send( + "/set-lang/", + { + token: $(this).attr("data-token"), + lang: $(this).find(":selected").text() + }, + function () { + return 0; + } + ) +}); + $('#save-crm').on("submit", function(e) { e.preventDefault(); send( @@ -39,6 +52,7 @@ $("#add-bot").on("submit", function(e) { } $("#bots tbody").append(getBotTemplate(data)); $("#token").val(""); + $('select').formSelect(); } ) }); @@ -101,6 +115,15 @@ function getBotTemplate(data) { ` ${data.name} ${data.token} + +
+ +
+