1

So, I'm trying to modify the Facebook Devkit script that is in my index.php to change depending the on the website's active language, but I'm not sure how to access the active language variable. I have tried both var lang ="<?php echo $lang; ?>";which produces empty quotes, and var lang = document.documentElement.lang just get me the browser language, not the active language on the site.

I know I could do this with a language specific template and just change the language value manually, but I don't foresee doing any other language specific stuff at the style or template level, so I think a quick regex fix would be better.

Update: If anyone is interested here's the code I used. Not pretty, but it works for my use case:

<script>(function(d, s, id) {
  var lang = "<?php echo JFactory::getLanguage()->getTag();?>";  
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  if (lang !="en-GB"){
  js.src = "//connect.facebook.net/de_DE/sdk.js#xfbml=1&version=v2.8";
  } else {
  js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.8";   
  }

  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
1

$lang, which I assume in your case derives from $lang = JFactory::getLanguage(); will give you the entire language object. From this object, you can then access one of it's properties.

So to get the language tag, you can use:

<script>
    var lang = '<?php echo JFactory::getLanguage()->getTag(); ?>';
</script>

This will output:

var lang = 'en-GB';
  • worked like a charm...now the regex! – nizz0k Feb 3 '17 at 11:44

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.