Skip to content

Commit

Permalink
Joomla.JText uses scriptOptions 'joomla.jtext'. Fixes #11690
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedik authored and wilsonge committed Sep 4, 2016
1 parent 28bd41d commit 5e54494
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 33 deletions.
29 changes: 0 additions & 29 deletions libraries/joomla/document/renderer/html/head.php
Expand Up @@ -268,35 +268,6 @@ public function fetchHead($document)
$buffer .= $tab . '</script>' . $lnEnd;
}

// Generate script language declarations.
if (count(JText::script()))
{
$buffer .= $tab . '<script';

if (!$document->isHtml5())
{
$buffer .= ' type="text/javascript"';
}

$buffer .= '>' . $lnEnd;

if ($document->_mime != 'text/html')
{
$buffer .= $tab . $tab . '//<![CDATA[' . $lnEnd;
}

$buffer .= $tab . $tab . '(function() {' . $lnEnd;
$buffer .= $tab . $tab . $tab . 'Joomla.JText.load(' . json_encode(JText::script()) . ');' . $lnEnd;
$buffer .= $tab . $tab . '})();' . $lnEnd;

if ($document->_mime != 'text/html')
{
$buffer .= $tab . $tab . '//]]>' . $lnEnd;
}

$buffer .= $tab . '</script>' . $lnEnd;
}

// Output the custom tags - array_unique makes sure that we don't output the same tags twice
foreach (array_unique($document->_custom) as $custom)
{
Expand Down
3 changes: 3 additions & 0 deletions libraries/joomla/language/text.php
Expand Up @@ -399,6 +399,9 @@ public static function script($string = null, $jsSafe = false, $interpretBackSla

// Load core.js dependency
JHtml::_('behavior.core');

// Update Joomla.JText script options
JFactory::getDocument()->addScriptOptions('joomla.jtext', static::$strings, false);
}

return static::getScriptStrings();
Expand Down
34 changes: 32 additions & 2 deletions media/system/js/core-uncompressed.js
Expand Up @@ -58,15 +58,45 @@ Joomla.editors.instances = Joomla.editors.instances || {};
* Allows you to call Joomla.JText._() to get a translated JavaScript string pushed in with JText::script() in Joomla.
*/
Joomla.JText = {
strings: {},
strings: {},

/**
* Translates a string into the current language.
*
* @param {String} key The string to translate
* @param {String} def Default string
*
* @returns {String}
*/
'_': function( key, def ) {
return typeof this.strings[ key.toUpperCase() ] !== 'undefined' ? this.strings[ key.toUpperCase() ] : def;

// Check for new strings in the optionsStorage, and load them
var newStrings = Joomla.getOptions('joomla.jtext');
if ( newStrings ) {
this.load(newStrings);

// Clean up the optionsStorage from useless data
Joomla.loadOptions({'joomla.jtext': null});
}

def = def === undefined ? '' : def;
key = key.toUpperCase();

return this.strings[ key ] !== undefined ? this.strings[ key ] : def;
},

/**
* Load new strings in to Joomla.JText
*
* @param {Object} object Object with new strings
* @returns {Joomla.JText}
*/
load: function( object ) {
for ( var key in object ) {
if (!object.hasOwnProperty(key)) continue;
this.strings[ key.toUpperCase() ] = object[ key ];
}

return this;
}
};
Expand Down
2 changes: 1 addition & 1 deletion media/system/js/core.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion tests/javascript/core/fixtures/fixture.html
Expand Up @@ -40,7 +40,11 @@
<script type="application/json" class="joomla-script-options new">{
"com_foobar": ["my options"],
"com_foobar2": "Alert message!",
"com_foobar3": false
"com_foobar3": false,
"joomla.jtext": {
"string1": "String 1",
"STRING2": "String 2"
}
}</script>
</div>
</div>
9 changes: 9 additions & 0 deletions tests/javascript/core/spec.js
Expand Up @@ -65,6 +65,15 @@ define(['jquery', 'testsRoot/core/spec-setup', 'jasmineJquery'], function ($) {
it('should return \'test\' on calling Joomla.JText._(\'JTOGGLE_REMOVE_SIDEBAR\', \'test\')', function () {
expect(Joomla.JText._('JTOGGLE_REMOVE_SIDEBAR', 'test')).toEqual('test');
});

// Test strings in optionsStorage
it('should return \'String 1\' on calling Joomla.JText._(\'stRing1\')', function () {
expect(Joomla.JText._('stRing1')).toEqual('String 1');
});
it('should return \'String 2\' on calling Joomla.JText._(\'StrinG2\')', function () {
expect(Joomla.JText._('StrinG2')).toEqual('String 2');
});

});

describe('Core Joomla.replaceTokens', function () {
Expand Down

0 comments on commit 5e54494

Please sign in to comment.