From e6875b4421243139fb961f5e9b65b9ef83d04c30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morel=20Se=CC=81bastien?= Date: Fri, 6 Apr 2018 10:53:35 -0700 Subject: [PATCH] Google just removes tag, fix by faking it --- ...zPlatformAutomatedTranslationExtension.php | 1 + bundle/Resources/config/default_settings.yml | 1 + lib/Encoder.php | 36 +++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/bundle/DependencyInjection/EzPlatformAutomatedTranslationExtension.php b/bundle/DependencyInjection/EzPlatformAutomatedTranslationExtension.php index 1700f61..bd2b603 100644 --- a/bundle/DependencyInjection/EzPlatformAutomatedTranslationExtension.php +++ b/bundle/DependencyInjection/EzPlatformAutomatedTranslationExtension.php @@ -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); } } diff --git a/bundle/Resources/config/default_settings.yml b/bundle/Resources/config/default_settings.yml index d606193..9d64eb8 100644 --- a/bundle/Resources/config/default_settings.yml +++ b/bundle/Resources/config/default_settings.yml @@ -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: [] diff --git a/lib/Encoder.php b/lib/Encoder.php index 5a88a7e..3bf197e 100644 --- a/lib/Encoder.php +++ b/lib/Encoder.php @@ -84,6 +84,13 @@ class Encoder */ private $nonTranslatableTags; + /** + * Every attributes inside these tags must be preserve from translation. + * + * @var array + */ + private $nonValidAttributeTags; + /** * @var ContentTypeService */ @@ -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; } /** @@ -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; } @@ -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; }