Last active
November 5, 2021 09:49
-
-
Save fnordfish/100564ed0190452dd7136c66bd600961 to your computer and use it in GitHub Desktop.
A proof-of-concept patch to illustrate the proposed feature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 666e8b2ce0a000168ee5de0e5b98147e462dab79 Mon Sep 17 00:00:00 2001 | |
From: Robert Schulze <[email protected]> | |
Date: Tue, 2 Nov 2021 14:38:57 +0100 | |
Subject: [PATCH] --wip-- [skip ci] | |
--- | |
hugolib/paths/paths.go | 5 +++++ | |
hugolib/site.go | 30 +++++++++++++++++++++++++----- | |
hugolib/site_render.go | 8 +++++++- | |
langs/config.go | 2 ++ | |
langs/language.go | 1 + | |
5 files changed, 40 insertions(+), 6 deletions(-) | |
diff --git hugolib/paths/paths.go hugolib/paths/paths.go | |
index 1ab7ae87..8f6be8eb 100644 | |
--- hugolib/paths/paths.go | |
+++ hugolib/paths/paths.go | |
@@ -231,6 +231,11 @@ func (p *Paths) GetLanguagePrefix() string { | |
if currentLang == "" || (currentLang == defaultLang && !defaultInSubDir) { | |
return "" | |
} | |
+ | |
+ if p.Language.LanguagePrefix != "" { | |
+ return p.Language.LanguagePrefix | |
+ } | |
+ | |
return currentLang | |
} | |
diff --git hugolib/site.go hugolib/site.go | |
index 96cf0b93..0ea05869 100644 | |
--- hugolib/site.go | |
+++ hugolib/site.go | |
@@ -1277,7 +1277,11 @@ func (s *Site) initialize() (err error) { | |
func (s *SiteInfo) HomeAbsURL() string { | |
base := "" | |
if s.IsMultiLingual() { | |
- base = s.Language().Lang | |
+ if s.Language().LanguagePrefix != "" { | |
+ base = s.Language().LanguagePrefix | |
+ } else { | |
+ base = s.Language().Lang | |
+ } | |
} | |
return s.owner.AbsURL(base, false) | |
} | |
@@ -1309,7 +1313,11 @@ func (s *Site) initializeSiteInfo() error { | |
languagePrefix := "" | |
if s.multilingualEnabled() && (defaultContentInSubDir || lang.Lang != defaultContentLanguage) { | |
- languagePrefix = "/" + lang.Lang | |
+ if lang.LanguagePrefix != "" { | |
+ languagePrefix = "/" + lang.LanguagePrefix | |
+ } else { | |
+ languagePrefix = "/" + lang.Lang | |
+ } | |
} | |
uglyURLs := func(p page.Page) bool { | |
@@ -1544,7 +1552,11 @@ func (s *Site) assembleMenus() { | |
// get any language code to prefix the target file path with. | |
func (s *Site) getLanguageTargetPathLang(alwaysInSubDir bool) string { | |
if s.h.IsMultihost() { | |
- return s.Language().Lang | |
+ if s.Language().LanguagePrefix != "" { | |
+ return s.Language().LanguagePrefix | |
+ } else { | |
+ return s.Language().Lang | |
+ } | |
} | |
return s.getLanguagePermalinkLang(alwaysInSubDir) | |
@@ -1557,13 +1569,21 @@ func (s *Site) getLanguagePermalinkLang(alwaysInSubDir bool) string { | |
} | |
if alwaysInSubDir { | |
- return s.Language().Lang | |
+ if s.Language().LanguagePrefix != "" { | |
+ return s.Language().LanguagePrefix | |
+ } else { | |
+ return s.Language().Lang | |
+ } | |
} | |
isDefault := s.Language().Lang == s.multilingual().DefaultLang.Lang | |
if !isDefault || s.Info.defaultContentLanguageInSubdir { | |
- return s.Language().Lang | |
+ if s.Language().LanguagePrefix != "" { | |
+ return s.Language().LanguagePrefix | |
+ } else { | |
+ return s.Language().Lang | |
+ } | |
} | |
return "" | |
diff --git hugolib/site_render.go hugolib/site_render.go | |
index 77ece780..e3cd46d5 100644 | |
--- hugolib/site_render.go | |
+++ hugolib/site_render.go | |
@@ -388,7 +388,13 @@ func (s *Site) renderMainLanguageRedirect() error { | |
if found { | |
mainLang := s.h.multilingual.DefaultLang | |
if s.Info.defaultContentLanguageInSubdir { | |
- mainLangURL := s.PathSpec.AbsURL(mainLang.Lang+"/", false) | |
+ mainLangPrefix := "" | |
+ if mainLang.LanguagePrefix != "" { | |
+ mainLangPrefix = mainLang.LanguagePrefix | |
+ } else { | |
+ mainLangPrefix = mainLang.Lang | |
+ } | |
+ mainLangURL := s.PathSpec.AbsURL(mainLangPrefix+"/", false) | |
s.Log.Debugf("Write redirect to main language %s: %s", mainLang, mainLangURL) | |
if err := s.publishDestAlias(true, "/", mainLangURL, html, nil); err != nil { | |
return err | |
diff --git langs/config.go langs/config.go | |
index f79b7dd0..974b7fea 100644 | |
--- langs/config.go | |
+++ langs/config.go | |
@@ -190,6 +190,8 @@ func toSortedLanguages(cfg config.Provider, l map[string]interface{}) (Languages | |
language.LanguageName = cast.ToString(v) | |
case "languagedirection": | |
language.LanguageDirection = cast.ToString(v) | |
+ case "languageprefix": | |
+ language.LanguagePrefix = cast.ToString(v) | |
case "weight": | |
language.Weight = cast.ToInt(v) | |
case "contentdir": | |
diff --git langs/language.go langs/language.go | |
index c4cba15b..ba0cabd3 100644 | |
--- langs/language.go | |
+++ langs/language.go | |
@@ -48,6 +48,7 @@ type Language struct { | |
Lang string | |
LanguageName string | |
LanguageDirection string | |
+ LanguagePrefix string | |
Title string | |
Weight int | |
-- | |
2.33.0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment