github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/hugolib/translations.go (about)

     1  // Copyright 2019 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package hugolib
    15  
    16  import (
    17  	"github.com/gohugoio/hugo/resources/page"
    18  )
    19  
    20  func pagesToTranslationsMap(sites []*Site) map[string]page.Pages {
    21  	out := make(map[string]page.Pages)
    22  
    23  	for _, s := range sites {
    24  		s.pageMap.pageTrees.WalkLinkable(func(ss string, n *contentNode) bool {
    25  			p := n.p
    26  			// TranslationKey is implemented for all page types.
    27  			base := p.TranslationKey()
    28  
    29  			pageTranslations, found := out[base]
    30  			if !found {
    31  				pageTranslations = make(page.Pages, 0)
    32  			}
    33  
    34  			pageTranslations = append(pageTranslations, p)
    35  			out[base] = pageTranslations
    36  
    37  			return false
    38  		})
    39  	}
    40  
    41  	return out
    42  }
    43  
    44  func assignTranslationsToPages(allTranslations map[string]page.Pages, sites []*Site) {
    45  	for _, s := range sites {
    46  		s.pageMap.pageTrees.Walk(func(ss string, n *contentNode) bool {
    47  			p := n.p
    48  			base := p.TranslationKey()
    49  			translations, found := allTranslations[base]
    50  			if !found {
    51  				return false
    52  			}
    53  			p.setTranslations(translations)
    54  			return false
    55  		})
    56  	}
    57  }