现在我有url例如
/en/page
/cs/stranka
假设EN是我的默认语言。
有没有办法获得以下内容?
/page // without en in url for EN
/cs/stranka
任何建议表示赞赏。
答案 0 :(得分:1)
你可以使用这个 formatUrl(yourUrl,yourLanguageToRemove)功能:
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