| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Translation; |
| 13 |
|
| 14 |
use Symfony\Component\Translation\Exception\LogicException; |
| 15 |
use Symfony\Contracts\Translation\TranslatorInterface; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
final class PseudoLocalizationTranslator implements TranslatorInterface, TranslatorBagInterface |
| 21 |
{ |
| 22 |
private const EXPANSION_CHARACTER = '~'; |
| 23 |
|
| 24 |
private bool $accents; |
| 25 |
private float $expansionFactor; |
| 26 |
private bool $brackets; |
| 27 |
private bool $parseHTML; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
private array $localizableHTMLAttributes; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
ö". |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
ata would be left unchanged. |
| 66 |
|
| 67 |
public function __construct( |
| 68 |
private TranslatorInterface $translator, |
| 69 |
array $options = [], |
| 70 |
) { |
| 71 |
$this->translator = $translator; |
| 72 |
$this->accents = $options['accents'] ?? true; |
| 73 |
|
| 74 |
if (1.0 > ($this->expansionFactor = $options['expansion_factor'] ?? 1.0)) { |
| 75 |
throw new \InvalidArgumentException('The expansion factor must be greater than or equal to 1.'); |
| 76 |
} |
| 77 |
|
| 78 |
$this->brackets = $options['brackets'] ?? true; |
| 79 |
|
| 80 |
$this->parseHTML = $options['parse_html'] ?? false; |
| 81 |
if ($this->parseHTML && !$this->accents && 1.0 === $this->expansionFactor) { |
| 82 |
$this->parseHTML = false; |
| 83 |
} |
| 84 |
|
| 85 |
$this->localizableHTMLAttributes = $options['localizable_html_attributes'] ?? []; |
| 86 |
} |
| 87 |
|
| 88 |
public function trans(string $id, array $parameters = [], ?string $domain = null, ?string $locale = null): string |
| 89 |
{ |
| 90 |
$trans = ''; |
| 91 |
$visibleText = ''; |
| 92 |
|
| 93 |
foreach ($this->getParts($this->translator->trans($id, $parameters, $domain, $locale)) as [$visible, $localizable, $text]) { |
| 94 |
if ($visible) { |
| 95 |
$visibleText .= $text; |
| 96 |
} |
| 97 |
|
| 98 |
if (!$localizable) { |
| 99 |
$trans .= $text; |
| 100 |
|
| 101 |
continue; |
| 102 |
} |
| 103 |
|
| 104 |
$this->addAccents($trans, $text); |
| 105 |
} |
| 106 |
|
| 107 |
$this->expand($trans, $visibleText); |
| 108 |
|
| 109 |
$this->addBrackets($trans); |
| 110 |
|
| 111 |
return $trans; |
| 112 |
} |
| 113 |
|
| 114 |
public function getLocale(): string |
| 115 |
{ |
| 116 |
return $this->translator->getLocale(); |
| 117 |
} |
| 118 |
|
| 119 |
public function getCatalogue(?string $locale = null): MessageCatalogueInterface |
| 120 |
{ |
| 121 |
if (!$this->translator instanceof TranslatorBagInterface) { |
| 122 |
throw new LogicException(\sprintf('The "%s()" method cannot be called as the wrapped translator class "%s" does not implement the "%s".', __METHOD__, $this->translator::class, TranslatorBagInterface::class)); |
| 123 |
} |
| 124 |
|
| 125 |
return $this->translator->getCatalogue($locale); |
| 126 |
} |
| 127 |
|
| 128 |
public function getCatalogues(): array |
| 129 |
{ |
| 130 |
if (!$this->translator instanceof TranslatorBagInterface) { |
| 131 |
throw new LogicException(\sprintf('The "%s()" method cannot be called as the wrapped translator class "%s" does not implement the "%s".', __METHOD__, $this->translator::class, TranslatorBagInterface::class)); |
| 132 |
} |
| 133 |
|
| 134 |
return $this->translator->getCatalogues(); |
| 135 |
} |
| 136 |
|
| 137 |
private function getParts(string $originalTrans): array |
| 138 |
{ |
| 139 |
if (!$this->parseHTML) { |
| 140 |
return [[true, true, $originalTrans]]; |
| 141 |
} |
| 142 |
|
| 143 |
$html = mb_encode_numericentity($originalTrans, [0x80, 0x10FFFF, 0, 0x1FFFFF], mb_detect_encoding($originalTrans, null, true) ?: 'UTF-8'); |
| 144 |
|
| 145 |
$useInternalErrors = libxml_use_internal_errors(true); |
| 146 |
|
| 147 |
$dom = new \DOMDocument(); |
| 148 |
$dom->loadHTML('<trans>'.$html.'</trans>'); |
| 149 |
|
| 150 |
libxml_clear_errors(); |
| 151 |
libxml_use_internal_errors($useInternalErrors); |
| 152 |
|
| 153 |
return $this->parseNode($dom->childNodes->item(1)->childNodes->item(0)->childNodes->item(0)); |
| 154 |
} |
| 155 |
|
| 156 |
private function parseNode(\DOMNode $node): array |
| 157 |
{ |
| 158 |
$parts = []; |
| 159 |
|
| 160 |
foreach ($node->childNodes as $childNode) { |
| 161 |
if (!$childNode instanceof \DOMElement) { |
| 162 |
$parts[] = [true, true, $childNode->nodeValue]; |
| 163 |
|
| 164 |
continue; |
| 165 |
} |
| 166 |
|
| 167 |
$parts[] = [false, false, '<'.$childNode->tagName]; |
| 168 |
|
| 169 |
foreach ($childNode->attributes as $attribute) { |
| 170 |
$parts[] = [false, false, ' '.$attribute->nodeName.'="']; |
| 171 |
|
| 172 |
$localizableAttribute = \in_array($attribute->nodeName, $this->localizableHTMLAttributes, true); |
| 173 |
foreach (preg_split('/(&(?:amp|quot|#039|lt|gt);+)/', htmlspecialchars($attribute->nodeValue, \ENT_QUOTES, 'UTF-8'), -1, \PREG_SPLIT_DELIM_CAPTURE) as $i => $match) { |
| 174 |
if ('' === $match) { |
| 175 |
continue; |
| 176 |
} |
| 177 |
|
| 178 |
$parts[] = [false, $localizableAttribute && 0 === $i % 2, $match]; |
| 179 |
} |
| 180 |
|
| 181 |
$parts[] = [false, false, '"']; |
| 182 |
} |
| 183 |
|
| 184 |
$parts[] = [false, false, '>']; |
| 185 |
|
| 186 |
$parts = array_merge($parts, $this->parseNode($childNode)); |
| 187 |
|
| 188 |
$parts[] = [false, false, '</'.$childNode->tagName.'>']; |
| 189 |
} |
| 190 |
|
| 191 |
return $parts; |
| 192 |
} |
| 193 |
|
| 194 |
private function addAccents(string &$trans, string $text): void |
| 195 |
{ |
| 196 |
$trans .= $this->accents ? strtr($text, [ |
| 197 |
' ' => ' ', |
| 198 |
'!' => '¡', |
| 199 |
'"' => '″', |
| 200 |
'#' => '♯', |
| 201 |
'$' => '€', |
| 202 |
'%' => '‰', |
| 203 |
'&' => '⅋', |
| 204 |
'\'' => '´', |
| 205 |
'(' => '{', |
| 206 |
')' => '}', |
| 207 |
'*' => '⁎', |
| 208 |
'+' => '⁺', |
| 209 |
',' => '،', |
| 210 |
'-' => '‐', |
| 211 |
'.' => '·', |
| 212 |
'/' => '⁄', |
| 213 |
'0' => '⓪', |
| 214 |
'1' => '①', |
| 215 |
'2' => '②', |
| 216 |
'3' => '③', |
| 217 |
'4' => '④', |
| 218 |
'5' => '⑤', |
| 219 |
'6' => '⑥', |
| 220 |
'7' => '⑦', |
| 221 |
'8' => '⑧', |
| 222 |
'9' => '⑨', |
| 223 |
':' => '∶', |
| 224 |
';' => '⁏', |
| 225 |
'<' => '≤', |
| 226 |
'=' => '≂', |
| 227 |
'>' => '≥', |
| 228 |
'?' => '¿', |
| 229 |
'@' => '՞', |
| 230 |
'A' => 'Å', |
| 231 |
'B' => 'Ɓ', |
| 232 |
'C' => 'Ç', |
| 233 |
'D' => 'Ð', |
| 234 |
'E' => 'É', |
| 235 |
'F' => 'Ƒ', |
| 236 |
'G' => 'Ĝ', |
| 237 |
'H' => 'Ĥ', |
| 238 |
'I' => 'Î', |
| 239 |
'J' => 'Ĵ', |
| 240 |
'K' => 'Ķ', |
| 241 |
'L' => 'Ļ', |
| 242 |
'M' => 'Ṁ', |
| 243 |
'N' => 'Ñ', |
| 244 |
'O' => 'Ö', |
| 245 |
'P' => 'Þ', |
| 246 |
'Q' => 'Ǫ', |
| 247 |
'R' => 'Ŕ', |
| 248 |
'S' => 'Š', |
| 249 |
'T' => 'Ţ', |
| 250 |
'U' => 'Û', |
| 251 |
'V' => 'Ṽ', |
| 252 |
'W' => 'Ŵ', |
| 253 |
'X' => 'Ẋ', |
| 254 |
'Y' => 'Ý', |
| 255 |
'Z' => 'Ž', |
| 256 |
'[' => '⁅', |
| 257 |
'\\' => '∖', |
| 258 |
']' => '⁆', |
| 259 |
'^' => '˄', |
| 260 |
'_' => '‿', |
| 261 |
'`' => '‵', |
| 262 |
'a' => 'å', |
| 263 |
'b' => 'ƀ', |
| 264 |
'c' => 'ç', |
| 265 |
'd' => 'ð', |
| 266 |
'e' => 'é', |
| 267 |
'f' => 'ƒ', |
| 268 |
'g' => 'ĝ', |
| 269 |
'h' => 'ĥ', |
| 270 |
'i' => 'î', |
| 271 |
'j' => 'ĵ', |
| 272 |
'k' => 'ķ', |
| 273 |
'l' => 'ļ', |
| 274 |
'm' => 'ɱ', |
| 275 |
'n' => 'ñ', |
| 276 |
'o' => 'ö', |
| 277 |
'p' => 'þ', |
| 278 |
'q' => 'ǫ', |
| 279 |
'r' => 'ŕ', |
| 280 |
's' => 'š', |
| 281 |
't' => 'ţ', |
| 282 |
'u' => 'û', |
| 283 |
'v' => 'ṽ', |
| 284 |
'w' => 'ŵ', |
| 285 |
'x' => 'ẋ', |
| 286 |
'y' => 'ý', |
| 287 |
'z' => 'ž', |
| 288 |
'{' => '(', |
| 289 |
'|' => '¦', |
| 290 |
'}' => ')', |
| 291 |
'~' => '˞', |
| 292 |
]) : $text; |
| 293 |
} |
| 294 |
|
| 295 |
private function expand(string &$trans, string $visibleText): void |
| 296 |
{ |
| 297 |
if (1.0 >= $this->expansionFactor) { |
| 298 |
return; |
| 299 |
} |
| 300 |
|
| 301 |
$visibleLength = $this->strlen($visibleText); |
| 302 |
$missingLength = (int) ceil($visibleLength * $this->expansionFactor) - $visibleLength; |
| 303 |
if ($this->brackets) { |
| 304 |
$missingLength -= 2; |
| 305 |
} |
| 306 |
|
| 307 |
if (0 >= $missingLength) { |
| 308 |
return; |
| 309 |
} |
| 310 |
|
| 311 |
$words = []; |
| 312 |
$wordsCount = 0; |
| 313 |
foreach (preg_split('/ +/', $visibleText, -1, \PREG_SPLIT_NO_EMPTY) as $word) { |
| 314 |
$wordLength = $this->strlen($word); |
| 315 |
|
| 316 |
if ($wordLength >= $missingLength) { |
| 317 |
continue; |
| 318 |
} |
| 319 |
|
| 320 |
if (!isset($words[$wordLength])) { |
| 321 |
$words[$wordLength] = 0; |
| 322 |
} |
| 323 |
|
| 324 |
++$words[$wordLength]; |
| 325 |
++$wordsCount; |
| 326 |
} |
| 327 |
|
| 328 |
if (!$words) { |
| 329 |
$trans .= 1 === $missingLength ? self::EXPANSION_CHARACTER : ' '.str_repeat(self::EXPANSION_CHARACTER, $missingLength - 1); |
| 330 |
|
| 331 |
return; |
| 332 |
} |
| 333 |
|
| 334 |
arsort($words, \SORT_NUMERIC); |
| 335 |
|
| 336 |
$longestWordLength = max(array_keys($words)); |
| 337 |
|
| 338 |
while (true) { |
| 339 |
$r = mt_rand(1, $wordsCount); |
| 340 |
|
| 341 |
foreach ($words as $length => $count) { |
| 342 |
$r -= $count; |
| 343 |
if ($r <= 0) { |
| 344 |
break; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
$trans .= ' '.str_repeat(self::EXPANSION_CHARACTER, $length); |
| 349 |
|
| 350 |
$missingLength -= $length + 1; |
| 351 |
|
| 352 |
if (0 === $missingLength) { |
| 353 |
return; |
| 354 |
} |
| 355 |
|
| 356 |
while ($longestWordLength >= $missingLength) { |
| 357 |
$wordsCount -= $words[$longestWordLength]; |
| 358 |
unset($words[$longestWordLength]); |
| 359 |
|
| 360 |
if (!$words) { |
| 361 |
$trans .= 1 === $missingLength ? self::EXPANSION_CHARACTER : ' '.str_repeat(self::EXPANSION_CHARACTER, $missingLength - 1); |
| 362 |
|
| 363 |
return; |
| 364 |
} |
| 365 |
|
| 366 |
$longestWordLength = max(array_keys($words)); |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
private function addBrackets(string &$trans): void |
| 372 |
{ |
| 373 |
if (!$this->brackets) { |
| 374 |
return; |
| 375 |
} |
| 376 |
|
| 377 |
$trans = '['.$trans.']'; |
| 378 |
} |
| 379 |
|
| 380 |
private function strlen(string $s): int |
| 381 |
{ |
| 382 |
return false === ($encoding = mb_detect_encoding($s, null, true)) ? \strlen($s) : mb_strlen($s, $encoding); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
|