PluginProbe ʕ •ᴥ•ʔ
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress / trunk
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress vtrunk
4.17.2 4.17.1 4.17.0 4.16.19 4.16.18 4.16.17 4.16.16 trunk 1.0 1.0.1 1.0.2 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5a 1.1.6 1.1.7 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4 1.4.1 1.4.2 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.7 1.7.1 1.7.2 1.8 1.8.1 1.8.10 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.1.9 2.2.10 2.2.11 2.2.12 2.2.13 2.2.14 2.2.15 2.2.16 2.2.2 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 3.0 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 3.1.19 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.14 3.2.15 3.2.16 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 4.0.0 4.0.1 4.0.2 4.0.3 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.10.0 4.10.1 4.10.2 4.10.3 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.13.3 4.13.4 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.15.0 4.15.1 4.15.10 4.15.11 4.15.12 4.15.13 4.15.14 4.15.15 4.15.16 4.15.17 4.15.18 4.15.19 4.15.2 4.15.20 4.15.20.1 4.15.21 4.15.22 4.15.23 4.15.24 4.15.25 4.15.3 4.15.4 4.15.5 4.15.6 4.15.7 4.15.8 4.15.9 4.16.0 4.16.1 4.16.10 4.16.11 4.16.12 4.16.13 4.16.14 4.16.15 4.16.2 4.16.3 4.16.4 4.16.5 4.16.6 4.16.7 4.16.8 4.16.9 4.2.0 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.5.3 4.5.4 4.5.5 4.6.0 4.7.0 4.8.0 4.9.0
wp-user-avatar / third-party / vendor / symfony / translation / PseudoLocalizationTranslator.php
wp-user-avatar / third-party / vendor / symfony / translation Last commit date
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