From: Bjørn Erik Pedersen Date: Tue, 9 Aug 2016 12:26:55 +0000 (+0200) Subject: Add IsTranslated to Node and Page X-Git-Tag: v0.17~123 X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=76108440;p=brevno-suite%2Fhugo Add IsTranslated to Node and Page Makes the templates simpler. See #2309 --- diff --git a/docs/content/content/multilingual.md b/docs/content/content/multilingual.md index f5ff46f9..7e1df45e 100644 --- a/docs/content/content/multilingual.md +++ b/docs/content/content/multilingual.md @@ -93,13 +93,12 @@ By having the same _base file name_, the content pieces are linked together as t To create a list of links to translated content, use a template similar to this: ``` -{{ $translations := .Translations }} -{{ if gt (len $translations) 0 }} +{{ if .IsTranslated }}

{{ i18n "translations" }}

diff --git a/hugolib/node.go b/hugolib/node.go index 53c22af1..780190ec 100644 --- a/hugolib/node.go +++ b/hugolib/node.go @@ -271,10 +271,16 @@ func (n *Node) Translations() Nodes { translations = append(translations, t) } } - return translations } +// IsTranslated returns whether this node is translated to +// other language(s). +func (n *Node) IsTranslated() bool { + n.initTranslations() + return len(n.translations) > 1 +} + func (n *Node) initTranslations() { n.translationsInit.Do(func() { if n.translations != nil { diff --git a/hugolib/page.go b/hugolib/page.go index 74f51c0b..b09e2b1f 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -531,6 +531,12 @@ func (p *Page) AllTranslations() Pages { return p.translations } +// IsTranslated returns whether this content file is translated to +// other language(s). +func (p *Page) IsTranslated() bool { + return len(p.translations) > 1 +} + // Translations returns the translations excluding the current Page. func (p *Page) Translations() Pages { translations := make(Pages, 0)