mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-28 22:19:59 +02:00
@@ -31,6 +31,10 @@ type Language struct {
|
||||
paramsInit sync.Once
|
||||
}
|
||||
|
||||
func (l *Language) String() string {
|
||||
return l.Lang
|
||||
}
|
||||
|
||||
func NewLanguage(lang string) *Language {
|
||||
return &Language{Lang: lang, params: make(map[string]interface{})}
|
||||
}
|
||||
|
@@ -168,21 +168,32 @@ func AbsURL(in string, addLanguage bool) string {
|
||||
}
|
||||
|
||||
if addLanguage {
|
||||
addSlash := in == "" || strings.HasSuffix(in, "/")
|
||||
in = path.Join(getLanguagePrefix(), in)
|
||||
prefix := getLanguagePrefix()
|
||||
|
||||
if addSlash {
|
||||
in += "/"
|
||||
if prefix != "" {
|
||||
addSlash := in == "" || strings.HasSuffix(in, "/")
|
||||
in = path.Join(prefix, in)
|
||||
|
||||
if addSlash {
|
||||
in += "/"
|
||||
}
|
||||
}
|
||||
}
|
||||
return MakePermalink(baseURL, in).String()
|
||||
}
|
||||
|
||||
func getLanguagePrefix() string {
|
||||
defaultLang := viper.GetString("DefaultContentLanguage")
|
||||
defaultInSubDir := viper.GetBool("DefaultContentLanguageInSubdir")
|
||||
|
||||
if !viper.GetBool("Multilingual") {
|
||||
return ""
|
||||
}
|
||||
return viper.Get("CurrentContentLanguage").(*Language).Lang
|
||||
currentLang := viper.Get("CurrentContentLanguage").(*Language).Lang
|
||||
if currentLang == "" || (currentLang == defaultLang && !defaultInSubDir) {
|
||||
return ""
|
||||
}
|
||||
return currentLang
|
||||
}
|
||||
|
||||
// IsAbsURL determines whether the given path points to an absolute URL.
|
||||
@@ -211,12 +222,15 @@ func RelURL(in string, addLanguage bool) string {
|
||||
}
|
||||
|
||||
if addLanguage {
|
||||
hadSlash := strings.HasSuffix(u, "/")
|
||||
prefix := getLanguagePrefix()
|
||||
if prefix != "" {
|
||||
hadSlash := strings.HasSuffix(u, "/")
|
||||
|
||||
u = path.Join(getLanguagePrefix(), u)
|
||||
u = path.Join(prefix, u)
|
||||
|
||||
if hadSlash {
|
||||
u += "/"
|
||||
if hadSlash {
|
||||
u += "/"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -45,19 +45,24 @@ func TestURLize(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAbsURL(t *testing.T) {
|
||||
for _, addLanguage := range []bool{true, false} {
|
||||
for _, m := range []bool{true, false} {
|
||||
for _, l := range []string{"en", "fr"} {
|
||||
doTestAbsURL(t, addLanguage, m, l)
|
||||
for _, defaultInSubDir := range []bool{true, false} {
|
||||
for _, addLanguage := range []bool{true, false} {
|
||||
for _, m := range []bool{true, false} {
|
||||
for _, l := range []string{"en", "fr"} {
|
||||
doTestAbsURL(t, defaultInSubDir, addLanguage, m, l)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func doTestAbsURL(t *testing.T, addLanguage, multilingual bool, lang string) {
|
||||
func doTestAbsURL(t *testing.T, defaultInSubDir, addLanguage, multilingual bool, lang string) {
|
||||
viper.Reset()
|
||||
viper.Set("Multilingual", multilingual)
|
||||
viper.Set("CurrentContentLanguage", NewLanguage(lang))
|
||||
viper.Set("DefaultContentLanguage", "en")
|
||||
viper.Set("DefaultContentLanguageInSubdir", defaultInSubDir)
|
||||
|
||||
tests := []struct {
|
||||
input string
|
||||
baseURL string
|
||||
@@ -79,12 +84,17 @@ func doTestAbsURL(t *testing.T, addLanguage, multilingual bool, lang string) {
|
||||
output := AbsURL(test.input, addLanguage)
|
||||
expected := test.expected
|
||||
if multilingual && addLanguage {
|
||||
expected = strings.Replace(expected, "MULTI", lang+"/", 1)
|
||||
if !defaultInSubDir && lang == "en" {
|
||||
expected = strings.Replace(expected, "MULTI", "", 1)
|
||||
} else {
|
||||
expected = strings.Replace(expected, "MULTI", lang+"/", 1)
|
||||
}
|
||||
|
||||
} else {
|
||||
expected = strings.Replace(expected, "MULTI", "", 1)
|
||||
}
|
||||
if output != expected {
|
||||
t.Errorf("Expected %#v, got %#v\n", expected, output)
|
||||
t.Fatalf("Expected %#v, got %#v\n", expected, output)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -106,19 +116,23 @@ func TestIsAbsURL(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRelURL(t *testing.T) {
|
||||
for _, addLanguage := range []bool{true, false} {
|
||||
for _, m := range []bool{true, false} {
|
||||
for _, l := range []string{"en", "fr"} {
|
||||
doTestRelURL(t, addLanguage, m, l)
|
||||
for _, defaultInSubDir := range []bool{true, false} {
|
||||
for _, addLanguage := range []bool{true, false} {
|
||||
for _, m := range []bool{true, false} {
|
||||
for _, l := range []string{"en", "fr"} {
|
||||
doTestRelURL(t, defaultInSubDir, addLanguage, m, l)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func doTestRelURL(t *testing.T, addLanguage, multilingual bool, lang string) {
|
||||
func doTestRelURL(t *testing.T, defaultInSubDir, addLanguage, multilingual bool, lang string) {
|
||||
viper.Reset()
|
||||
viper.Set("Multilingual", multilingual)
|
||||
viper.Set("CurrentContentLanguage", NewLanguage(lang))
|
||||
viper.Set("DefaultContentLanguage", "en")
|
||||
viper.Set("DefaultContentLanguageInSubdir", defaultInSubDir)
|
||||
|
||||
tests := []struct {
|
||||
input string
|
||||
@@ -146,7 +160,11 @@ func doTestRelURL(t *testing.T, addLanguage, multilingual bool, lang string) {
|
||||
|
||||
expected := test.expected
|
||||
if multilingual && addLanguage {
|
||||
expected = strings.Replace(expected, "MULTI", "/"+lang, 1)
|
||||
if !defaultInSubDir && lang == "en" {
|
||||
expected = strings.Replace(expected, "MULTI", "", 1)
|
||||
} else {
|
||||
expected = strings.Replace(expected, "MULTI", "/"+lang, 1)
|
||||
}
|
||||
} else {
|
||||
expected = strings.Replace(expected, "MULTI", "", 1)
|
||||
}
|
||||
|
Reference in New Issue
Block a user