1
0
mirror of synced 2024-11-28 22:46:02 +03:00

add bots localization

This commit is contained in:
DmitryZagorulko 2018-09-21 12:48:42 +03:00
parent 9d6ebb540f
commit 16922ef0fc
11 changed files with 83 additions and 9 deletions

View File

@ -59,6 +59,7 @@ func getLocale() map[string]interface{} {
"AddBot": getLocalizedMessage("add_bot"), "AddBot": getLocalizedMessage("add_bot"),
"TableDelete": getLocalizedMessage("table_delete"), "TableDelete": getLocalizedMessage("table_delete"),
"Title": getLocalizedMessage("title"), "Title": getLocalizedMessage("title"),
"Language": getLocalizedMessage("language"),
"InfoBot": template.HTML(getLocalizedMessage("info_bot")), "InfoBot": template.HTML(getLocalizedMessage("info_bot")),
"CRMLink": template.HTML(getLocalizedMessage("crm_link")), "CRMLink": template.HTML(getLocalizedMessage("crm_link")),
"DocLink": template.HTML(getLocalizedMessage("doc_link")), "DocLink": template.HTML(getLocalizedMessage("doc_link")),

View File

@ -24,6 +24,7 @@ type Bot struct {
ChannelSettingsHash string `gorm:"channel_settings_hash type:varchar(70)"` ChannelSettingsHash string `gorm:"channel_settings_hash type:varchar(70)"`
Token string `gorm:"token type:varchar(100);not null;unique" json:"token,omitempty"` Token string `gorm:"token type:varchar(100);not null;unique" json:"token,omitempty"`
Name string `gorm:"name type:varchar(40)" json:"name,omitempty"` Name string `gorm:"name type:varchar(40)" json:"name,omitempty"`
Lang string `gorm:"lang type:varchar(2)" json:"lang,omitempty"`
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time
} }

View File

