helpers: Improve the language prefix logic
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 13 Sep 2016 21:44:16 +0000 (23:44 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 13 Sep 2016 21:44:16 +0000 (23:44 +0200)
See #2444

helpers/url.go

index 1bc6042e6179c1750dbc6e5bdf2710126108c8ee..49f670da2db6c51eb4f7df7fe29f17f4b1d0e8ea 100644 (file)
@@ -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 += "/"
+                               }
                        }
                }
        }