PluginProbe ʕ •ᴥ•ʔ
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress / 4.17.1
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress v4.17.1
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 / nesbot / carbon / src / Carbon / AbstractTranslator.php
wp-user-avatar / third-party / vendor / nesbot / carbon / src / Carbon Last commit date
Cli 2 months ago Exceptions 2 months ago Lang 2 months ago Laravel 2 months ago List 2 months ago MessageFormatter 2 months ago PHPStan 2 months ago Traits 2 months ago AbstractTranslator.php 2 months ago Carbon.php 2 months ago CarbonConverterInterface.php 2 months ago CarbonImmutable.php 2 months ago CarbonInterface.php 2 months ago CarbonInterval.php 2 months ago CarbonPeriod.php 2 months ago CarbonPeriodImmutable.php 2 months ago CarbonTimeZone.php 2 months ago Factory.php 2 months ago FactoryImmutable.php 2 months ago Language.php 2 months ago Translator.php 2 months ago TranslatorImmutable.php 2 months ago TranslatorStrongTypeInterface.php 2 months ago
AbstractTranslator.php
325 lines
1 <?php
2
3 /**
4 * This file is part of the Carbon package.
5 *
6 * (c) Brian Nesbitt <brian@nesbot.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\Carbon;
12
13 use ProfilePressVendor\Carbon\MessageFormatter\MessageFormatterMapper;
14 use Closure;
15 use ReflectionException;
16 use ReflectionFunction;
17 use ProfilePressVendor\Symfony\Component\Translation;
18 use ProfilePressVendor\Symfony\Component\Translation\Formatter\MessageFormatterInterface;
19 use ProfilePressVendor\Symfony\Component\Translation\Loader\ArrayLoader;
20 abstract class AbstractTranslator extends Translation\Translator
21 {
22 /**
23 * Translator singletons for each language.
24 *
25 * @var array
26 */
27 protected static $singletons = [];
28 /**
29 * List of custom localized messages.
30 *
31 * @var array
32 */
33 protected $messages = [];
34 /**
35 * List of custom directories that contain translation files.
36 *
37 * @var string[]
38 */
39 protected $directories = [];
40 /**
41 * Set to true while constructing.
42 *
43 * @var bool
44 */
45 protected $initializing = \false;
46 /**
47 * List of locales aliases.
48 *
49 * @var array<string, string>
50 */
51 protected $aliases = ['me' => 'sr_Latn_ME', 'scr' => 'sh'];
52 /**
53 * Return a singleton instance of Translator.
54 *
55 * @param string|null $locale optional initial locale ("en" - english by default)
56 *
57 * @return static
58 */
59 public static function get($locale = null)
60 {
61 $locale = $locale ?: 'en';
62 $key = static::class === Translator::class ? $locale : static::class . '|' . $locale;
63 if (!isset(static::$singletons[$key])) {
64 static::$singletons[$key] = new static($locale);
65 }
66 return static::$singletons[$key];
67 }
68 public function __construct($locale, ?MessageFormatterInterface $formatter = null, $cacheDir = null, $debug = \false)
69 {
70 parent::setLocale($locale);
71 $this->initializing = \true;
72 $this->directories = [__DIR__ . '/Lang'];
73 $this->addLoader('array', new ArrayLoader());
74 parent::__construct($locale, new MessageFormatterMapper($formatter), $cacheDir, $debug);
75 $this->initializing = \false;
76 }
77 /**
78 * Returns the list of directories translation files are searched in.
79 *
80 * @return array
81 */
82 public function getDirectories(): array
83 {
84 return $this->directories;
85 }
86 /**
87 * Set list of directories translation files are searched in.
88 *
89 * @param array $directories new directories list
90 *
91 * @return $this
92 */
93 public function setDirectories(array $directories)
94 {
95 $this->directories = $directories;
96 return $this;
97 }
98 /**
99 * Add a directory to the list translation files are searched in.
100 *
101 * @param string $directory new directory
102 *
103 * @return $this
104 */
105 public function addDirectory(string $directory)
106 {
107 $this->directories[] = $directory;
108 return $this;
109 }
110 /**
111 * Remove a directory from the list translation files are searched in.
112 *
113 * @param string $directory directory path
114 *
115 * @return $this
116 */
117 public function removeDirectory(string $directory)
118 {
119 $search = rtrim(strtr($directory, '\\', '/'), '/');
120 return $this->setDirectories(array_filter($this->getDirectories(), function ($item) use ($search) {
121 return rtrim(strtr($item, '\\', '/'), '/') !== $search;
122 }));
123 }
124 /**
125 * Reset messages of a locale (all locale if no locale passed).
126 * Remove custom messages and reload initial messages from matching
127 * file in Lang directory.
128 *
129 * @param string|null $locale
130 *
131 * @return bool
132 */
133 public function resetMessages($locale = null)
134 {
135 if ($locale === null) {
136 $this->messages = [];
137 return \true;
138 }
139 $this->assertValidLocale($locale);
140 foreach ($this->getDirectories() as $directory) {
141 $data = @include \sprintf('%s/%s.php', rtrim($directory, '\/'), $locale);
142 if ($data !== \false) {
143 $this->messages[$locale] = $data;
144 $this->addResource('array', $this->messages[$locale], $locale);
145 return \true;
146 }
147 }
148 return \false;
149 }
150 /**
151 * Returns the list of files matching a given locale prefix (or all if empty).
152 *
153 * @param string $prefix prefix required to filter result
154 *
155 * @return array
156 */
157 public function getLocalesFiles($prefix = '')
158 {
159 $files = [];
160 foreach ($this->getDirectories() as $directory) {
161 $directory = rtrim($directory, '\/');
162 foreach (glob("{$directory}/{$prefix}*.php") as $file) {
163 $files[] = $file;
164 }
165 }
166 return array_unique($files);
167 }
168 /**
169 * Returns the list of internally available locales and already loaded custom locales.
170 * (It will ignore custom translator dynamic loading.)
171 *
172 * @param string $prefix prefix required to filter result
173 *
174 * @return array
175 */
176 public function getAvailableLocales($prefix = '')
177 {
178 $locales = [];
179 foreach ($this->getLocalesFiles($prefix) as $file) {
180 $locales[] = substr($file, strrpos($file, '/') + 1, -4);
181 }
182 return array_unique(array_merge($locales, array_keys($this->messages)));
183 }
184 protected function translate(?string $id, array $parameters = [], ?string $domain = null, ?string $locale = null): string
185 {
186 if ($domain === null) {
187 $domain = 'messages';
188 }
189 $catalogue = $this->getCatalogue($locale);
190 $format = $this instanceof TranslatorStrongTypeInterface ? $this->getFromCatalogue($catalogue, (string) $id, $domain) : $this->getCatalogue($locale)->get((string) $id, $domain);
191 // @codeCoverageIgnore
192 if ($format instanceof Closure) {
193 // @codeCoverageIgnoreStart
194 try {
195 $count = (new ReflectionFunction($format))->getNumberOfRequiredParameters();
196 } catch (ReflectionException $exception) {
197 $count = 0;
198 }
199 // @codeCoverageIgnoreEnd
200 return $format(...array_values($parameters), ...array_fill(0, max(0, $count - \count($parameters)), null));
201 }
202 return parent::trans($id, $parameters, $domain, $locale);
203 }
204 /**
205 * Init messages language from matching file in Lang directory.
206 *
207 * @param string $locale
208 *
209 * @return bool
210 */
211 protected function loadMessagesFromFile($locale)
212 {
213 return isset($this->messages[$locale]) || $this->resetMessages($locale);
214 }
215 /**
216 * Set messages of a locale and take file first if present.
217 *
218 * @param string $locale
219 * @param array $messages
220 *
221 * @return $this
222 */
223 public function setMessages($locale, $messages)
224 {
225 $this->loadMessagesFromFile($locale);
226 $this->addResource('array', $messages, $locale);
227 $this->messages[$locale] = array_merge($this->messages[$locale] ?? [], $messages);
228 return $this;
229 }
230 /**
231 * Set messages of the current locale and take file first if present.
232 *
233 * @param array $messages
234 *
235 * @return $this
236 */
237 public function setTranslations($messages)
238 {
239 return $this->setMessages($this->getLocale(), $messages);
240 }
241 /**
242 * Get messages of a locale, if none given, return all the
243 * languages.
244 *
245 * @param string|null $locale
246 *
247 * @return array
248 */
249 public function getMessages($locale = null)
250 {
251 return $locale === null ? $this->messages : $this->messages[$locale];
252 }
253 /**
254 * Set the current translator locale and indicate if the source locale file exists
255 *
256 * @param string $locale locale ex. en
257 *
258 * @return bool
259 */
260 public function setLocale($locale)
261 {
262 $locale = preg_replace_callback('/[-_]([a-z]{2,}|\d{2,})/', function ($matches) {
263 // _2-letters or YUE is a region, _3+-letters is a variant
264 $upper = strtoupper($matches[1]);
265 if ($upper === 'YUE' || $upper === 'ISO' || \strlen($upper) < 3) {
266 return "_{$upper}";
267 }
268 return '_' . ucfirst($matches[1]);
269 }, strtolower($locale));
270 $previousLocale = $this->getLocale();
271 if ($previousLocale === $locale && isset($this->messages[$locale])) {
272 return \true;
273 }
274 unset(static::$singletons[$previousLocale]);
275 if ($locale === 'auto') {
276 $completeLocale = setlocale(\LC_TIME, '0');
277 $locale = preg_replace('/^([^_.-]+).*$/', '$1', $completeLocale);
278 $locales = $this->getAvailableLocales($locale);
279 $completeLocaleChunks = preg_split('/[_.-]+/', $completeLocale);
280 $getScore = function ($language) use ($completeLocaleChunks) {
281 return self::compareChunkLists($completeLocaleChunks, preg_split('/[_.-]+/', $language));
282 };
283 usort($locales, function ($first, $second) use ($getScore) {
284 return $getScore($second) <=> $getScore($first);
285 });
286 $locale = $locales[0];
287 }
288 if (isset($this->aliases[$locale])) {
289 $locale = $this->aliases[$locale];
290 }
291 // If subtag (ex: en_CA) first load the macro (ex: en) to have a fallback
292 if (str_contains($locale, '_') && $this->loadMessagesFromFile($macroLocale = preg_replace('/^([^_]+).*$/', '$1', $locale))) {
293 parent::setLocale($macroLocale);
294 }
295 if (!$this->loadMessagesFromFile($locale) && !$this->initializing) {
296 return \false;
297 }
298 parent::setLocale($locale);
299 return \true;
300 }
301 /**
302 * Show locale on var_dump().
303 *
304 * @return array
305 */
306 public function __debugInfo()
307 {
308 return ['locale' => $this->getLocale()];
309 }
310 private static function compareChunkLists($referenceChunks, $chunks)
311 {
312 $score = 0;
313 foreach ($referenceChunks as $index => $chunk) {
314 if (!isset($chunks[$index])) {
315 $score++;
316 continue;
317 }
318 if (strtolower($chunks[$index]) === strtolower($chunk)) {
319 $score += 10;
320 }
321 }
322 return $score;
323 }
324 }
325