Archived
Template
1
0

Added multilanguage support

This commit is contained in:
uberswe
2022-01-29 10:21:05 +01:00
parent 3bbc858721
commit 1987a0b0ec
49 changed files with 2402 additions and 154 deletions

39
lang/main.go Normal file
View File

@ -0,0 +1,39 @@
package lang
import (
"github.com/gin-gonic/gin"
"github.com/nicksnyder/go-i18n/v2/i18n"
)
type Service struct {
bundle *i18n.Bundle
ctx *gin.Context
localizer *i18n.Localizer
}
func New(ctx *gin.Context, bundle *i18n.Bundle) Service {
localizer := i18n.NewLocalizer(bundle, ctx.Request.Header.Get("Accept-Language"), "en")
return Service{
bundle: bundle,
ctx: ctx,
localizer: localizer,
}
}
func (s *Service) Trans(str string) string {
// TODO, modify this to handle plural and more types of phrases
for _, m := range translationMessages {
if m.ID == str {
localizedString, _ := s.localizer.Localize(&i18n.LocalizeConfig{
DefaultMessage: &m,
})
return localizedString
} else if m.Other == str {
localizedString, _ := s.localizer.Localize(&i18n.LocalizeConfig{
DefaultMessage: &m,
})
return localizedString
}
}
return str
}