I want the button (facebook) to be displayed in different languages, tried as follows:

if(window.location.href == '/lv/'){ js.src = "//connect.facebook.net/lv_LV/sdk.js#xfbml=1&version=v2.6&appId=491659391035911"; }else if(/ru/.test(window.location.href)){ js.src = "//connect.facebook.net/ru_RU/sdk.js#xfbml=1&version=v2.6&appId=491659391035911"; }else if(/lt/.test(window.location.href)){ js.src = "//connect.facebook.net/lt_LT/sdk.js#xfbml=1&version=v2.6&appId=491659391035911"; }else if(/et/.test(window.location.href)){ js.src = "//connect.facebook.net/et_EE/sdk.js#xfbml=1&version=v2.6&appId=491659391035911"; }else{ js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.6&appId=491659391035911"; } 

This is not working correctly, for example, instead of English, Lithuanian displays, advise how to do it right?

  • First, look at what you have in window.location.href . Most likely you need location.pathname , but even in this case it’s impossible to check the exact match is pretty stupid. - Alexey Ten
  • Can't Facebook detect it by itself? - blits
  • site.ru/ru/postname something like facebook not detecting itself - Alexander Reizan

1 answer 1

While we do not know what you have in window.location.href is window.location.href shooting.

 //var url = window.location.href ; //url = "www/ru/index.html?some=param"; // Для примера //url = "www/lv/index.html?some=param"; url = "www/et/index.html?some=param"; if(new RegExp("site[.]ru\/lv\/").test(url)){ alert("lv"); }else if(new RegExp("site[.]ru\/ru\/").test(url)){ alert("ru"); }else if(new RegExp("site[.]ru\/lt\/").test(url)){ alert("lt"); }else if(new RegExp("site[.]ru\/et\/").test(url)){ alert("et"); }else{ alert("else"); } 

Play here , click on the Run button at the top