Skip to content

Commit

Permalink
Fixed non translatable characters replace
Browse files Browse the repository at this point in the history
  • Loading branch information
damianz5 committed Jul 18, 2018
1 parent 33f47c7 commit a1b6a04
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions lib/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function __construct(ContentTypeService $contentTypeService, ConfigResolv
'ez_platform_automated_translation'
);

$this->nonTranslatableTags = ['ezembed'] + $tags;
$this->nonTranslatableTags = ['ezvalue', 'ezconfig', 'ezembed'] + $tags;
$this->nonTranslatableCharactersHashMap = ["\n" => 'XXXEOLXXX'] + $chars;
$this->nonValidAttributeTags = ['title'] + $attributes;
}
Expand Down Expand Up @@ -211,12 +211,7 @@ private function richTextEncode(RichTextValue $value): string
{
$xmlString = (string) $value;
$xmlString = substr($xmlString, strpos($xmlString, '>') + 1);
$xmlString = str_replace(
array_keys($this->nonTranslatableCharactersHashMap),
array_values($this->nonTranslatableCharactersHashMap),
$xmlString
);

$xmlString = $this->encodeNonTranslatableCharacters($xmlString);
foreach ($this->nonTranslatableTags as $tag) {
$xmlString = preg_replace_callback(
'#<' . $tag . '(.[^>]*)>(.*)</' . $tag . '>#Uuim',
Expand Down Expand Up @@ -253,12 +248,8 @@ function ($matches) use ($tag) {
*/
private function richTextDecode(string $value): string
{
$value = str_replace(
array_values($this->nonTranslatableCharactersHashMap),
array_keys($this->nonTranslatableCharactersHashMap),
$value
);
foreach ($this->nonTranslatableTags as $tag) {
$value = $this->decodeNonTranslatableCharacters($value);
foreach (array_reverse($this->nonTranslatableTags) as $tag) {
$value = preg_replace_callback(
'#<' . $tag . '>(.*)</' . $tag . '>#Uuim',
function ($matches) {
Expand All @@ -277,7 +268,36 @@ function ($matches) {
);
$value = str_replace("</fake{$tag}>", "</{$tag}>", $value);
}
$value = $this->decodeNonTranslatableCharacters($value);

return $value;
}

/**
* @param string $value
*
* @return string
*/
private function encodeNonTranslatableCharacters(string $value): string
{
return str_replace(
array_keys($this->nonTranslatableCharactersHashMap),
array_values($this->nonTranslatableCharactersHashMap),
$value
);
}

/**
* @param string $value
*
* @return string
*/
private function decodeNonTranslatableCharacters(string $value): string
{
return str_replace(
array_values($this->nonTranslatableCharactersHashMap),
array_keys($this->nonTranslatableCharactersHashMap),
$value
);
}
}

0 comments on commit a1b6a04

Please sign in to comment.