From 627eed1d620910f494056330733db6c6187b8fe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sun, 10 Apr 2022 20:30:52 +0200 Subject: Make string sorting (e.g. ByTitle, ByLinkTitle and ByParam) language aware Fixes #2180 --- hugolib/site.go | 5 +++++ hugolib/taxonomy.go | 23 ++++++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) (limited to 'hugolib') diff --git a/hugolib/site.go b/hugolib/site.go index d7b5cb64e..bbabf91a3 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -739,7 +739,12 @@ func (s *SiteInfo) Sites() page.Sites { } // Current returns the currently rendered Site. +// If that isn't set yet, which is the situation before we start rendering, +// if will return the Site itself. func (s *SiteInfo) Current() page.Site { + if s.s.h.currentSite == nil { + return s + } return s.s.h.currentSite.Info } diff --git a/hugolib/taxonomy.go b/hugolib/taxonomy.go index e3f033109..6b3c2b961 100644 --- a/hugolib/taxonomy.go +++ b/hugolib/taxonomy.go @@ -18,6 +18,7 @@ import ( "sort" "github.com/gohugoio/hugo/compare" + "github.com/gohugoio/hugo/langs" "github.com/gohugoio/hugo/resources/page" ) @@ -40,6 +41,15 @@ type Taxonomy map[string]page.WeightedPages // Important because you can't order a map. type OrderedTaxonomy []OrderedTaxonomyEntry +// getOneOPage returns one page in the taxonomy, +// nil if there is none. +func (t OrderedTaxonomy) getOneOPage() page.Page { + if len(t) == 0 { + return nil + } + return t[0].Pages()[0] +} + // OrderedTaxonomyEntry is similar to an element of a Taxonomy, but with the key embedded (as name) // e.g: {Name: Technology, page.WeightedPages: TaxonomyPages} type OrderedTaxonomyEntry struct { @@ -72,11 +82,18 @@ func (i Taxonomy) TaxonomyArray() OrderedTaxonomy { // Alphabetical returns an ordered taxonomy sorted by key name. func (i Taxonomy) Alphabetical() OrderedTaxonomy { + ia := i.TaxonomyArray() + p := ia.getOneOPage() + if p == nil { + return ia + } + currentSite := p.Site().Current() + coll := langs.GetCollator(currentSite.Language()) + coll.Lock() + defer coll.Unlock() name := func(i1, i2 *OrderedTaxonomyEntry) bool { - return compare.LessStrings(i1.Name, i2.Name) + return coll.CompareStrings(i1.Name, i2.Name) < 0 } - - ia := i.TaxonomyArray() oiBy(name).Sort(ia) return ia } -- cgit v1.2.3