1. Fabrik 3.9 has been released. If you have updated Joomla to 3.9, this is a required update.
    Dismiss Notice

get the current joomla language in javascript

Discussion in 'Community' started by mirceat, Oct 12, 2018.

  1. mirceat

    mirceat Member

    Level: Community

    i searched the internet and couldn't find a proper solution for getting the current language tag within a javascript code. I need to display a javascript confirmation message if the current language value doesn't match the recorded database value.

    So far i tried this code onload, but i can't get the lang variable, in console log i see the php text instead of language value:

    Code (Javascript):
    var lang = "<?php echo JFactory::getLanguage()->getTag();?>";
    there is another way to get the language value in javascript?

    Thank you
  2. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Unfortunately I don't think the language tag is included in the standard Joomla JS options.

    And assuming by "I tried this code onload" you mean in a Fabrik element, then no, that won't work, as we don't eval that, so the PHP won't get executed.

    You'll need to add it yourself, either in a custom Fabrik template, or maybe in your site template:

    Code (Text):

    JFactory::getDocument()->addScriptOptions('myOptions', array('lang' => JFactory::getLanguage()->getTag()));
    ... and then in your JS code you can do ...

    Code (Text):

    var myOptions = Joomla.getOptions('myOptions');
    // ... and the tag will be in myOptions.lang
    If you don't want to edit templates, and if you are doing this in a Fabrik form, you could put that line of code in a form PHP plugin, running onLoad. Or if it's a list, you could add a list php_events plugin, again running onLoad.

    -- hugh

Share This Page