Back to Top

Translate language Tag in external JavaScript file

Updated 27 December 2017

Translate language Tag in external JavaScript file: Joomla CMS is used by people from different countries with different language. Most joomla extension developer need to keep their joomla extension compatible for translation. Joomla support language files for translation string so it’s not a big deal. Again, it’s not like we always use the translation strings in xml(joomla form api xml) and php scripts but sometimes need to use them in Javascript. Adding translation in javascript in joomla is bit tricky.

Which will give you matching translation in current language. This way you can keep JS and PHP in separate files.

core.js is play important role to translate language constant.

Location: YOUR_SITE/media/system/js/

core.js defines the Joomla namespace for JavaScript functions and also defines the Joomla.JText method.
Previously this was loaded together with the MooTools framework and it also depended on it.

Start your headless eCommerce
Find out More

Core.js was rewritten to jQuery after 3.4 and thus we can now load it without loading MooTools. That’s what JHtmlBehavior::core() which also can load by JHml::_(‘behavior.framework’)

As we know joomla component use MVC design pattern so all presentation part of component in view.

We will follow 3 easy step for better understanding.

Step 1 :

 Write the translation string in .ini language file as we do always

Step2 :

Declare translation string in .php script to add it in a global js object.


Step 3 :

In any external js file loaded from joomla you can now use the string as, make sure that you load the js after you load the translation string in step2

Joomla.JText._(‘COM_CUSTOM_LANGUAGE_TAG’);//where you want display in js file.

Explain With Code:

View file

We can write code in any files of views here I am writing view.html.php of view. I think prepareDocument it is best part of view to write this code.
 * Webkul Software.
 * @category Webkul
 * @author Webkul
 * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (
 * @license
defined('_JEXEC') or die;

 * Registration view class for Test.
class TestViewRegistration extends JViewLegacy
	protected $data;
	protected $form;
	protected $params;
	protected $state;
	public $document;
	 * Method to display the view.
	 * @param   string  $tpl  The template file to include
	 * @return  mixed
	 * @since   1.6
	public function display($tpl = null)
	{    $this->data   = $this->get('Data');
	     $this->form   = $this->get('Form');
	     $this->state  = $this->get('State');
	     $this->params = $this->state->get('params');
		// Check for errors.
	     if (count($errors = $this->get('Errors'))) {
		JError::raiseError(500, implode('<br />', $errors));
		return false;
             return parent::display($tpl);

	 * Prepares the document.
	 * @return  void
	 * @since   1.6
	protected function prepareDocument()
		$app   = JFactory::getApplication();
		$menus = $app->getMenu();
		$title = null;
                //We are writing language tag here                
                $document->addScript('test.js');//Path of js file
		// Because the application sets a default page title,
		// we need to get it from the menu item itself
		$menu = $menus->getActive();

		if ($menu)
			$this->params->def('page_heading', $this->params->get('page_title', $menu->title));
			$this->params->def('page_heading', JText::_('COM_TEST_REGISTRATION'));

		$title = $this->params->get('page_title', '');

		if (empty($title))
			$title = $app->get('sitename');
		elseif ($app->get('sitename_pagetitles', 0) == 1){
			$title = JText::sprintf('JPAGETITLE', $app->get('sitename'), $title);
		elseif ($app->get('sitename_pagetitles', 0) == 2)
			$title = JText::sprintf('JPAGETITLE', $title, $app->get('sitename'));

		if ($this->params->get('robots'))
		    $this->document->setMetadata('robots', $this->params->get('robots'));

JS File

test.js(external js)//here language tag will be prepend on webkul css class with translated language code
jQuery(function() {
	if (typeof Joomla == 'undefined') {
		Joomla = {};
	var elements = document.getElementsByClassName("webkul"); // get all elements with class 'webkul'
	for (var i = 0; i < elements.length; i++) { // for every element with class 'webkul':
		elements[i].insertAdjacentHTML("afterbegin", Joomla.JText._('COM_CUSTOM_LANGUAGE_TAG'));



For any query regarding Joomla virtuemart Extensions and add-ons you can communicate with us at:
[email protected]

Please visit this link to find more extensions of joomla Webkul Joomla extensions

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*

Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home

Table of Content