Skip to content

Commit

Permalink
Merge pull request #8 from damianz5/content_separation_for_translation
Browse files Browse the repository at this point in the history
Fixed tags separation (ezembed) for Encoder
  • Loading branch information
damianz5 authored Aug 16, 2018
2 parents ed98546 + a1b6a04 commit c2010df
Show file tree
Hide file tree
Showing 9 changed files with 374 additions and 19 deletions.
58 changes: 39 additions & 19 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 @@ -207,19 +207,14 @@ public function decode(string $xml): array
*
* @return string
*/
public function richTextEncode(RichTextValue $value): string
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 . '>#uim',
'#<' . $tag . '(.[^>]*)>(.*)</' . $tag . '>#Uuim',
function ($matches) use ($tag) {
$hash = sha1($matches[0]);
$this->placeHolderMap[$hash] = $matches[0];
Expand All @@ -231,7 +226,7 @@ function ($matches) use ($tag) {
}
foreach ($this->nonValidAttributeTags as $tag) {
$xmlString = preg_replace_callback(
'#<' . $tag . '(.[^>]*)>#uim',
'#<' . $tag . '(.[^>]*)>#Uuim',
function ($matches) use ($tag) {
$hash = sha1($matches[0]);
$this->placeHolderMap[$hash] = $matches[0];
Expand All @@ -251,16 +246,12 @@ function ($matches) use ($tag) {
*
* @return string
*/
public function richTextDecode(string $value): string
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 . '>#uim',
'#<' . $tag . '>(.*)</' . $tag . '>#Uuim',
function ($matches) {
return $this->placeHolderMap[trim($matches[1])];
},
Expand All @@ -269,15 +260,44 @@ function ($matches) {
}
foreach ($this->nonValidAttributeTags as $tag) {
$value = preg_replace_callback(
'#<fake' . $tag . '(.[^>]*)>#uim',
'#<fake' . $tag . '(.[^>]*)>#Uuim',
function ($matches) {
return $this->placeHolderMap[trim($matches[1])];
},
$value
);
$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
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0"?>
<response><field_1_richtext type="eZ\Publish\Core\FieldType\RichText\Value"><fakecdata>XXXEOLXXX<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">XXXEOLXXX <para>Mexican cuisine is primarily a fusion of indigenous Mesoamerican cooking with European, especially Spanish.</para>XXXEOLXXX</section>XXXEOLXXX</fakecdata></field_1_richtext><field_2_richtext type="eZ\Publish\Core\FieldType\RichText\Value"><fakecdata>XXXEOLXXX<section xmlns="http://ez.no/namespaces/ezpublish5/xhtml5/edit">XXXEOLXXX <p>Mexican cuisine is primarily a fusion of indigenous Mesoamerican cooking with European, especially Spanish. TheXXXEOLXXX basic staples are native foods such as corn, beans and chili peppers.</p>XXXEOLXXX <h2>Main Features</h2>XXXEOLXXX <div data-ezelement="ezembed" data-href="ezcontent://192" data-ezview="embed" class="ez-embed-type-image" data-ezalign="right"><span data-ezelement="ezconfig"><span data-ezelement="ezvalue" data-ezvalue-key="size">large</span> </span></div>XXXEOLXXX <p>The ability to cook well, called "saz&#xF3;n" (lit. seasoning) is considered to be a gift generally gained fromXXXEOLXXX experience and a sense of commitment to the diners.</p>XXXEOLXXX <div data-ezelement="ezembed" data-href="ezcontent://191" data-ezview="embed" class="ez-embed-type-image" data-ezalign="left"><span data-ezelement="ezconfig"><span data-ezelement="ezvalue" data-ezvalue-key="size">large</span> </span></div>XXXEOLXXX <p>Mexican regional home cooking is completely different from the food served in most Mexican restaurants outsideXXXEOLXXX Mexico, which is usually some variety of Tex-Mex.</p>XXXEOLXXX</section>XXXEOLXXX</fakecdata></field_2_richtext></response>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml"
xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
<para>Mexican cuisine is primarily a fusion of indigenous Mesoamerican cooking with European, especially Spanish.</para>
</section>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<section xmlns="http://ez.no/namespaces/ezpublish5/xhtml5/edit">
<p>Mexican cuisine is primarily a fusion of indigenous Mesoamerican cooking with European, especially Spanish. The
basic staples are native foods such as corn, beans and chili peppers.</p>
<h2>Main Features</h2>
<div data-ezelement="ezembed" data-href="ezcontent://192" data-ezview="embed" class="ez-embed-type-image"
data-ezalign="right"><span data-ezelement="ezconfig"><span data-ezelement="ezvalue"
data-ezvalue-key="size">large</span> </span></div>
<p>The ability to cook well, called "sazón" (lit. seasoning) is considered to be a gift generally gained from
experience and a sense of commitment to the diners.</p>
<div data-ezelement="ezembed" data-href="ezcontent://191" data-ezview="embed" class="ez-embed-type-image"
data-ezalign="left"><span data-ezelement="ezconfig"><span data-ezelement="ezvalue"
data-ezvalue-key="size">large</span> </span></div>
<p>Mexican regional home cooking is completely different from the food served in most Mexican restaurants outside
Mexico, which is usually some variety of Tex-Mex.</p>
</section>
2 changes: 2 additions & 0 deletions tests/fixtures/testEncodeTwoRichText_expectedEncodeResult.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0"?>
<response><field_1_richtext type="eZ\Publish\Core\FieldType\RichText\Value"><fakecdata>XXXEOLXXX<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">XXXEOLXXX <para>XXXEOLXXX Mexican cuisine is primarily a fusion of indigenous Mesoamerican cooking with European, especially Spanish.XXXEOLXXX </para>XXXEOLXXX</section>XXXEOLXXX</fakecdata></field_1_richtext><field_2_richtext type="eZ\Publish\Core\FieldType\RichText\Value"><fakecdata>XXXEOLXXX<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">XXXEOLXXX <para>XXXEOLXXX Mexican cuisine is primarily a fusion of indigenous Mesoamerican cooking with European, especially Spanish.XXXEOLXXX </para>XXXEOLXXX <para>File 2</para>XXXEOLXXX</section>XXXEOLXXX</fakecdata></field_2_richtext></response>
7 changes: 7 additions & 0 deletions tests/fixtures/testEncodeTwoRichText_field1_richtext.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml"
xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
<para>
Mexican cuisine is primarily a fusion of indigenous Mesoamerican cooking with European, especially Spanish.
</para>
</section>
8 changes: 8 additions & 0 deletions tests/fixtures/testEncodeTwoRichText_field2_richtext.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml"
xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0">
<para>
Mexican cuisine is primarily a fusion of indigenous Mesoamerican cooking with European, especially Spanish.
</para>
<para>File 2</para>
</section>
2 changes: 2 additions & 0 deletions tests/fixtures/testEncodeTwoTextline_expectedEncodeResult.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0"?>
<response><field_1_textline type="eZ\Publish\Core\FieldType\TextLine\Value">Some text 1</field_1_textline><field_2_textline type="eZ\Publish\Core\FieldType\TextLine\Value">Some text 2</field_2_textline></response>
Loading

0 comments on commit c2010df

Please sign in to comment.