feat(localization): Add option for forcing a language (#742)
* Add forcesLanguage option * Change comments for forced language option * Add changes to CHANGELOG.md --------- Signed-off-by: Xe Iaso <me@xeiaso.net> Co-authored-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
parent
c981c23f7e
commit
0e43138324
4 changed files with 19 additions and 7 deletions
|
|
@ -3,6 +3,7 @@ package localization
|
|||
import (
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"github.com/TecharoHQ/anubis"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
|
|
@ -57,14 +58,14 @@ func NewLocalizationService() *LocalizationService {
|
|||
|
||||
globalService = &LocalizationService{bundle: bundle}
|
||||
})
|
||||
|
||||
|
||||
// Safety check - if globalService is still nil, create a minimal one
|
||||
if globalService == nil {
|
||||
bundle := i18n.NewBundle(language.English)
|
||||
bundle.RegisterUnmarshalFunc("json", json.Unmarshal)
|
||||
globalService = &LocalizationService{bundle: bundle}
|
||||
}
|
||||
|
||||
|
||||
return globalService
|
||||
}
|
||||
|
||||
|
|
@ -93,8 +94,13 @@ func (sl *SimpleLocalizer) T(messageID string) string {
|
|||
return sl.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: messageID})
|
||||
}
|
||||
|
||||
// GetLocalizer creates a localizer based on the request's Accept-Language header
|
||||
// GetLocalizer creates a localizer based on the request's Accept-Language header or forcedLanguage option
|
||||
func GetLocalizer(r *http.Request) *SimpleLocalizer {
|
||||
localizer := NewLocalizationService().GetLocalizerFromRequest(r)
|
||||
var localizer *i18n.Localizer
|
||||
if anubis.ForcedLanguage == "" {
|
||||
localizer = NewLocalizationService().GetLocalizerFromRequest(r)
|
||||
} else {
|
||||
localizer = NewLocalizationService().GetLocalizer(anubis.ForcedLanguage)
|
||||
}
|
||||
return &SimpleLocalizer{Localizer: localizer}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue