Reset the i18n func map on reload

Also improve the error message on missing resource bundles.
This commit is contained in:
Bjørn Erik Pedersen 2016-09-08 17:04:04 +03:00
parent 5ef45bec63
commit fe0c270577
3 changed files with 18 additions and 9 deletions

View file

@ -229,6 +229,11 @@ func NewDistinctFeedbackLogger() *DistinctLogger {
// DistinctErrorLog cann be used to avoid spamming the logs with errors. // DistinctErrorLog cann be used to avoid spamming the logs with errors.
var DistinctErrorLog = NewDistinctErrorLogger() var DistinctErrorLog = NewDistinctErrorLogger()
// InitLoggers sets up the global distinct loggers.
func InitLoggers() {
DistinctErrorLog = NewDistinctErrorLogger()
}
// Deprecated logs ERROR logs about a deprecation, but only once for a given set of arguments' values. // Deprecated logs ERROR logs about a deprecation, but only once for a given set of arguments' values.
func Deprecated(object, item, alternative string) { func Deprecated(object, item, alternative string) {
// deprecatedLogger.Printf("%s's %s is deprecated and will be removed in Hugo %s. Use %s instead.", object, item, NextHugoReleaseVersion(), alternative) // deprecatedLogger.Printf("%s's %s is deprecated and will be removed in Hugo %s. Use %s instead.", object, item, NextHugoReleaseVersion(), alternative)

View file

@ -276,6 +276,8 @@ func (h *HugoSites) Rebuild(config BuildCfg, events ...fsnotify.Event) error {
s.resetBuildState() s.resetBuildState()
} }
helpers.InitLoggers()
changed, err := firstSite.reBuild(events) changed, err := firstSite.reBuild(events)
if err != nil { if err != nil {

View file

@ -33,21 +33,22 @@ type translate struct {
current bundle.TranslateFunc current bundle.TranslateFunc
} }
var translater *translate = &translate{translateFuncs: make(map[string]bundle.TranslateFunc)} var translator *translate
// SetTranslateLang sets the translations language to use during template processing. // SetTranslateLang sets the translations language to use during template processing.
// This construction is unfortunate, but the template system is currently global. // This construction is unfortunate, but the template system is currently global.
func SetTranslateLang(lang string) error { func SetTranslateLang(lang string) error {
if f, ok := translater.translateFuncs[lang]; ok { if f, ok := translator.translateFuncs[lang]; ok {
translater.current = f translator.current = f
} else { } else {
jww.WARN.Printf("Translation func for language %v not found, use default.", lang) jww.WARN.Printf("Translation func for language %v not found, use default.", lang)
translater.current = translater.translateFuncs[viper.GetString("DefaultContentLanguage")] translator.current = translator.translateFuncs[viper.GetString("DefaultContentLanguage")]
} }
return nil return nil
} }
func SetI18nTfuncs(bndl *bundle.Bundle) { func SetI18nTfuncs(bndl *bundle.Bundle) {
translator = &translate{translateFuncs: make(map[string]bundle.TranslateFunc)}
defaultContentLanguage := viper.GetString("DefaultContentLanguage") defaultContentLanguage := viper.GetString("DefaultContentLanguage")
var ( var (
defaultT bundle.TranslateFunc defaultT bundle.TranslateFunc
@ -66,10 +67,10 @@ func SetI18nTfuncs(bndl *bundle.Bundle) {
if err != nil { if err != nil {
jww.WARN.Printf("could not load translations for language %q (%s), will use default content language.\n", lang, err) jww.WARN.Printf("could not load translations for language %q (%s), will use default content language.\n", lang, err)
translater.translateFuncs[currentLang] = defaultT translator.translateFuncs[currentLang] = defaultT
continue continue
} }
translater.translateFuncs[currentLang] = func(translationID string, args ...interface{}) string { translator.translateFuncs[currentLang] = func(translationID string, args ...interface{}) string {
if translated := tFunc(translationID, args...); translated != translationID { if translated := tFunc(translationID, args...); translated != translationID {
return translated return translated
} }
@ -85,8 +86,9 @@ func SetI18nTfuncs(bndl *bundle.Bundle) {
} }
func I18nTranslate(id string, args ...interface{}) (string, error) { func I18nTranslate(id string, args ...interface{}) (string, error) {
if translater == nil || translater.current == nil { if translator == nil || translator.current == nil {
return "", fmt.Errorf("i18n not initialized, have you configured everything properly?") helpers.DistinctErrorLog.Printf("i18n not initialized, check that you have language file (in i18n) that matches the site language or the default language.")
return "", nil
} }
return translater.current(id, args...), nil return translator.current(id, args...), nil
} }