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 :
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.
View file
/** * 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
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:
[email protected]
Please visit this link to find more extensions of joomla Webkul Joomla extensions
Be the first to comment.