Catalogue
2 months ago
Command
2 months ago
DataCollector
2 months ago
DependencyInjection
2 months ago
Dumper
2 months ago
Exception
2 months ago
Extractor
2 months ago
Formatter
2 months ago
Loader
2 months ago
Provider
2 months ago
Reader
2 months ago
Resources
2 months ago
Util
2 months ago
Writer
2 months ago
DataCollectorTranslator.php
2 months ago
IdentityTranslator.php
2 months ago
LoggingTranslator.php
2 months ago
MessageCatalogue.php
2 months ago
MessageCatalogueInterface.php
2 months ago
MetadataAwareInterface.php
2 months ago
PseudoLocalizationTranslator.php
2 months ago
TranslatableMessage.php
2 months ago
Translator.php
2 months ago
TranslatorBag.php
2 months ago
TranslatorBagInterface.php
2 months ago
PseudoLocalizationTranslator.php
216 lines
| 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 | namespace ProfilePressVendor\Symfony\Component\Translation; |
| 12 | |
| 13 | use ProfilePressVendor\Symfony\Contracts\Translation\TranslatorInterface; |
| 14 | /** |
| 15 | * This translator should only be used in a development environment. |
| 16 | */ |
| 17 | final class PseudoLocalizationTranslator implements TranslatorInterface |
| 18 | { |
| 19 | private const EXPANSION_CHARACTER = '~'; |
| 20 | private $translator; |
| 21 | private $accents; |
| 22 | private $expansionFactor; |
| 23 | private $brackets; |
| 24 | private $parseHTML; |
| 25 | /** |
| 26 | * @var string[] |
| 27 | */ |
| 28 | private $localizableHTMLAttributes; |
| 29 | /** |
| 30 | * Available options: |
| 31 | * * accents: |
| 32 | * type: boolean |
| 33 | * default: true |
| 34 | * description: replace ASCII characters of the translated string with accented versions or similar characters |
| 35 | * example: if true, "foo" => "ƒöö". |
| 36 | * |
| 37 | * * expansion_factor: |
| 38 | * type: float |
| 39 | * default: 1 |
| 40 | * validation: it must be greater than or equal to 1 |
| 41 | * description: expand the translated string by the given factor with spaces and tildes |
| 42 | * example: if 2, "foo" => "~foo ~" |
| 43 | * |
| 44 | * * brackets: |
| 45 | * type: boolean |
| 46 | * default: true |
| 47 | * description: wrap the translated string with brackets |
| 48 | * example: if true, "foo" => "[foo]" |
| 49 | * |
| 50 | * * parse_html: |
| 51 | * type: boolean |
| 52 | * default: false |
| 53 | * 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 |
| 54 | * warning: unclosed tags are unsupported, they will be fixed (closed) by the parser - eg, "foo <div>bar" => "foo <div>bar</div>" |
| 55 | * |
| 56 | * * localizable_html_attributes: |
| 57 | * type: string[] |
| 58 | * default: [] |
| 59 | * description: the list of HTML attributes whose values can be altered - it is only useful when the "parse_html" option is set to true |
| 60 | * 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. |
| 61 | */ |
| 62 | public function __construct(TranslatorInterface $translator, array $options = []) |
| 63 | { |
| 64 | $this->translator = $translator; |
| 65 | $this->accents = $options['accents'] ?? \true; |
| 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 | $this->brackets = $options['brackets'] ?? \true; |
| 70 | $this->parseHTML = $options['parse_html'] ?? \false; |
| 71 | if ($this->parseHTML && !$this->accents && 1.0 === $this->expansionFactor) { |
| 72 | $this->parseHTML = \false; |
| 73 | } |
| 74 | $this->localizableHTMLAttributes = $options['localizable_html_attributes'] ?? []; |
| 75 | } |
| 76 | /** |
| 77 | * {@inheritdoc} |
| 78 | */ |
| 79 | public function trans(string $id, array $parameters = [], ?string $domain = null, ?string $locale = null): string |
| 80 | { |
| 81 | $trans = ''; |
| 82 | $visibleText = ''; |
| 83 | foreach ($this->getParts($this->translator->trans($id, $parameters, $domain, $locale)) as [$visible, $localizable, $text]) { |
| 84 | if ($visible) { |
| 85 | $visibleText .= $text; |
| 86 | } |
| 87 | if (!$localizable) { |
| 88 | $trans .= $text; |
| 89 | continue; |
| 90 | } |
| 91 | $this->addAccents($trans, $text); |
| 92 | } |
| 93 | $this->expand($trans, $visibleText); |
| 94 | $this->addBrackets($trans); |
| 95 | return $trans; |
| 96 | } |
| 97 | public function getLocale(): string |
| 98 | { |
| 99 | return $this->translator->getLocale(); |
| 100 | } |
| 101 | private function getParts(string $originalTrans): array |
| 102 | { |
| 103 | if (!$this->parseHTML) { |
| 104 | return [[\true, \true, $originalTrans]]; |
| 105 | } |
| 106 | $html = mb_encode_numericentity($originalTrans, [0x80, 0x10ffff, 0, 0x1fffff], mb_detect_encoding($originalTrans, null, \true) ?: 'UTF-8'); |
| 107 | $useInternalErrors = libxml_use_internal_errors(\true); |
| 108 | $dom = new \DOMDocument(); |
| 109 | $dom->loadHTML('<trans>' . $html . '</trans>'); |
| 110 | libxml_clear_errors(); |
| 111 | libxml_use_internal_errors($useInternalErrors); |
| 112 | return $this->parseNode($dom->childNodes->item(1)->childNodes->item(0)->childNodes->item(0)); |
| 113 | } |
| 114 | private function parseNode(\DOMNode $node): array |
| 115 | { |
| 116 | $parts = []; |
| 117 | foreach ($node->childNodes as $childNode) { |
| 118 | if (!$childNode instanceof \DOMElement) { |
| 119 | $parts[] = [\true, \true, $childNode->nodeValue]; |
| 120 | continue; |
| 121 | } |
| 122 | $parts[] = [\false, \false, '<' . $childNode->tagName]; |
| 123 | /** @var \DOMAttr $attribute */ |
| 124 | foreach ($childNode->attributes as $attribute) { |
| 125 | $parts[] = [\false, \false, ' ' . $attribute->nodeName . '="']; |
| 126 | $localizableAttribute = \in_array($attribute->nodeName, $this->localizableHTMLAttributes, \true); |
| 127 | foreach (preg_split('/(&(?:amp|quot|#039|lt|gt);+)/', htmlspecialchars($attribute->nodeValue, \ENT_QUOTES, 'UTF-8'), -1, \PREG_SPLIT_DELIM_CAPTURE) as $i => $match) { |
| 128 | if ('' === $match) { |
| 129 | continue; |
| 130 | } |
| 131 | $parts[] = [\false, $localizableAttribute && 0 === $i % 2, $match]; |
| 132 | } |
| 133 | $parts[] = [\false, \false, '"']; |
| 134 | } |
| 135 | $parts[] = [\false, \false, '>']; |
| 136 | $parts = array_merge($parts, $this->parseNode($childNode, $parts)); |
| 137 | $parts[] = [\false, \false, '</' . $childNode->tagName . '>']; |
| 138 | } |
| 139 | return $parts; |
| 140 | } |
| 141 | private function addAccents(string &$trans, string $text): void |
| 142 | { |
| 143 | $trans .= $this->accents ? strtr($text, [' ' => ' ', '!' => '¡', '"' => '″', '#' => '♯', '$' => '€', '%' => '‰', '&' => '� |
| 144 | �', '\'' => '´', '(' => '{', ')' => '}', '*' => '⁎', '+' => '⁺', ',' => '،', '-' => '‐', '.' => '·', '/' => '⁄', '0' => '⓪', '1' => '①', '2' => '②', '3' => '③', '4' => '④', '5' => '⑤', '6' => '⑥', '7' => '⑦', '8' => '⑧', '9' => '⑨', ':' => '∶', ';' => '⁏', '<' => '≤', '=' => '≂', '>' => '≥', '?' => '¿', '@' => '՞', 'A' => '� |
| 145 | ', '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' => 'Ž', '[' => '� |
| 146 | ', '\\' => '∖', ']' => '⁆', '^' => '˄', '_' => '‿', '`' => '‵', '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; |
| 147 | } |
| 148 | private function expand(string &$trans, string $visibleText): void |
| 149 | { |
| 150 | if (1.0 >= $this->expansionFactor) { |
| 151 | return; |
| 152 | } |
| 153 | $visibleLength = $this->strlen($visibleText); |
| 154 | $missingLength = (int) ceil($visibleLength * $this->expansionFactor) - $visibleLength; |
| 155 | if ($this->brackets) { |
| 156 | $missingLength -= 2; |
| 157 | } |
| 158 | if (0 >= $missingLength) { |
| 159 | return; |
| 160 | } |
| 161 | $words = []; |
| 162 | $wordsCount = 0; |
| 163 | foreach (preg_split('/ +/', $visibleText, -1, \PREG_SPLIT_NO_EMPTY) as $word) { |
| 164 | $wordLength = $this->strlen($word); |
| 165 | if ($wordLength >= $missingLength) { |
| 166 | continue; |
| 167 | } |
| 168 | if (!isset($words[$wordLength])) { |
| 169 | $words[$wordLength] = 0; |
| 170 | } |
| 171 | ++$words[$wordLength]; |
| 172 | ++$wordsCount; |
| 173 | } |
| 174 | if (!$words) { |
| 175 | $trans .= 1 === $missingLength ? self::EXPANSION_CHARACTER : ' ' . str_repeat(self::EXPANSION_CHARACTER, $missingLength - 1); |
| 176 | return; |
| 177 | } |
| 178 | arsort($words, \SORT_NUMERIC); |
| 179 | $longestWordLength = max(array_keys($words)); |
| 180 | while (\true) { |
| 181 | $r = mt_rand(1, $wordsCount); |
| 182 | foreach ($words as $length => $count) { |
| 183 | $r -= $count; |
| 184 | if ($r <= 0) { |
| 185 | break; |
| 186 | } |
| 187 | } |
| 188 | $trans .= ' ' . str_repeat(self::EXPANSION_CHARACTER, $length); |
| 189 | $missingLength -= $length + 1; |
| 190 | if (0 === $missingLength) { |
| 191 | return; |
| 192 | } |
| 193 | while ($longestWordLength >= $missingLength) { |
| 194 | $wordsCount -= $words[$longestWordLength]; |
| 195 | unset($words[$longestWordLength]); |
| 196 | if (!$words) { |
| 197 | $trans .= 1 === $missingLength ? self::EXPANSION_CHARACTER : ' ' . str_repeat(self::EXPANSION_CHARACTER, $missingLength - 1); |
| 198 | return; |
| 199 | } |
| 200 | $longestWordLength = max(array_keys($words)); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | private function addBrackets(string &$trans): void |
| 205 | { |
| 206 | if (!$this->brackets) { |
| 207 | return; |
| 208 | } |
| 209 | $trans = '[' . $trans . ']'; |
| 210 | } |
| 211 | private function strlen(string $s): int |
| 212 | { |
| 213 | return \false === ($encoding = mb_detect_encoding($s, null, \true)) ? \strlen($s) : mb_strlen($s, $encoding); |
| 214 | } |
| 215 | } |
| 216 |