Exception
1 year ago
Provider
1 year ago
Test
1 year ago
Util
1 year ago
DataCollectorTranslator.php
1 year ago
IdentityTranslator.php
1 year ago
LoggingTranslator.php
1 year ago
MessageCatalogue.php
1 year ago
MessageCatalogueInterface.php
1 year ago
MetadataAwareInterface.php
1 year ago
PseudoLocalizationTranslator.php
1 year ago
TranslatableMessage.php
1 year ago
Translator.php
1 year ago
TranslatorBag.php
1 year ago
TranslatorBagInterface.php
1 year ago
index.php
3 years ago
PseudoLocalizationTranslator.php
164 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\Translation; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Symfony\Contracts\Translation\TranslatorInterface; |
| 5 | final class PseudoLocalizationTranslator implements TranslatorInterface |
| 6 | { |
| 7 | private const EXPANSION_CHARACTER = '~'; |
| 8 | private $translator; |
| 9 | private $accents; |
| 10 | private $expansionFactor; |
| 11 | private $brackets; |
| 12 | private $parseHTML; |
| 13 | private $localizableHTMLAttributes; |
| 14 | public function __construct(TranslatorInterface $translator, array $options = []) |
| 15 | { |
| 16 | $this->translator = $translator; |
| 17 | $this->accents = $options['accents'] ?? \true; |
| 18 | if (1.0 > ($this->expansionFactor = $options['expansion_factor'] ?? 1.0)) { |
| 19 | throw new \InvalidArgumentException('The expansion factor must be greater than or equal to 1.'); |
| 20 | } |
| 21 | $this->brackets = $options['brackets'] ?? \true; |
| 22 | $this->parseHTML = $options['parse_html'] ?? \false; |
| 23 | if ($this->parseHTML && !$this->accents && 1.0 === $this->expansionFactor) { |
| 24 | $this->parseHTML = \false; |
| 25 | } |
| 26 | $this->localizableHTMLAttributes = $options['localizable_html_attributes'] ?? []; |
| 27 | } |
| 28 | public function trans(string $id, array $parameters = [], ?string $domain = null, ?string $locale = null) : string |
| 29 | { |
| 30 | $trans = ''; |
| 31 | $visibleText = ''; |
| 32 | foreach ($this->getParts($this->translator->trans($id, $parameters, $domain, $locale)) as [$visible, $localizable, $text]) { |
| 33 | if ($visible) { |
| 34 | $visibleText .= $text; |
| 35 | } |
| 36 | if (!$localizable) { |
| 37 | $trans .= $text; |
| 38 | continue; |
| 39 | } |
| 40 | $this->addAccents($trans, $text); |
| 41 | } |
| 42 | $this->expand($trans, $visibleText); |
| 43 | $this->addBrackets($trans); |
| 44 | return $trans; |
| 45 | } |
| 46 | public function getLocale() : string |
| 47 | { |
| 48 | return $this->translator->getLocale(); |
| 49 | } |
| 50 | private function getParts(string $originalTrans) : array |
| 51 | { |
| 52 | if (!$this->parseHTML) { |
| 53 | return [[\true, \true, $originalTrans]]; |
| 54 | } |
| 55 | $html = \mb_encode_numericentity($originalTrans, [0x80, 0x10ffff, 0, 0x1fffff], \mb_detect_encoding($originalTrans, null, \true) ?: 'UTF-8'); |
| 56 | $useInternalErrors = \libxml_use_internal_errors(\true); |
| 57 | $dom = new \DOMDocument(); |
| 58 | $dom->loadHTML('<trans>' . $html . '</trans>'); |
| 59 | \libxml_clear_errors(); |
| 60 | \libxml_use_internal_errors($useInternalErrors); |
| 61 | return $this->parseNode($dom->childNodes->item(1)->childNodes->item(0)->childNodes->item(0)); |
| 62 | } |
| 63 | private function parseNode(\DOMNode $node) : array |
| 64 | { |
| 65 | $parts = []; |
| 66 | foreach ($node->childNodes as $childNode) { |
| 67 | if (!$childNode instanceof \DOMElement) { |
| 68 | $parts[] = [\true, \true, $childNode->nodeValue]; |
| 69 | continue; |
| 70 | } |
| 71 | $parts[] = [\false, \false, '<' . $childNode->tagName]; |
| 72 | foreach ($childNode->attributes as $attribute) { |
| 73 | $parts[] = [\false, \false, ' ' . $attribute->nodeName . '="']; |
| 74 | $localizableAttribute = \in_array($attribute->nodeName, $this->localizableHTMLAttributes, \true); |
| 75 | foreach (\preg_split('/(&(?:amp|quot|#039|lt|gt);+)/', \htmlspecialchars($attribute->nodeValue, \ENT_QUOTES, 'UTF-8'), -1, \PREG_SPLIT_DELIM_CAPTURE) as $i => $match) { |
| 76 | if ('' === $match) { |
| 77 | continue; |
| 78 | } |
| 79 | $parts[] = [\false, $localizableAttribute && 0 === $i % 2, $match]; |
| 80 | } |
| 81 | $parts[] = [\false, \false, '"']; |
| 82 | } |
| 83 | $parts[] = [\false, \false, '>']; |
| 84 | $parts = \array_merge($parts, $this->parseNode($childNode, $parts)); |
| 85 | $parts[] = [\false, \false, '</' . $childNode->tagName . '>']; |
| 86 | } |
| 87 | return $parts; |
| 88 | } |
| 89 | private function addAccents(string &$trans, string $text) : void |
| 90 | { |
| 91 | $trans .= $this->accents ? \strtr($text, [' ' => ' ', '!' => '¡', '"' => '″', '#' => '♯', '$' => '€', '%' => '‰', '&' => '� |
| 92 | �', '\'' => '´', '(' => '{', ')' => '}', '*' => '⁎', '+' => '⁺', ',' => '،', '-' => '‐', '.' => '·', '/' => '⁄', '0' => '⓪', '1' => '①', '2' => '②', '3' => '③', '4' => '④', '5' => '⑤', '6' => '⑥', '7' => '⑦', '8' => '⑧', '9' => '⑨', ':' => '∶', ';' => '⁏', '<' => '≤', '=' => '≂', '>' => '≥', '?' => '¿', '@' => '՞', 'A' => '� |
| 93 | ', 'B' => 'Ɓ', 'C' => 'Ç', 'D' => 'Ð', 'E' => 'É', 'F' => 'Ƒ', 'G' => 'Ĝ', 'H' => 'Ĥ', 'I' => 'Î', 'J' => 'Ĵ', 'K' => 'Ķ', 'L' => 'Ļ', 'M' => 'Ṁ', 'N' => 'Ñ', 'O' => 'Ö', 'P' => 'Þ', 'Q' => 'Ǫ', 'R' => 'Ŕ', 'S' => 'Š', 'T' => 'Ţ', 'U' => 'Û', 'V' => 'Ṽ', 'W' => 'Ŵ', 'X' => 'Ẋ', 'Y' => 'Ý', 'Z' => 'Ž', '[' => '� |
| 94 | ', '\\' => '∖', ']' => '⁆', '^' => '˄', '_' => '‿', '`' => '‵', 'a' => 'å', 'b' => 'ƀ', 'c' => 'ç', 'd' => 'ð', 'e' => 'é', 'f' => 'ƒ', 'g' => 'ĝ', 'h' => 'ĥ', 'i' => 'î', 'j' => 'ĵ', 'k' => 'ķ', 'l' => 'ļ', 'm' => 'ɱ', 'n' => 'ñ', 'o' => 'ö', 'p' => 'þ', 'q' => 'ǫ', 'r' => 'ŕ', 's' => 'š', 't' => 'ţ', 'u' => 'û', 'v' => 'ṽ', 'w' => 'ŵ', 'x' => 'ẋ', 'y' => 'ý', 'z' => 'ž', '{' => '(', '|' => '¦', '}' => ')', '~' => '˞']) : $text; |
| 95 | } |
| 96 | private function expand(string &$trans, string $visibleText) : void |
| 97 | { |
| 98 | if (1.0 >= $this->expansionFactor) { |
| 99 | return; |
| 100 | } |
| 101 | $visibleLength = $this->strlen($visibleText); |
| 102 | $missingLength = (int) \ceil($visibleLength * $this->expansionFactor) - $visibleLength; |
| 103 | if ($this->brackets) { |
| 104 | $missingLength -= 2; |
| 105 | } |
| 106 | if (0 >= $missingLength) { |
| 107 | return; |
| 108 | } |
| 109 | $words = []; |
| 110 | $wordsCount = 0; |
| 111 | foreach (\preg_split('/ +/', $visibleText, -1, \PREG_SPLIT_NO_EMPTY) as $word) { |
| 112 | $wordLength = $this->strlen($word); |
| 113 | if ($wordLength >= $missingLength) { |
| 114 | continue; |
| 115 | } |
| 116 | if (!isset($words[$wordLength])) { |
| 117 | $words[$wordLength] = 0; |
| 118 | } |
| 119 | ++$words[$wordLength]; |
| 120 | ++$wordsCount; |
| 121 | } |
| 122 | if (!$words) { |
| 123 | $trans .= 1 === $missingLength ? self::EXPANSION_CHARACTER : ' ' . \str_repeat(self::EXPANSION_CHARACTER, $missingLength - 1); |
| 124 | return; |
| 125 | } |
| 126 | \arsort($words, \SORT_NUMERIC); |
| 127 | $longestWordLength = \max(\array_keys($words)); |
| 128 | while (\true) { |
| 129 | $r = \mt_rand(1, $wordsCount); |
| 130 | foreach ($words as $length => $count) { |
| 131 | $r -= $count; |
| 132 | if ($r <= 0) { |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | $trans .= ' ' . \str_repeat(self::EXPANSION_CHARACTER, $length); |
| 137 | $missingLength -= $length + 1; |
| 138 | if (0 === $missingLength) { |
| 139 | return; |
| 140 | } |
| 141 | while ($longestWordLength >= $missingLength) { |
| 142 | $wordsCount -= $words[$longestWordLength]; |
| 143 | unset($words[$longestWordLength]); |
| 144 | if (!$words) { |
| 145 | $trans .= 1 === $missingLength ? self::EXPANSION_CHARACTER : ' ' . \str_repeat(self::EXPANSION_CHARACTER, $missingLength - 1); |
| 146 | return; |
| 147 | } |
| 148 | $longestWordLength = \max(\array_keys($words)); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | private function addBrackets(string &$trans) : void |
| 153 | { |
| 154 | if (!$this->brackets) { |
| 155 | return; |
| 156 | } |
| 157 | $trans = '[' . $trans . ']'; |
| 158 | } |
| 159 | private function strlen(string $s) : int |
| 160 | { |
| 161 | return \false === ($encoding = \mb_detect_encoding($s, null, \true)) ? \strlen($s) : \mb_strlen($s, $encoding); |
| 162 | } |
| 163 | } |
| 164 |