mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-29 10:12:04 -05:00
i18n: Simplify code for detecting of untranslated id
This commit is contained in:
parent
cce49997a4
commit
8f2ab5f498
1 changed files with 22 additions and 19 deletions
41
i18n/i18n.go
41
i18n/i18n.go
|
@ -57,13 +57,8 @@ func (t Translator) Func(lang string) bundle.TranslateFunc {
|
||||||
|
|
||||||
func (t Translator) initFuncs(bndl *bundle.Bundle) {
|
func (t Translator) initFuncs(bndl *bundle.Bundle) {
|
||||||
defaultContentLanguage := t.cfg.GetString("defaultContentLanguage")
|
defaultContentLanguage := t.cfg.GetString("defaultContentLanguage")
|
||||||
var (
|
|
||||||
defaultT bundle.TranslateFunc
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
|
|
||||||
defaultT, err = bndl.Tfunc(defaultContentLanguage)
|
|
||||||
|
|
||||||
|
defaultT, err := bndl.Tfunc(defaultContentLanguage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jww.WARN.Printf("No translation bundle found for default language %q", defaultContentLanguage)
|
jww.WARN.Printf("No translation bundle found for default language %q", defaultContentLanguage)
|
||||||
}
|
}
|
||||||
|
@ -79,20 +74,17 @@ func (t Translator) initFuncs(bndl *bundle.Bundle) {
|
||||||
}
|
}
|
||||||
|
|
||||||
translated := tFunc(translationID, args...)
|
translated := tFunc(translationID, args...)
|
||||||
// If there is no translation for translationID,
|
if translated != translationID {
|
||||||
// then Tfunc returns translationID itself.
|
|
||||||
if translated == translationID {
|
|
||||||
// But if user set same translationID and translation, we should check
|
|
||||||
// if it really untranslated this way:
|
|
||||||
// If bndl contains the translationID for specified currentLang,
|
|
||||||
// then the translationID is actually translated.
|
|
||||||
_, contains := bndl.Translations()[currentLang][translationID]
|
|
||||||
if contains {
|
|
||||||
return translated
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return translated
|
return translated
|
||||||
}
|
}
|
||||||
|
// If there is no translation for translationID,
|
||||||
|
// then Tfunc returns translationID itself.
|
||||||
|
// But if user set same translationID and translation, we should check
|
||||||
|
// if it really untranslated:
|
||||||
|
if isIDTranslated(currentLang, translationID, bndl) {
|
||||||
|
return translated
|
||||||
|
}
|
||||||
|
|
||||||
if t.cfg.GetBool("logI18nWarnings") {
|
if t.cfg.GetBool("logI18nWarnings") {
|
||||||
i18nWarningLogger.Printf("i18n|MISSING_TRANSLATION|%s|%s", currentLang, translationID)
|
i18nWarningLogger.Printf("i18n|MISSING_TRANSLATION|%s|%s", currentLang, translationID)
|
||||||
}
|
}
|
||||||
|
@ -100,7 +92,11 @@ func (t Translator) initFuncs(bndl *bundle.Bundle) {
|
||||||
return "[i18n] " + translationID
|
return "[i18n] " + translationID
|
||||||
}
|
}
|
||||||
if defaultT != nil {
|
if defaultT != nil {
|
||||||
if translated := defaultT(translationID, args...); translated != translationID {
|
translated := defaultT(translationID, args...)
|
||||||
|
if translated != translationID {
|
||||||
|
return translated
|
||||||
|
}
|
||||||
|
if isIDTranslated(defaultContentLanguage, translationID, bndl) {
|
||||||
return translated
|
return translated
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -108,3 +104,10 @@ func (t Translator) initFuncs(bndl *bundle.Bundle) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If bndl contains the translationID for specified currentLang,
|
||||||
|
// then the translationID is actually translated.
|
||||||
|
func isIDTranslated(lang, id string, b *bundle.Bundle) bool {
|
||||||
|
_, contains := b.Translations()[lang][id]
|
||||||
|
return contains
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue