diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2016-09-13 23:44:16 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2016-09-13 23:44:16 +0200 |
commit | 74ffb45fbe2c121881b2386fc3210f8b1c6bd952 (patch) | |
tree | e35dffb4d68825375e9f141d4fbc0db85c05fe66 /helpers/url.go | |
parent | f53145b2748067a9d50ab57efc6423d7568c237c (diff) |
helpers: Improve the language prefix logic
See #2444
Diffstat (limited to 'helpers/url.go')
-rw-r--r-- | helpers/url.go | 52 |
1 files changed, 28 insertions, 24 deletions
diff --git a/helpers/url.go b/helpers/url.go index 1bc6042e6..49f670da2 100644 --- a/helpers/url.go +++ b/helpers/url.go @@ -169,20 +169,22 @@ func AbsURL(in string, addLanguage bool) string { if addLanguage { prefix := getLanguagePrefix() - hasPrefix := false - // avoid adding language prefix if already present - if strings.HasPrefix(in, "/") { - hasPrefix = strings.HasPrefix(in[1:], prefix) - } else { - hasPrefix = strings.HasPrefix(in, prefix) - } + if prefix != "" { + hasPrefix := false + // avoid adding language prefix if already present + if strings.HasPrefix(in, "/") { + hasPrefix = strings.HasPrefix(in[1:], prefix) + } else { + hasPrefix = strings.HasPrefix(in, prefix) + } - if prefix != "" && !hasPrefix { - addSlash := in == "" || strings.HasSuffix(in, "/") - in = path.Join(prefix, in) + if !hasPrefix { + addSlash := in == "" || strings.HasSuffix(in, "/") + in = path.Join(prefix, in) - if addSlash { - in += "/" + if addSlash { + in += "/" + } } } } @@ -231,21 +233,23 @@ func RelURL(in string, addLanguage bool) string { if addLanguage { prefix := getLanguagePrefix() - hasPrefix := false - // avoid adding language prefix if already present - if strings.HasPrefix(in, "/") { - hasPrefix = strings.HasPrefix(in[1:], prefix) - } else { - hasPrefix = strings.HasPrefix(in, prefix) - } + if prefix != "" { + hasPrefix := false + // avoid adding language prefix if already present + if strings.HasPrefix(in, "/") { + hasPrefix = strings.HasPrefix(in[1:], prefix) + } else { + hasPrefix = strings.HasPrefix(in, prefix) + } - if prefix != "" && !hasPrefix { - hadSlash := strings.HasSuffix(u, "/") + if !hasPrefix { + hadSlash := strings.HasSuffix(u, "/") - u = path.Join(prefix, u) + u = path.Join(prefix, u) - if hadSlash { - u += "/" + if hadSlash { + u += "/" + } } } } |