?
avatar mavrosxristoforos
mavrosxristoforos
19 Nov 2020

Current Procedure

Currently, to translate any Javascript string, we use the combination of JText::script() in PHP and Joomla.JText._() in Javascript. However, it's common that we may want to include multiple strings to translate in Javascript. Currently, to do that, you have to write:

  $language_strings = array('COM_EXAMPLE_STRING', 'COM_EXAMPLE_ANOTHER_STRING');
  foreach($language_strings as $string) {
    JText::script($string);
  }

Desired Procedure

This could be easily simplified to this:

  $language_strings = array('COM_EXAMPLE_STRING', 'COM_EXAMPLE_ANOTHER_STRING');
  JText::script($language_strings);

Suggested change

This could be avoided, if the script function allowed arrays as input. A simple if (is_array($string)) and a loop could do this.

avatar mavrosxristoforos mavrosxristoforos - open - 19 Nov 2020
avatar joomla-cms-bot joomla-cms-bot - labeled - 19 Nov 2020
avatar mavrosxristoforos mavrosxristoforos - change - 19 Nov 2020
The description was changed
avatar mavrosxristoforos mavrosxristoforos - edited - 19 Nov 2020
avatar dgrammatiko
dgrammatiko - comment - 19 Nov 2020

The proposed solution is half baked as the API has 3 arguments and the proposed one is ignoring the last 2. But it could be like:

$texts = [
	'COM_EXAMPLE_STRING' => [
		'jsSafe' => true,
		'interpretBackSlashes' => true
	],
	'COM_EXAMPLE_STRING_NEXT' => [
		'jsSafe' => false,
		'interpretBackSlashes' => false
	],
	// Or with defaults
	'COM_EXAMPLE_STRING_WITH_DEFAULTS' => [],
];

My 2c

avatar mavrosxristoforos
mavrosxristoforos - comment - 19 Nov 2020

@dgrammatiko I intentionally omitted the other arguments, because providing an array of arrays doesn't necessarily simplify the input. Instead, you could just call the JText::script() function with an array of strings and have the other two parameters apply for the whole array. That makes sense, because if you want some strings to have jsSafe: true and others to have jsSafe: false, you can just call JText::script() once for every different set of strings.

avatar venomdev
venomdev - comment - 21 Nov 2020

Is it possible to do something simple like

function script($string, ...) {

    if(is_array($string)) {

        array_map($string, script);
    }
}

avatar rdeutz rdeutz - change - 20 Dec 2020
Status New Closed
Closed_Date 0000-00-00 00:00:00 2020-12-20 13:50:58
Closed_By rdeutz
avatar rdeutz rdeutz - close - 20 Dec 2020

Add a Comment

Login with GitHub to post a comment