Request Quote

Translate language Tag in external JavaScript file

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.

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
COM_CUSTOM_LANGUAGE_TAG=”We Are Webkul”

Step2 :

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

JText::script(‘COM_CUSTOM_LANGUAGE_TAG’);

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.
<?php
/**
 * Webkul Software.
 *
 * @category Webkul
 * @author Webkul
 * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
 * @license https://store.webkul.com/license.html
 */
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;
	     }
             $this->prepareDocument();
             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                
                JText::script('COM_CUSTOM_LANGUAGE_TAG');
                $document=JFactory::getDocument();
                $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));
		}
		else
		{
			$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'));
		}

		$this->document->setTitle($title);
		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'));
	}

});

Support:

For any query regarding Joomla virtuemart Extensions and add-ons you can communicate with us at:
support@webkul.com

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


. . .

Comment

Add Your Comment

Be the first to comment.

css.php
Hire Us!
Brief us about your requirements and we'll get back to you.
Woo! Hooy!
We have just recieved your project brief and our expert will contact you shortly.
Send Again
Close

Index