2019-09-04 15:22:27 +03:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
2021-07-29 11:29:31 +03:00
|
|
|
"io/fs"
|
2024-06-14 18:10:48 +03:00
|
|
|
"os"
|
2019-09-04 15:22:27 +03:00
|
|
|
"path"
|
2020-05-18 13:37:24 +03:00
|
|
|
"sync"
|
2019-09-04 15:22:27 +03:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
|
|
|
"golang.org/x/text/language"
|
|
|
|
"gopkg.in/yaml.v2"
|
2021-12-01 15:40:23 +03:00
|
|
|
|
|
|
|
"github.com/retailcrm/mg-transport-core/v2/core/util/errorutil"
|
2019-09-04 15:22:27 +03:00
|
|
|
)
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// DefaultLanguages for transports.
|
2020-05-18 13:37:24 +03:00
|
|
|
var DefaultLanguages = []language.Tag{
|
|
|
|
language.English,
|
|
|
|
language.Russian,
|
|
|
|
language.Spanish,
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// DefaultLanguage is a base language which will be chosen if current language is unspecified.
|
2020-05-18 13:37:24 +03:00
|
|
|
var DefaultLanguage = language.English
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// LocalizerContextKey is a key which is used to store localizer in gin.Context key-value storage.
|
2020-05-18 13:37:24 +03:00
|
|
|
const LocalizerContextKey = "localizer"
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// Localizer struct.
|
2019-09-04 15:22:27 +03:00
|
|
|
type Localizer struct {
|
2020-05-18 13:37:24 +03:00
|
|
|
i18nStorage *sync.Map
|
2021-07-29 11:29:31 +03:00
|
|
|
TranslationsFS fs.FS
|
2020-05-18 13:37:24 +03:00
|
|
|
loadMutex *sync.RWMutex
|
2019-09-04 15:22:27 +03:00
|
|
|
LocaleMatcher language.Matcher
|
|
|
|
LanguageTag language.Tag
|
|
|
|
TranslationsPath string
|
|
|
|
}
|
|
|
|
|
2022-04-22 09:37:16 +03:00
|
|
|
// LocalizerInterface contains entire public interface of the localizer component.
|
2022-03-28 16:51:41 +03:00
|
|
|
type LocalizerInterface interface {
|
2022-04-22 09:37:16 +03:00
|
|
|
MessageLocalizer
|
|
|
|
LocalizerWithMiddleware
|
|
|
|
LocalizerWithFuncs
|
|
|
|
LocaleControls
|
|
|
|
HTTPResponseLocalizer
|
|
|
|
CloneableLocalizer
|
|
|
|
}
|
|
|
|
|
|
|
|
// MessageLocalizer can localize regular strings and strings with template parameters.
|
|
|
|
type MessageLocalizer interface {
|
|
|
|
GetLocalizedMessage(string) string
|
|
|
|
GetLocalizedTemplateMessage(string, map[string]interface{}) string
|
|
|
|
Localize(string) (string, error)
|
|
|
|
LocalizeTemplateMessage(string, map[string]interface{}) (string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocalizerWithMiddleware can provide middlewares for usage in the app.
|
|
|
|
type LocalizerWithMiddleware interface {
|
|
|
|
LocalizationMiddleware() gin.HandlerFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocalizerWithFuncs can provide template functions.
|
|
|
|
type LocalizerWithFuncs interface {
|
|
|
|
LocalizationFuncMap() template.FuncMap
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocaleControls is an instance of localizer with exposed locale controls.
|
|
|
|
type LocaleControls interface {
|
|
|
|
Preload([]language.Tag)
|
|
|
|
SetLocale(string)
|
|
|
|
SetLanguage(language.Tag)
|
|
|
|
Language() language.Tag
|
|
|
|
LoadTranslations()
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPResponseLocalizer can localize strings and return them with HTTP error codes.
|
|
|
|
type HTTPResponseLocalizer interface {
|
|
|
|
BadRequestLocalized(string) (int, interface{})
|
|
|
|
UnauthorizedLocalized(string) (int, interface{})
|
|
|
|
ForbiddenLocalized(string) (int, interface{})
|
|
|
|
InternalServerErrorLocalized(string) (int, interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloneableLocalizer is a localizer which can clone itself.
|
|
|
|
type CloneableLocalizer interface {
|
|
|
|
Clone() CloneableLocalizer
|
2022-03-28 16:51:41 +03:00
|
|
|
}
|
|
|
|
|
2019-09-04 15:22:27 +03:00
|
|
|
// NewLocalizer returns localizer instance with specified parameters.
|
|
|
|
// Usage:
|
2024-06-14 18:10:48 +03:00
|
|
|
//
|
|
|
|
// NewLocalizer(language.English, DefaultLocalizerMatcher(), "translations")
|
2022-04-22 09:37:16 +03:00
|
|
|
func NewLocalizer(locale language.Tag, matcher language.Matcher, translationsPath string) LocalizerInterface {
|
2019-09-04 15:22:27 +03:00
|
|
|
localizer := &Localizer{
|
2020-05-18 13:37:24 +03:00
|
|
|
i18nStorage: &sync.Map{},
|
2019-09-04 15:22:27 +03:00
|
|
|
LocaleMatcher: matcher,
|
|
|
|
TranslationsPath: translationsPath,
|
2020-05-18 13:37:24 +03:00
|
|
|
loadMutex: &sync.RWMutex{},
|
2019-09-04 15:22:27 +03:00
|
|
|
}
|
|
|
|
localizer.SetLanguage(locale)
|
|
|
|
localizer.LoadTranslations()
|
|
|
|
|
|
|
|
return localizer
|
|
|
|
}
|
|
|
|
|
2021-07-29 11:29:31 +03:00
|
|
|
// NewLocalizerFS returns localizer instance with specified parameters.
|
2019-12-25 17:36:02 +03:00
|
|
|
// Usage:
|
2024-06-14 18:10:48 +03:00
|
|
|
//
|
|
|
|
// NewLocalizerFS(language.English, DefaultLocalizerMatcher(), translationsFS)
|
|
|
|
//
|
2019-12-25 17:36:02 +03:00
|
|
|
// TODO This code should be covered with tests.
|
2021-07-29 11:29:31 +03:00
|
|
|
func NewLocalizerFS(
|
|
|
|
locale language.Tag, matcher language.Matcher, translationsFS fs.FS,
|
2022-04-22 09:37:16 +03:00
|
|
|
) LocalizerInterface {
|
2019-12-25 17:36:02 +03:00
|
|
|
localizer := &Localizer{
|
2021-07-29 11:29:31 +03:00
|
|
|
i18nStorage: &sync.Map{},
|
|
|
|
LocaleMatcher: matcher,
|
|
|
|
TranslationsFS: translationsFS,
|
|
|
|
loadMutex: &sync.RWMutex{},
|
2019-12-25 17:36:02 +03:00
|
|
|
}
|
|
|
|
localizer.SetLanguage(locale)
|
|
|
|
localizer.LoadTranslations()
|
|
|
|
|
|
|
|
return localizer
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// DefaultLocalizerBundle returns new localizer bundle with English as default language.
|
2019-09-04 15:22:27 +03:00
|
|
|
func DefaultLocalizerBundle() *i18n.Bundle {
|
2020-05-18 13:37:24 +03:00
|
|
|
return i18n.NewBundle(DefaultLanguage)
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// LocalizerBundle returns new localizer bundle provided language as default.
|
2020-05-18 13:37:24 +03:00
|
|
|
func LocalizerBundle(tag language.Tag) *i18n.Bundle {
|
|
|
|
return i18n.NewBundle(tag)
|
2019-09-04 15:22:27 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// DefaultLocalizerMatcher returns matcher with English, Russian and Spanish tags.
|
2019-09-04 15:22:27 +03:00
|
|
|
func DefaultLocalizerMatcher() language.Matcher {
|
2020-05-18 13:37:24 +03:00
|
|
|
return language.NewMatcher(DefaultLanguages)
|
2019-09-04 15:22:27 +03:00
|
|
|
}
|
|
|
|
|
2020-11-12 10:23:27 +03:00
|
|
|
// Clone *core.Localizer. Clone shares it's translations with the parent localizer. Language tag will not be shared.
|
|
|
|
// Because of that you can change clone's language without affecting parent localizer.
|
|
|
|
// This method should be used when LocalizationMiddleware is not feasible (outside of *gin.HandlerFunc).
|
2022-04-22 09:37:16 +03:00
|
|
|
func (l *Localizer) Clone() CloneableLocalizer {
|
2020-11-12 10:23:27 +03:00
|
|
|
clone := &Localizer{
|
|
|
|
i18nStorage: l.i18nStorage,
|
2021-07-29 11:29:31 +03:00
|
|
|
TranslationsFS: l.TranslationsFS,
|
2020-11-12 10:23:27 +03:00
|
|
|
LocaleMatcher: l.LocaleMatcher,
|
|
|
|
LanguageTag: l.LanguageTag,
|
|
|
|
TranslationsPath: l.TranslationsPath,
|
|
|
|
loadMutex: l.loadMutex,
|
|
|
|
}
|
|
|
|
clone.SetLanguage(DefaultLanguage)
|
|
|
|
|
|
|
|
return clone
|
|
|
|
}
|
|
|
|
|
2019-09-04 15:22:27 +03:00
|
|
|
// LocalizationMiddleware returns gin.HandlerFunc which will set localizer language by Accept-Language header
|
2020-05-18 13:37:24 +03:00
|
|
|
// Result Localizer instance will share it's internal data (translations, bundles, etc) with instance which was used
|
|
|
|
// to append middleware to gin.
|
|
|
|
// Because of that all Localizer instances from this middleware will share *same* mutex. This mutex is used to wrap
|
|
|
|
// i18n.Bundle methods (those aren't goroutine-safe to use).
|
2019-09-04 15:22:27 +03:00
|
|
|
// Usage:
|
2024-06-14 18:10:48 +03:00
|
|
|
//
|
|
|
|
// engine := gin.New()
|
|
|
|
// localizer := NewLocalizer("en", DefaultLocalizerMatcher(), "translations")
|
|
|
|
// engine.Use(localizer.LocalizationMiddleware())
|
2019-09-04 15:22:27 +03:00
|
|
|
func (l *Localizer) LocalizationMiddleware() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
2022-04-22 09:37:16 +03:00
|
|
|
clone := l.Clone().(LocaleControls)
|
2020-05-18 13:37:24 +03:00
|
|
|
clone.SetLocale(c.GetHeader("Accept-Language"))
|
|
|
|
c.Set(LocalizerContextKey, clone)
|
2019-09-04 15:22:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocalizationFuncMap returns template.FuncMap (html template is used) with one method - trans
|
|
|
|
// Usage in code:
|
2024-06-14 18:10:48 +03:00
|
|
|
//
|
|
|
|
// engine := gin.New()
|
|
|
|
// engine.FuncMap = localizer.LocalizationFuncMap()
|
|
|
|
//
|
2019-09-04 15:22:27 +03:00
|
|
|
// or (with multitemplate)
|
2024-06-14 18:10:48 +03:00
|
|
|
//
|
|
|
|
// renderer := multitemplate.NewRenderer()
|
|
|
|
// funcMap := localizer.LocalizationFuncMap()
|
|
|
|
// renderer.AddFromFilesFuncs("index", funcMap, "template/index.html")
|
|
|
|
//
|
2019-09-04 15:22:27 +03:00
|
|
|
// funcMap must be passed for every .AddFromFilesFuncs call
|
|
|
|
// Usage in templates:
|
2024-06-14 18:10:48 +03:00
|
|
|
//
|
|
|
|
// <p class="info">{{"need_login_msg" | trans}}
|
|
|
|
//
|
2019-09-04 15:22:27 +03:00
|
|
|
// You can borrow FuncMap from this method and add your functions to it.
|
|
|
|
func (l *Localizer) LocalizationFuncMap() template.FuncMap {
|
|
|
|
return template.FuncMap{
|
|
|
|
"trans": l.GetLocalizedMessage,
|
2022-02-17 09:39:25 +03:00
|
|
|
"transTpl": func(messageID string, parts ...string) string {
|
|
|
|
if len(parts) == 0 {
|
|
|
|
return l.GetLocalizedMessage(messageID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(parts)%2 != 0 {
|
|
|
|
parts = append(parts, "")
|
|
|
|
}
|
|
|
|
|
2024-06-14 18:10:48 +03:00
|
|
|
partsMap := make(map[string]interface{}, len(parts)/2) // nolint:gomnd
|
2022-02-17 09:39:25 +03:00
|
|
|
|
|
|
|
for i := 0; i < len(parts)-1; i += 2 {
|
|
|
|
partsMap[parts[i]] = parts[i+1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return l.GetLocalizedTemplateMessage(messageID, partsMap)
|
|
|
|
},
|
2019-09-04 15:22:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// createLocaleBundleByTag creates locale bundle by language tag.
|
2020-05-18 13:37:24 +03:00
|
|
|
func (l *Localizer) createLocaleBundleByTag(tag language.Tag) *i18n.Bundle {
|
|
|
|
bundle := i18n.NewBundle(tag)
|
|
|
|
bundle.RegisterUnmarshalFunc("yml", yaml.Unmarshal)
|
|
|
|
l.loadTranslationsToBundle(bundle)
|
|
|
|
|
|
|
|
return bundle
|
2019-09-04 15:22:27 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// LoadTranslations will load all translation files from translations directory or from embedded box.
|
2019-09-04 15:22:27 +03:00
|
|
|
func (l *Localizer) LoadTranslations() {
|
2020-05-18 13:37:24 +03:00
|
|
|
defer l.loadMutex.Unlock()
|
|
|
|
l.loadMutex.Lock()
|
|
|
|
l.getCurrentLocalizer()
|
|
|
|
}
|
2019-09-18 10:28:55 +03:00
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// loadTranslationsToBundle loads translations to provided bundle.
|
2020-05-18 13:37:24 +03:00
|
|
|
func (l *Localizer) loadTranslationsToBundle(i18nBundle *i18n.Bundle) {
|
2019-09-18 10:28:55 +03:00
|
|
|
switch {
|
|
|
|
case l.TranslationsPath != "":
|
2020-05-18 13:37:24 +03:00
|
|
|
if err := l.loadFromDirectory(i18nBundle); err != nil {
|
2019-09-18 10:28:55 +03:00
|
|
|
panic(err.Error())
|
|
|
|
}
|
2021-07-29 11:29:31 +03:00
|
|
|
default:
|
2020-05-18 13:37:24 +03:00
|
|
|
if err := l.loadFromFS(i18nBundle); err != nil {
|
2019-09-18 10:28:55 +03:00
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// LoadTranslations will load all translation files from translations directory.
|
2020-05-18 13:37:24 +03:00
|
|
|
func (l *Localizer) loadFromDirectory(i18nBundle *i18n.Bundle) error {
|
2024-06-14 18:10:48 +03:00
|
|
|
files, err := os.ReadDir(l.TranslationsPath)
|
2019-09-04 15:22:27 +03:00
|
|
|
if err != nil {
|
2019-09-18 10:28:55 +03:00
|
|
|
return err
|
2019-09-04 15:22:27 +03:00
|
|
|
}
|
2019-09-18 10:28:55 +03:00
|
|
|
|
2019-09-04 15:22:27 +03:00
|
|
|
for _, f := range files {
|
|
|
|
if !f.IsDir() {
|
2020-05-18 13:37:24 +03:00
|
|
|
i18nBundle.MustLoadMessageFile(path.Join(l.TranslationsPath, f.Name()))
|
2019-09-04 15:22:27 +03:00
|
|
|
}
|
|
|
|
}
|
2019-09-18 10:28:55 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-29 11:29:31 +03:00
|
|
|
// LoadTranslations will load all translation files from embedding in binary.
|
2020-05-18 13:37:24 +03:00
|
|
|
func (l *Localizer) loadFromFS(i18nBundle *i18n.Bundle) error {
|
2021-07-29 11:29:31 +03:00
|
|
|
translationFiles, err := fs.ReadDir(l.TranslationsFS, string('.'))
|
2019-09-18 10:28:55 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-29 11:29:31 +03:00
|
|
|
for _, file := range translationFiles {
|
|
|
|
if !file.IsDir() {
|
|
|
|
data, err := fs.ReadFile(l.TranslationsFS, file.Name())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := i18nBundle.ParseMessageFileBytes(data, file.Name()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-12 09:35:05 +03:00
|
|
|
|
|
|
|
return nil
|
2019-09-04 15:22:27 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// getLocalizer returns *i18n.Localizer with provided language tag. It will be created if not exist.
|
2020-05-18 13:37:24 +03:00
|
|
|
func (l *Localizer) getLocalizer(tag language.Tag) *i18n.Localizer {
|
|
|
|
var localizer *i18n.Localizer
|
|
|
|
|
|
|
|
if l.isUnd(tag) {
|
|
|
|
tag = DefaultLanguage
|
|
|
|
}
|
|
|
|
|
|
|
|
if item, ok := l.i18nStorage.Load(tag); !ok {
|
|
|
|
l.i18nStorage.Store(tag, i18n.NewLocalizer(l.createLocaleBundleByTag(tag), tag.String()))
|
|
|
|
} else {
|
|
|
|
localizer = item.(*i18n.Localizer)
|
|
|
|
}
|
|
|
|
|
|
|
|
return localizer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Localizer) matchByString(al string) language.Tag {
|
|
|
|
tag, _ := language.MatchStrings(l.LocaleMatcher, al)
|
|
|
|
if l.isUnd(tag) {
|
|
|
|
return DefaultLanguage
|
|
|
|
}
|
|
|
|
|
|
|
|
return tag
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Localizer) isUnd(tag language.Tag) bool {
|
|
|
|
return tag == language.Und || tag.IsRoot()
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// getCurrentLocalizer returns *i18n.Localizer with current language tag.
|
2020-05-18 13:37:24 +03:00
|
|
|
func (l *Localizer) getCurrentLocalizer() *i18n.Localizer {
|
2022-04-22 09:37:16 +03:00
|
|
|
return l.getLocalizer(l.Language())
|
2020-05-18 13:37:24 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// SetLocale will change language for current localizer.
|
2019-09-04 15:22:27 +03:00
|
|
|
func (l *Localizer) SetLocale(al string) {
|
2020-05-18 13:37:24 +03:00
|
|
|
l.SetLanguage(l.matchByString(al))
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// Preload provided languages (so they will not be loaded every time in middleware).
|
2020-05-18 13:37:24 +03:00
|
|
|
func (l *Localizer) Preload(tags []language.Tag) {
|
|
|
|
for _, tag := range tags {
|
|
|
|
l.getLocalizer(tag)
|
|
|
|
}
|
2019-09-04 15:22:27 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// SetLanguage will change language using language tag.
|
2019-09-04 15:22:27 +03:00
|
|
|
func (l *Localizer) SetLanguage(tag language.Tag) {
|
2020-05-18 13:37:24 +03:00
|
|
|
if l.isUnd(tag) {
|
|
|
|
tag = DefaultLanguage
|
|
|
|
}
|
|
|
|
|
2019-09-04 15:22:27 +03:00
|
|
|
l.LanguageTag = tag
|
2020-05-18 13:37:24 +03:00
|
|
|
l.LoadTranslations()
|
2019-09-04 15:22:27 +03:00
|
|
|
}
|
|
|
|
|
2022-04-22 09:37:16 +03:00
|
|
|
// Language returns current language tag.
|
|
|
|
func (l *Localizer) Language() language.Tag {
|
|
|
|
return l.LanguageTag
|
|
|
|
}
|
|
|
|
|
2019-09-04 15:22:27 +03:00
|
|
|
// FetchLanguage will load language from tag
|
2020-05-18 13:37:24 +03:00
|
|
|
//
|
2021-02-09 14:57:14 +03:00
|
|
|
// Deprecated: Use `(*core.Localizer).LoadTranslations()` instead.
|
2019-09-04 15:22:27 +03:00
|
|
|
func (l *Localizer) FetchLanguage() {
|
2020-05-18 13:37:24 +03:00
|
|
|
l.LoadTranslations()
|
2019-09-04 15:22:27 +03:00
|
|
|
}
|
|
|
|
|
2020-05-18 13:37:24 +03:00
|
|
|
// GetLocalizedMessage will return localized message by it's ID. It doesn't use `Must` prefix in order to keep BC.
|
2019-09-04 15:22:27 +03:00
|
|
|
func (l *Localizer) GetLocalizedMessage(messageID string) string {
|
2020-05-18 13:37:24 +03:00
|
|
|
return l.getCurrentLocalizer().MustLocalize(&i18n.LocalizeConfig{MessageID: messageID})
|
2019-09-04 15:22:27 +03:00
|
|
|
}
|
|
|
|
|
2021-12-01 15:40:23 +03:00
|
|
|
// GetLocalizedTemplateMessage will return localized message with specified data.
|
|
|
|
// It doesn't use `Must` prefix in order to keep BC. It uses text/template syntax: https://golang.org/pkg/text/template/
|
2019-09-04 15:22:27 +03:00
|
|
|
func (l *Localizer) GetLocalizedTemplateMessage(messageID string, templateData map[string]interface{}) string {
|
2020-05-18 13:37:24 +03:00
|
|
|
return l.getCurrentLocalizer().MustLocalize(&i18n.LocalizeConfig{
|
|
|
|
MessageID: messageID,
|
|
|
|
TemplateData: templateData,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// Localize will return localized message by it's ID, or error if message wasn't found.
|
2020-05-18 13:37:24 +03:00
|
|
|
func (l *Localizer) Localize(messageID string) (string, error) {
|
|
|
|
return l.getCurrentLocalizer().Localize(&i18n.LocalizeConfig{MessageID: messageID})
|
|
|
|
}
|
|
|
|
|
2020-08-03 14:45:16 +03:00
|
|
|
// LocalizeTemplateMessage will return localized message with specified data, or error if message wasn't found
|
2020-05-18 13:37:24 +03:00
|
|
|
// It uses text/template syntax: https://golang.org/pkg/text/template/
|
|
|
|
func (l *Localizer) LocalizeTemplateMessage(messageID string, templateData map[string]interface{}) (string, error) {
|
|
|
|
return l.getCurrentLocalizer().Localize(&i18n.LocalizeConfig{
|
2019-09-04 15:22:27 +03:00
|
|
|
MessageID: messageID,
|
|
|
|
TemplateData: templateData,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:40:23 +03:00
|
|
|
// BadRequestLocalized is same as errorutil.BadRequest(string), but passed string will be localized.
|
2019-09-04 15:22:27 +03:00
|
|
|
func (l *Localizer) BadRequestLocalized(err string) (int, interface{}) {
|
2021-12-01 15:40:23 +03:00
|
|
|
return errorutil.BadRequest(l.GetLocalizedMessage(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnauthorizedLocalized is same as errorutil.Unauthorized(string), but passed string will be localized.
|
|
|
|
func (l *Localizer) UnauthorizedLocalized(err string) (int, interface{}) {
|
|
|
|
return errorutil.Unauthorized(l.GetLocalizedMessage(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ForbiddenLocalized is same as errorutil.Forbidden(string), but passed string will be localized.
|
|
|
|
func (l *Localizer) ForbiddenLocalized(err string) (int, interface{}) {
|
|
|
|
return errorutil.Forbidden(l.GetLocalizedMessage(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
// InternalServerErrorLocalized is same as errorutil.InternalServerError(string), but passed string will be localized.
|
|
|
|
func (l *Localizer) InternalServerErrorLocalized(err string) (int, interface{}) {
|
|
|
|
return errorutil.InternalServerError(l.GetLocalizedMessage(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetContextLocalizer returns localizer from context if it is present there.
|
|
|
|
// Language will be set using Accept-Language header and root language tag.
|
2022-04-22 09:37:16 +03:00
|
|
|
func GetContextLocalizer(c *gin.Context) (loc LocalizerInterface, ok bool) {
|
2021-12-01 15:40:23 +03:00
|
|
|
loc, ok = extractLocalizerFromContext(c)
|
|
|
|
if loc != nil {
|
|
|
|
loc.SetLocale(c.GetHeader("Accept-Language"))
|
|
|
|
|
2022-04-22 09:37:16 +03:00
|
|
|
lang := GetRootLanguageTag(loc.Language())
|
|
|
|
if lang != loc.Language() {
|
2021-12-01 15:40:23 +03:00
|
|
|
loc.SetLanguage(lang)
|
|
|
|
loc.LoadTranslations()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2019-09-04 15:22:27 +03:00
|
|
|
}
|
2020-05-18 13:37:24 +03:00
|
|
|
|
2021-12-01 15:40:23 +03:00
|
|
|
// MustGetContextLocalizer returns Localizer instance if it exists in provided context. Panics otherwise.
|
2022-04-22 09:37:16 +03:00
|
|
|
func MustGetContextLocalizer(c *gin.Context) LocalizerInterface {
|
2021-12-01 15:40:23 +03:00
|
|
|
if localizer, ok := GetContextLocalizer(c); ok {
|
|
|
|
return localizer
|
|
|
|
}
|
|
|
|
panic("localizer is not present in provided context")
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractLocalizerFromContext returns localizer from context if it exist there.
|
2022-04-22 09:37:16 +03:00
|
|
|
func extractLocalizerFromContext(c *gin.Context) (LocalizerInterface, bool) {
|
2020-05-18 13:37:24 +03:00
|
|
|
if c == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if item, ok := c.Get(LocalizerContextKey); ok {
|
2022-04-22 09:37:16 +03:00
|
|
|
if localizer, ok := item.(LocalizerInterface); ok {
|
2020-05-18 13:37:24 +03:00
|
|
|
return localizer, true
|
|
|
|
}
|
|
|
|
}
|
2020-08-03 14:45:16 +03:00
|
|
|
|
|
|
|
return nil, false
|
2020-05-18 13:37:24 +03:00
|
|
|
}
|
|
|
|
|
2021-12-01 15:40:23 +03:00
|
|
|
// GetRootLanguageTag returns root language tag for country-specific tags (e.g "es" for "es_CA").
|
|
|
|
// Useful when you don't have country-specific language variations.
|
|
|
|
func GetRootLanguageTag(t language.Tag) language.Tag {
|
|
|
|
for {
|
|
|
|
parent := t.Parent()
|
|
|
|
if parent == language.Und {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
t = parent
|
2020-05-18 13:37:24 +03:00
|
|
|
}
|
|
|
|
}
|