diff options
author | Bjørn Erik Pedersen <[email protected]> | 2016-08-08 13:55:18 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2016-09-06 18:32:18 +0300 |
commit | 8da040342eb0a3098e54dc6ed2cb10bac6173230 (patch) | |
tree | 6ebd4750103cb1ac8a2b1aa6ec4bdb8e85d46ea8 /helpers | |
parent | d953e39e63deac608e7f1442a6c0d5b2fe56180e (diff) | |
download | hugo-8da040342eb0a3098e54dc6ed2cb10bac6173230.tar.gz hugo-8da040342eb0a3098e54dc6ed2cb10bac6173230.zip |
Render main content language in root by default
Fixes #2312
Diffstat (limited to 'helpers')
-rw-r--r-- | helpers/language.go | 4 | ||||
-rw-r--r-- | helpers/url.go | 32 | ||||
-rw-r--r-- | helpers/url_test.go | 44 |
3 files changed, 58 insertions, 22 deletions
diff --git a/helpers/language.go b/helpers/language.go index 4ef5edbc4..994129308 100644 --- a/helpers/language.go +++ b/helpers/language.go @@ -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{})} } diff --git a/helpers/url.go b/helpers/url.go index 83273324d..4d06fb042 100644 --- a/helpers/url.go +++ b/helpers/url.go @@ -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 += "/" + } } } diff --git a/helpers/url_test.go b/helpers/url_test.go index f6dd9f9da..d1fadb252 100644 --- a/helpers/url_test.go +++ b/helpers/url_test.go @@ -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) } |