Skip to content

Commit

Permalink
delete messages add translations
Browse files Browse the repository at this point in the history
  • Loading branch information
samarets committed Oct 2, 2022
1 parent 3964109 commit fcaf3b2
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 80 deletions.
Empty file added Makefile
Empty file.
12 changes: 11 additions & 1 deletion cmd/bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/samarets/support-bot/internal/bot"
"github.com/samarets/support-bot/internal/db"
"github.com/samarets/support-bot/internal/log"
"github.com/samarets/support-bot/internal/translations"
)

func main() {
Expand All @@ -14,20 +15,29 @@ func main() {
panic(err)
}

translator, err := translations.NewTranslations(cfg.DefaultLocale, "./locales")
if err != nil {
log.Error().Err(err).Send()
panic(err)
}

database, err := db.InitDB()
if err != nil {
log.Error().Err(err).Send()
panic(err)
}

defer func(database *db.DB) {
err := database.Close()
if err != nil {
log.Error().Err(err).Send()
panic(err)
}
}(database)

err = bot.InitBot(cfg.TelegramLoggerBotToken, cfg.DefaultLocale, cfg.BotPrefix, database)
err = bot.InitBot(cfg.TelegramLoggerBotToken, translator, database)
if err != nil {
log.Error().Err(err).Send()
panic(err)
}
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ module github.com/samarets/support-bot
go 1.18

require (
github.com/BurntSushi/toml v1.2.0
github.com/dgraph-io/badger/v3 v3.2103.2
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/nicksnyder/go-i18n/v2 v2.2.0
github.com/rs/zerolog v1.27.0
github.com/spf13/viper v1.12.0
golang.org/x/text v0.3.7
)

require (
Expand Down Expand Up @@ -38,7 +41,6 @@ require (
go.opencensus.io v0.23.0 // indirect
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU=
github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
Expand Down Expand Up @@ -184,6 +188,8 @@ github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/nicksnyder/go-i18n/v2 v2.2.0 h1:MNXbyPvd141JJqlU6gJKrczThxJy+kdCNivxZpBQFkw=
github.com/nicksnyder/go-i18n/v2 v2.2.0/go.mod h1:4OtLfzqyAxsscyCb//3gfqSvBc81gImX91LrZzczN1o=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
Expand Down Expand Up @@ -544,6 +550,7 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.66.4 h1:SsAcf+mM7mRZo2nJNGt8mZCjG8ZRaNGMURJw7BsIST4=
gopkg.in/ini.v1 v1.66.4/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
12 changes: 4 additions & 8 deletions internal/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@ import (

"github.com/dgraph-io/badger/v3"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/samarets/support-bot/internal/translations"

"github.com/samarets/support-bot/internal/db"
"github.com/samarets/support-bot/internal/log"
"github.com/samarets/support-bot/internal/messages"
)

type bot struct {
bot *tgbotapi.BotAPI
db *botDB
mh *messages.Helper
tl *translations.Translator
}

func InitBot(token, defaultLocale, botPrefix string, db *db.DB) error {
mh, err := messages.NewMessagesHelper(defaultLocale, botPrefix)
if err != nil {
return err
}
func InitBot(token string, translator *translations.Translator, db *db.DB) error {

botAPI, err := tgbotapi.NewBotAPI(token)
if err != nil {
Expand All @@ -33,7 +29,7 @@ func InitBot(token, defaultLocale, botPrefix string, db *db.DB) error {
botState := &bot{
bot: botAPI,
db: dbState,
mh: mh,
tl: translator,
}
botState.InitUpdates()

Expand Down
23 changes: 19 additions & 4 deletions internal/bot/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/samarets/support-bot/internal/log"
"github.com/samarets/support-bot/internal/messages"
)

const (
Expand All @@ -20,12 +19,21 @@ func (b *bot) StartCommand(update tgbotapi.Update) {
return
}

helloMessage, err := b.tl.NewLocalize(update.SentFrom().LanguageCode, "hello").
AddMessage("🤖 Привіт {{.Name}}, напиши своє питання - ми допоможемо").
AddTemplate("Name", update.SentFrom().FirstName).
Localize()
if err != nil {
log.Error().Err(err).Send()
return
}

msg := tgbotapi.NewMessage(
update.Message.Chat.ID,
b.mh.GetMessage(messages.HelloMessage, update.SentFrom().LanguageCode),
helloMessage,
)

_, err := b.bot.Send(msg)
_, err = b.bot.Send(msg)
if err != nil {
log.Error().Err(err).Send()
}
Expand Down Expand Up @@ -78,7 +86,14 @@ func (b *bot) ConnectCommand(update tgbotapi.Update) {
return
}

msg := tgbotapi.NewMessage(user.ID, b.mh.GetMessage(messages.OperatorConnected, update.SentFrom().LanguageCode))
operatorConnectedMessage, err := b.tl.NewLocalize(update.SentFrom().LanguageCode, "hello").
AddMessage("🤖 До вас доєднався оператор!!!").
Localize()
if err != nil {
log.Error().Err(err).Send()
}

msg := tgbotapi.NewMessage(user.ID, operatorConnectedMessage)
_, err = b.bot.Send(msg)
if err != nil {
log.Error().Err(err).Send()
Expand Down
28 changes: 0 additions & 28 deletions internal/messages/helper.go

This file was deleted.

19 changes: 0 additions & 19 deletions internal/messages/locales.go

This file was deleted.

19 changes: 0 additions & 19 deletions internal/messages/messages.go

This file was deleted.

90 changes: 90 additions & 0 deletions internal/translations/translation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package translations

import (
"os"
"strings"

"github.com/BurntSushi/toml"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
)

type Translator struct {
bundle *i18n.Bundle
}

func NewTranslations(defaultLanguage, languageDirPath string) (*Translator, error) {
tag, err := language.Parse(defaultLanguage)
if err != nil {
return nil, err
}

bundle := i18n.NewBundle(tag)
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
files, err := os.ReadDir(languageDirPath)
if err != nil {
return nil, err
}

for _, file := range files {
if file.IsDir() {
continue
}

fileSepName := strings.Split(file.Name(), ".")
if len(fileSepName) < 3 {
continue
}
if fileSepName[0] != "active" {
continue
}

bundle.MustLoadMessageFile(languageDirPath + "/" + file.Name())
}

return &Translator{
bundle: bundle,
}, nil
}

type LocalizeBuilder struct {
loc *i18n.Localizer
message *i18n.Message
templateData map[string]string
}

func (t *Translator) NewLocalize(lang, id string) *LocalizeBuilder {
return &LocalizeBuilder{
loc: i18n.NewLocalizer(t.bundle, lang),
message: &i18n.Message{
ID: id,
},
templateData: make(map[string]string),
}
}

func (lb *LocalizeBuilder) AddMessage(message string) *LocalizeBuilder {
lb.message.Other = message
return lb
}

func (lb *LocalizeBuilder) AddTemplate(templateID, message string) *LocalizeBuilder {
lb.templateData[templateID] = message
return lb
}

func (lb *LocalizeBuilder) Localize() (string, error) {
localizeConfig := &i18n.LocalizeConfig{
DefaultMessage: lb.message,
}
if len(lb.templateData) > 0 {
localizeConfig.TemplateData = lb.templateData
}

message, err := lb.loc.Localize(localizeConfig)
if err != nil {
return "", err
}

return message, nil
}

0 comments on commit fcaf3b2

Please sign in to comment.