How to force a view to load other modules languages into SUGAR.language?

sugarcrmdevelopers —  June 11, 2012

SugarCRMAs you should know each SugarCRM view extends SugarView class which has a nifty method called _displayJavascript()  who is responsible for including all needed JavaScript files.

When it comes to labels, by default, this function only loads current module labels into SUGAR.language.

These labels are acessible through the following command:

SUGAR.language.get('<ModuleName>', 'LBL_<LabelName>');

In order to add support to other module labels to be included, you should override _displayJavascript() method in your modules view, and specify which other modules labels should be included.

// modules/<ModuleName>/views/view.edit.php

require_once 'include/MVC/View/views/view.edit.php';

class <ModuleName>ViewEdit extends ViewEdit
{
    protected function _displayJavascript()
    {
        parent::_displayJavascript();

        if (!$this->_getOption('show_javascript')) {
            return;
        }

        $modules = array(
            '<Module1Name>',
            '<Module2Name>',
        );

        foreach ($modules as $module) {
            if (!is_file($GLOBALS['sugar_config']['cache_dir'] . 'jsLanguage/' . $module . '/' . $GLOBALS['current_language'] . '.js')) {
                require_once 'include/language/jsLanguage.php';
                jsLanguage::createModuleStringsCache($module, $GLOBALS['current_language']);
            }
            echo 'http://'%20.%20$GLOBALS%5B'sugar_config'%5D%5B'cache_dir'%5D%20.%20'jsLanguage/'%20.%20$module%20.%20'/'%20.%20$GLOBALS%5B'current_language'%5D%20.%20'.js?s='%20.%20$GLOBALS%5B'js_version_key'%5D%20.%20'&c='%20.%20$GLOBALS%5B'sugar_config'%5D%5B'js_custom_version'%5D%20.%20'&j='%20.%20$GLOBALS%5B'sugar_config'%5D%5B'js_lang_version'%5D%20.%20'';
        }
    }
}