@ -121,11 +121,13 @@ func settingsHandler(c *gin.Context) {
Bots Bots Bots Bots
Locale map[string]interface{} Locale map[string]interface{}
Year int Year int
LangCode []string
}{ }{
p, p,
bots, bots,
getLocale(), getLocale(),
time.Now().Year(), time.Now().Year(),
[]string{"en", "ru", "es"},
} }
c.HTML(http.StatusOK, "form", &res) c.HTML(http.StatusOK, "form", &res)
@ -246,6 +248,25 @@ func activityHandler(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"success": true}) 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 { func getIntegrationModule(clientId string) v5.IntegrationModule {
return v5.IntegrationModule{ return v5.IntegrationModule{
Code: config.TransportInfo.Code, Code: config.TransportInfo.Code,
@ -559,6 +580,8 @@ func mgWebhookHandler(c *gin.Context) {
return return
} }
setLocale(b.Lang)
switch msg.Type { switch msg.Type {
case "message_sent": case "message_sent":
var mb string var mb string
@ -568,13 +591,13 @@ func mgWebhookHandler(c *gin.Context) {
if msg.Data.Product.Cost != nil && msg.Data.Product.Cost.Value != 0 { if msg.Data.Product.Cost != nil && msg.Data.Product.Cost.Value != 0 {
mb += fmt.Sprintf( mb += fmt.Sprintf(
"\n%s: %s", "\n%s: %s\n",
getLocalizedMessage("item_cost"), getLocalizedMessage("item_cost"),
getLocalizedTemplateMessage( getLocalizedTemplateMessage(
"cost_currency", "cost_currency",
map[string]interface{}{ map[string]interface{}{
"CostValue": msg.Data.Product.Cost.Value, "Amount": msg.Data.Product.Cost.Value,
"CostCurrency": currency[strings.ToLower(msg.Data.Product.Cost.Currency)], "Currency": currency[strings.ToLower(msg.Data.Product.Cost.Currency)],
}, },
), ),
) )

View File

@ -90,6 +90,7 @@ func setup() *gin.Engine {
r.POST("/create/", checkConnectionForRequest(), createHandler) r.POST("/create/", checkConnectionForRequest(), createHandler)
r.POST("/add-bot/", checkBotForRequest(), addBotHandler) r.POST("/add-bot/", checkBotForRequest(), addBotHandler)
r.POST("/delete-bot/", checkBotForRequest(), deleteBotHandler) r.POST("/delete-bot/", checkBotForRequest(), deleteBotHandler)
r.POST("/set-lang/", checkBotForRequest(), setLangBotHandler)
r.POST("/actions/activity", activityHandler) r.POST("/actions/activity", activityHandler)
r.POST("/telegram/:token", telegramWebhookHandler) r.POST("/telegram/:token", telegramWebhookHandler)
r.POST("/webhook/", mgWebhookHandler) r.POST("/webhook/", mgWebhookHandler)

View File

@ -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) { $('#save-crm').on("submit", function(e) {
e.preventDefault(); e.preventDefault();
send( send(
@ -39,6 +52,7 @@ $("#add-bot").on("submit", function(e) {
} }
$("#bots tbody").append(getBotTemplate(data)); $("#bots tbody").append(getBotTemplate(data));
$("#token").val(""); $("#token").val("");
$('select').formSelect();
} }
) )
}); });
@ -101,6 +115,15 @@ function getBotTemplate(data) {
`<tr> `<tr>
<td>${data.name}</td> <td>${data.name}</td>
<td>${data.token}</td> <td>${data.token}</td>
<td>
<div class="col s3 sel-lang">
<select data-token="${data.token}">
<option value="en" selected>en</option>
<option value="ru">ru</option>
<option value="es">es</option>
</select>
</div>
</td>
<td> <td>
<button class="delete-bot btn btn-small waves-effect waves-light light-blue darken-1" type="submit" name="action" <button class="delete-bot btn btn-small waves-effect waves-light light-blue darken-1" type="submit" name="action"
data-token="${data.token}"> data-token="${data.token}">
@ -120,6 +143,7 @@ function formDataToObj(formArray) {
} }
$( document ).ready(function() { $( document ).ready(function() {
$('select').formSelect();
M.Tabs.init(document.getElementById("tab")); M.Tabs.init(document.getElementById("tab"));
if ($("table tbody").children().length === 0) { if ($("table tbody").children().length === 0) {
$("#bots").addClass("hide"); $("#bots").addClass("hide");

View File

@ -32,10 +32,19 @@ main {
float: right; float: right;
} }
#bots .select-wrapper input.select-dropdown,
#bots { #bots {
font-size: 12px; font-size: 12px;
} }
#bots .sel-lang{
padding: 0;
}
#bots .select-wrapper ul li span{
color: #039be5;
}
#msg{ #msg{
height: 23px; height: 23px;
} }

View File

@ -51,19 +51,31 @@
</div> </div>
</div> </div>
</form> </form>
{{$LangCode := .LangCode}}
<table id="bots" class="tab-el-center"> <table id="bots" class="tab-el-center">
<thead> <thead>
<tr> <tr>
<th>{{.Locale.TableName}}</th> <th>{{.Locale.TableName}}</th>
<th>{{.Locale.TableToken}}</th> <th>{{.Locale.TableToken}}</th>
<th>{{.Locale.Language}}</th>
<th class="text-left">{{.Locale.TableDelete}}</th> <th class="text-left">{{.Locale.TableDelete}}</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{{range .Bots}} {{range .Bots}}
{{$lang := .Lang}}
<tr> <tr>
<td>{{.Name}}</td> <td>{{.Name}}</td>
<td>{{.Token}}</td> <td>{{.Token}}</td>
<td>
<div class="col s3 sel-lang">
<select data-token="{{.Token}}">
{{range $key, $value := $LangCode}}
<option value="{{$value}}" {{if eq $value $lang}}selected{{end}}>{{$value}}</option>
{{end}}
</select>
</div>
</td>
<td> <td>
<button class="delete-bot btn btn-small waves-effect waves-light light-blue darken-1" type="submit" name="action" <button class="delete-bot btn btn-small waves-effect waves-light light-blue darken-1" type="submit" name="action"
data-token="{{.Token}}"> data-token="{{.Token}}">

View File

@ -3,9 +3,9 @@
<meta title="Telegram transport"> <meta title="Telegram transport">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>{{.Locale.Title}}</title> <title>{{.Locale.Title}}</title>
<link rel="stylesheet" href="/static/materialize.min.css">
<link rel="stylesheet" href="/static/font.css" > <link rel="stylesheet" href="/static/font.css" >
<link rel="stylesheet" href="/static/jquery-confirm.min.css"> <link rel="stylesheet" href="/static/jquery-confirm.min.css">
<link rel="stylesheet" href="/static/materialize.min.css">
<link rel="stylesheet" href="/static/style.css" > <link rel="stylesheet" href="/static/style.css" >
</head> </head>
<body> <body>
@ -29,8 +29,8 @@
</div> </div>
</div> </div>
</footer> </footer>
<script src="/static/materialize.min.js"></script>
<script src="/static/jquery-3.3.1.min.js"></script> <script src="/static/jquery-3.3.1.min.js"></script>
<script src="/static/materialize.min.js"></script>
<script src="/static/jquery-confirm.min.js"></script> <script src="/static/jquery-confirm.min.js"></script>
<script src="/static/script.js"></script> <script src="/static/script.js"></script>
</body> </body>

View File

@ -8,6 +8,7 @@ api_key: API key
add_bot: Add a bot add_bot: Add a bot
title: Module of connecting Telegram to retailCRM title: Module of connecting Telegram to retailCRM
successful: Data was updated successfully successful: Data was updated successfully
language: Language
no_bot_token: Enter a token no_bot_token: Enter a token
wrong_data: Wrong data wrong_data: Wrong data

View File

@ -8,6 +8,7 @@ api_key: API key
add_bot: Añadir un bot add_bot: Añadir un bot
title: Múdulo de conexión de Telegram a retailCRM title: Múdulo de conexión de Telegram a retailCRM
successful: Datos actualizados con éxito successful: Datos actualizados con éxito
language: Idioma
no_bot_token: Introduzca un token no_bot_token: Introduzca un token
wrong_data: Datos erróneos wrong_data: Datos erróneos

View File

@ -8,6 +8,7 @@ api_key: API Ключ
add_bot: Добавить бота add_bot: Добавить бота
title: Модуль подключения Telegram к retailCRM title: Модуль подключения Telegram к retailCRM
successful: Данные успешно обновлены successful: Данные успешно обновлены
language: Язык
no_bot_token: Введите токен no_bot_token: Введите токен
wrong_data: Неверные данные wrong_data: Неверные данные