How can I remove lang from url for default language

276 Views Asked by At

Now I have url for example

/en/page
/cs/stranka

Let's say that EN is my default language.

Is there any way to get the following?

/page // without en in url for EN
/cs/stranka

Any suggestions appreciated.

1

There are 1 best solutions below

4
On

you can use this formatUrl(yourUrl, yourLanguageToRemove) function:

function formatUrl(url, language){
 var result = url;
 var indexOfLanguage = url.indexOf(language);
 if(indexOfLanguage> -1){
  result = url.substring(0, indexOfLanguage);
  if( url[indexOfLanguage+language.length] === "/" ){
   result += url.substring(indexOfLanguage+language.length+1, url.length);
  }else{
   result += url.substring(indexOfLanguage+language.length, url.length);
  }
 }
 return result;
}
   
var expl1 = formatUrl("http://domain.com/en/page1", "en");
console.log(expl1);
//outputs: http://domain.com/page1
   
var expl2 = formatUrl("http://domain.com/category6/en/page2", "en");
console.log(expl2);
//outputs: http://domain.com/category6/page2
   
var expl3 = formatUrl("http://domain.com/category6/en", "en");
console.log(expl3);
//outputs: http://domain.com/category6/
   
var expl4 = formatUrl("http://domain.com/category6/cs/stranka", "en");
console.log(expl4);
//outputs: http://domain.com/category6/cs/stranka