Skip to content

Commit

Permalink
Google just removes <title> tag, fix by faking it
Browse files Browse the repository at this point in the history
  • Loading branch information
Plopix committed Apr 6, 2018
1 parent 46a137e commit e6875b4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ public function load(array $configs, ContainerBuilder $container): void
$processor->mapSetting('configurations', $config);
$processor->mapSetting('nontranslatablecharacters', $config);
$processor->mapSetting('nontranslatabletags', $config);
$processor->mapSetting('nonnalidattributetags', $config);
}
}
1 change: 1 addition & 0 deletions bundle/Resources/config/default_settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ parameters:
ez_platform_automated_translation.default.configurations: ~
ez_platform_automated_translation.default.nontranslatablecharacters: []
ez_platform_automated_translation.default.nontranslatabletags: []
ez_platform_automated_translation.default.nonnalidattributetags: []

36 changes: 36 additions & 0 deletions lib/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ class Encoder
*/
private $nonTranslatableTags;

/**
* Every attributes inside these tags must be preserve from translation.
*
* @var array
*/
private $nonValidAttributeTags;

/**
* @var ContentTypeService
*/
Expand Down Expand Up @@ -113,8 +120,14 @@ public function __construct(ContentTypeService $contentTypeService, ConfigResolv
'ez_platform_automated_translation'
);

$attributes = $configResolver->getParameter(
'nonnalidattributetags',
'ez_platform_automated_translation'
);

$this->nonTranslatableTags = ['ezembed'] + $tags;
$this->nonTranslatableCharactersHashMap = ["\n" => 'XXXEOLXXX'] + $chars;
$this->nonValidAttributeTags = ['title'] + $attributes;
}

/**
Expand Down Expand Up @@ -214,6 +227,19 @@ function ($matches) use ($tag) {
$xmlString
);
}
foreach ($this->nonValidAttributeTags as $tag) {
$xmlString = preg_replace_callback(
'#<' . $tag . '(.[^>]*)>#uim',
function ($matches) use ($tag) {
$hash = sha1($matches[0]);
$this->placeHolderMap[$hash] = $matches[0];

return "<fake{$tag} {$hash}>";
},
$xmlString
);
$xmlString = str_replace("</{$tag}>", "</fake{$tag}>", $xmlString);
}

return $xmlString;
}
Expand All @@ -239,6 +265,16 @@ function ($matches) {
$value
);
}
foreach ($this->nonValidAttributeTags as $tag) {
$value = preg_replace_callback(
'#<fake' . $tag . '(.[^>]*)>#uim',
function ($matches) {
return $this->placeHolderMap[trim($matches[1])];
},
$value
);
$value = str_replace("</fake{$tag}>", "</{$tag}>", $value);
}

return $value;
}
Expand Down

0 comments on commit e6875b4

Please sign in to comment.