Skip to content

Commit

Permalink
Fix problems with displayed encoded entities in javascript translatio…
Browse files Browse the repository at this point in the history
…ns (#21322)

* Try to avoid displaying encoded entities in javascript translations

* updates expected UI test files
  • Loading branch information
sgiehl authored Oct 12, 2023
1 parent e83ebd2 commit 2c878f4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
28 changes: 27 additions & 1 deletion core/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public function getJavascriptTranslations()
$clientSideTranslations = array();
foreach ($this->getClientSideTranslationKeys() as $id) {
[$plugin, $key] = explode('_', $id, 2);
$clientSideTranslations[$id] = $this->getTranslation($id, $this->currentLanguage, $plugin, $key);
$clientSideTranslations[$id] = $this->decodeEntitiesSafeForHTML($this->getTranslation($id, $this->currentLanguage, $plugin, $key));
}

$js = 'var translations = ' . json_encode($clientSideTranslations) . ';';
Expand All @@ -208,6 +208,32 @@ public function getJavascriptTranslations()
return $js;
}

/**
* Decodes all entities in the given string except of > and <
*
* @param string $text
* @return string
*/
private function decodeEntitiesSafeForHTML(string $text): string
{
// replace encoded html tag entities, as they need to remain encoded
$text = str_replace(
['>', '<'],
['###gt###', '###lt###'],
$text
);

// decode all remaining entities
$text = html_entity_decode($text);

// recover encoded html tag entities
return str_replace(
['###gt###', '###lt###'],
['>', '<'],
$text
);
}

/**
* Returns the list of client side translations by key. These translations will be outputted
* to the translation JavaScript.
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2c878f4

Please sign in to comment.