|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Translation; |
| 13 | + |
| 14 | +use Symfony\Component\DomCrawler\Crawler; |
| 15 | +use Symfony\Contracts\Translation\TranslatorInterface; |
| 16 | + |
| 17 | +final class PseudoLocalizationTranslator implements TranslatorInterface |
| 18 | +{ |
| 19 | + private $translator; |
| 20 | + |
| 21 | + private $accents; |
| 22 | + private $expansionFactor; |
| 23 | + private $brackets; |
| 24 | + private $parseHTML; |
| 25 | + private $localizableHTMLAttributes; |
| 26 | + |
| 27 | + /** |
| 28 | + * Available options: |
| 29 | + * * accents: |
| 30 | + * type: boolean |
| 31 | + * default: true |
| 32 | + * description: replace ASCII characters of the translated string with accented versions or similar characters |
| 33 | + * example: if true, "foo" => "ƒöö" |
| 34 | + * |
| 35 | + * * expansion_factor: |
| 36 | + * type: float |
| 37 | + * default: 1 |
| 38 | + * validation: it must be greater than or equal to 1 |
| 39 | + * description: expand the translated string by the given factor with spaces and tildes |
| 40 | + * example: if 2, "foo" => "~foo ~" |
| 41 | + * |
| 42 | + * * brackets: |
| 43 | + * type: boolean |
| 44 | + * default: true |
| 45 | + * description: wrap the translated string with brackets |
| 46 | + * example: if true, "foo" => "[foo]" |
| 47 | + * |
| 48 | + * * parse_html: |
| 49 | + * type: boolean |
| 50 | + * default: false |
| 51 | + * description: parse the translated string as HTML - looking for HTML tags has a performance impact but allows to preserve them from alterations - it also allows to compute the visible translated string length which is useful to correctly expand ot when it contains HTML |
| 52 | + * warning: unclosed tags are unsupported, they will be fixed (closed) by the parser - eg, "foo <div>bar" => "foo <div>bar</div>" |
| 53 | + * |
| 54 | + * * localizable_html_attributes: |
| 55 | + * type: string[] |
| 56 | + * default: [] |
| 57 | + * description: the list of HTML attributes whose values can be altered - it is only useful when the "parse_html" option is set to true |
| 58 | + * example: if ["title"], and with the "accents" option set to true, "<a href="#" title="Go to your profile">Profile</a>" => "<a href="#" title="Ĝö ţö ýöûŕ þŕöƒîļé">Þŕöƒîļé</a>" - if "title" was not in the "localizable_html_attributes" list, the title attribute data would be left unchanged. |
| 59 | + */ |
| 60 | + public function __construct(TranslatorInterface $translator, array $options = []) |
| 61 | + { |
| 62 | + $this->translator = $translator; |
| 63 | + |
| 64 | + $this->accents = $options['accents'] ?? true; |
| 65 | + |
| 66 | + if (1.0 > ($this->expansionFactor = $options['expansion_factor'] ?? 1.0)) { |
| 67 | + throw new \InvalidArgumentException('The expansion factor must be greater than or equal to 1.'); |
| 68 | + } |
| 69 | + |
| 70 | + $this->brackets = $options['brackets'] ?? true; |
| 71 | + |
| 72 | + $this->parseHTML = $options['parse_html'] ?? false; |
| 73 | + if ($this->parseHTML && !$this->accents && 1.0 === $this->expansionFactor) { |
| 74 | + $this->parseHTML = false; |
| 75 | + } |
| 76 | + |
| 77 | + if ($this->parseHTML && !class_exists(Crawler::class)) { |
| 78 | + throw new \LogicException('Parsing HTML with the pseudo localization translator requires the DomCrawler component. Try running "composer require symfony/dom-crawler".'); |
| 79 | + } |
| 80 | + |
| 81 | + $this->localizableHTMLAttributes = $options['localizable_html_attributes'] ?? []; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * {@inheritdoc} |
| 86 | + */ |
| 87 | + public function trans(string $id, array $parameters = [], string $domain = null, string $locale = null) |
| 88 | + { |
| 89 | + $trans = ''; |
| 90 | + $visibleText = ''; |
| 91 | + |
| 92 | + foreach ($this->getParts($this->translator->trans($id, $parameters, $domain, $locale)) as [$visible, $localizable, $text]) { |
| 93 | + if ($visible) { |
| 94 | + $visibleText .= $text; |
| 95 | + } |
| 96 | + |
| 97 | + if (!$localizable) { |
| 98 | + $trans .= $text; |
| 99 | + |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + $trans .= $this->accents ? strtr($text, [ |
| 104 | + ' ' => ' ', |
| 105 | + '!' => '¡', |
| 106 | + '"' => '″', |
| 107 | + '#' => '♯', |
| 108 | + '$' => '€', |
| 109 | + '%' => '‰', |
| 110 | + '&' => '⅋', |
| 111 | + '\'' => '´', |
| 112 | + '(' => '{', |
| 113 | + ')' => '}', |
| 114 | + '*' => '⁎', |
| 115 | + '+' => '⁺', |
| 116 | + ',' => '،', |
| 117 | + '-' => '‐', |
| 118 | + '.' => '·', |
| 119 | + '/' => '⁄', |
| 120 | + '0' => '⓪', |
| 121 | + '1' => '①', |
| 122 | + '2' => '②', |
| 123 | + '3' => '③', |
| 124 | + '4' => '④', |
| 125 | + '5' => '⑤', |
| 126 | + '6' => '⑥', |
| 127 | + '7' => '⑦', |
| 128 | + '8' => '⑧', |
| 129 | + '9' => '⑨', |
| 130 | + ':' => '∶', |
| 131 | + ';' => '⁏', |
| 132 | + '<' => '≤', |
| 133 | + '=' => '≂', |
| 134 | + '>' => '≥', |
| 135 | + '?' => '¿', |
| 136 | + '@' => '՞', |
| 137 | + 'A' => 'Å', |
| 138 | + 'B' => 'Ɓ', |
| 139 | + 'C' => 'Ç', |
| 140 | + 'D' => 'Ð', |
| 141 | + 'E' => 'É', |
| 142 | + 'F' => 'Ƒ', |
| 143 | + 'G' => 'Ĝ', |
| 144 | + 'H' => 'Ĥ', |
| 145 | + 'I' => 'Î', |
| 146 | + 'J' => 'Ĵ', |
| 147 | + 'K' => 'Ķ', |
| 148 | + 'L' => 'Ļ', |
| 149 | + 'M' => 'Ṁ', |
| 150 | + 'N' => 'Ñ', |
| 151 | + 'O' => 'Ö', |
| 152 | + 'P' => 'Þ', |
| 153 | + 'Q' => 'Ǫ', |
| 154 | + 'R' => 'Ŕ', |
| 155 | + 'S' => 'Š', |
| 156 | + 'T' => 'Ţ', |
| 157 | + 'U' => 'Û', |
| 158 | + 'V' => 'Ṽ', |
| 159 | + 'W' => 'Ŵ', |
| 160 | + 'X' => 'Ẋ', |
| 161 | + 'Y' => 'Ý', |
| 162 | + 'Z' => 'Ž', |
| 163 | + '[' => '⁅', |
| 164 | + '\\' => '∖', |
| 165 | + ']' => '⁆', |
| 166 | + '^' => '˄', |
| 167 | + '_' => '‿', |
| 168 | + '`' => '‵', |
| 169 | + 'a' => 'å', |
| 170 | + 'b' => 'ƀ', |
| 171 | + 'c' => 'ç', |
| 172 | + 'd' => 'ð', |
| 173 | + 'e' => 'é', |
| 174 | + 'f' => 'ƒ', |
| 175 | + 'g' => 'ĝ', |
| 176 | + 'h' => 'ĥ', |
| 177 | + 'i' => 'î', |
| 178 | + 'j' => 'ĵ', |
| 179 | + 'k' => 'ķ', |
| 180 | + 'l' => 'ļ', |
| 181 | + 'm' => 'ɱ', |
| 182 | + 'n' => 'ñ', |
| 183 | + 'o' => 'ö', |
| 184 | + 'p' => 'þ', |
| 185 | + 'q' => 'ǫ', |
| 186 | + 'r' => 'ŕ', |
| 187 | + 's' => 'š', |
| 188 | + 't' => 'ţ', |
| 189 | + 'u' => 'û', |
| 190 | + 'v' => 'ṽ', |
| 191 | + 'w' => 'ŵ', |
| 192 | + 'x' => 'ẋ', |
| 193 | + 'y' => 'ý', |
| 194 | + 'z' => 'ž', |
| 195 | + '{' => '(', |
| 196 | + '|' => '¦', |
| 197 | + '}' => ')', |
| 198 | + '~' => '˞']) : $text; |
| 199 | + } |
| 200 | + |
| 201 | + if (1.0 < $this->expansionFactor) { |
| 202 | + $textVisibleLength = false === ($encoding = mb_detect_encoding($visibleText, null, true)) ? \strlen($visibleText) : mb_strlen($visibleText, $encoding); |
| 203 | + $transMissingLength = intval(ceil($textVisibleLength * $this->expansionFactor)) - $textVisibleLength; |
| 204 | + if ($this->brackets) { |
| 205 | + $transMissingLength -= 2; |
| 206 | + } |
| 207 | + |
| 208 | + if (0 < $transMissingLength) { |
| 209 | + $transPrefixLength = intval(floor($transMissingLength / 2)); |
| 210 | + $transPrefix = ''; |
| 211 | + for ($i = 0; $i < $transPrefixLength; $i++) { |
| 212 | + $transPrefix .= 0 === $i % 2 ? '~' : ' '; |
| 213 | + } |
| 214 | + |
| 215 | + $transSuffixLength = $transMissingLength - $transPrefixLength; |
| 216 | + $transSuffix = ''; |
| 217 | + for ($i = 0; $i < $transSuffixLength; $i++) { |
| 218 | + $transSuffix .= 0 === $i % 2 ? ' ' : '~'; |
| 219 | + } |
| 220 | + |
| 221 | + $trans = $transPrefix.$trans.$transSuffix; |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + if ($this->brackets) { |
| 226 | + $trans = '['.$trans.']'; |
| 227 | + } |
| 228 | + |
| 229 | + return $trans; |
| 230 | + } |
| 231 | + |
| 232 | + private function getParts(string $originalTrans): array |
| 233 | + { |
| 234 | + if (!$this->parseHTML) { |
| 235 | + return [[true, true, $originalTrans]]; |
| 236 | + } |
| 237 | + |
| 238 | + $crawler = new Crawler(); |
| 239 | + $crawler->addHtmlContent('<html><body><trans>'.$originalTrans.'</trans></body></html>'); |
| 240 | + |
| 241 | + return $this->parseNode($crawler->children('body > trans')->getNode(0)); |
| 242 | + } |
| 243 | + |
| 244 | + private function parseNode(\DOMNode $node): array |
| 245 | + { |
| 246 | + $parts = []; |
| 247 | + |
| 248 | + foreach ($node->childNodes as $childNode) { |
| 249 | + if (!$childNode instanceof \DOMElement) { |
| 250 | + $parts[] = [true, true, $childNode->nodeValue]; |
| 251 | + |
| 252 | + continue; |
| 253 | + } |
| 254 | + |
| 255 | + $parts[] = [false, false, '<'.$childNode->tagName]; |
| 256 | + |
| 257 | + /** @var \DOMAttr $attribute */ |
| 258 | + foreach ($childNode->attributes as $attribute) { |
| 259 | + $parts[] = [false, false, ' '.$attribute->nodeName.'="']; |
| 260 | + $parts[] = [false, \in_array($attribute->nodeName, $this->localizableHTMLAttributes, true), $attribute->nodeValue]; |
| 261 | + $parts[] = [false, false, '"']; |
| 262 | + } |
| 263 | + |
| 264 | + $parts[] = [false, false, '>']; |
| 265 | + |
| 266 | + $parts = array_merge($parts, $this->parseNode($childNode, $parts)); |
| 267 | + |
| 268 | + $parts[] = [false, false, '</'.$childNode->tagName.'>']; |
| 269 | + } |
| 270 | + |
| 271 | + return $parts; |
| 272 | + } |
| 273 | +} |
0 commit comments