github.com/lyeb/hugo@v0.47.1/hugolib/translations.go (about)

     1  // Copyright 2016 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  // Translations represent the other translations for a given page. The
    17  // string here is the language code, as affected by the `post.LANG.md`
    18  // filename.
    19  type Translations map[string]*Page
    20  
    21  func pagesToTranslationsMap(pages []*Page) map[string]Translations {
    22  	out := make(map[string]Translations)
    23  
    24  	for _, page := range pages {
    25  		base := page.TranslationKey()
    26  
    27  		pageTranslation, present := out[base]
    28  		if !present {
    29  			pageTranslation = make(Translations)
    30  		}
    31  
    32  		pageLang := page.Lang()
    33  		if pageLang == "" {
    34  			continue
    35  		}
    36  
    37  		pageTranslation[pageLang] = page
    38  		out[base] = pageTranslation
    39  	}
    40  
    41  	return out
    42  }
    43  
    44  func assignTranslationsToPages(allTranslations map[string]Translations, pages []*Page) {
    45  	for _, page := range pages {
    46  		page.translations = page.translations[:0]
    47  		base := page.TranslationKey()
    48  		trans, exist := allTranslations[base]
    49  		if !exist {
    50  			continue
    51  		}
    52  
    53  		for _, translatedPage := range trans {
    54  			page.translations = append(page.translations, translatedPage)
    55  		}
    56  
    57  		pageBy(languagePageSort).Sort(page.translations)
    58  	}
    59  }