File manager - Edit - /home/opticamezl/www/newok/translation.tar
Back
src/Translator.php 0000644 00000002645 15174212770 0010212 0 ustar 00 <?php namespace YOOtheme; interface Translator { /** * Returns the current locale. * * @return string */ public function getLocale(); /** * Sets the current locale. * * @param string $locale */ public function setLocale($locale); /** * Gets all Resources. * * @return array */ public function getResources(); /** * Adds a Resource. * * @param mixed $resource * @param string $locale * * @return $this */ public function addResource($resource, $locale = null); /** * Translates the given message. * * @param string $id * @param array $parameters * @param string $locale * * @return string */ public function trans($id, array $parameters = [], $locale = null); /** * Translates the given choice message by choosing a translation according to a number. * * @param string $id The message id (may also be an object that can be cast to string) * @param int $number The number to use to find the indice of the message * @param array $parameters An array of parameters for the message * @param string|null $locale The locale or null to use the default * * @return string The translated string */ public function transChoice($id, $number, array $parameters = [], $locale = null); } src/Translation/Interval.php 0000644 00000004371 15174212770 0012141 0 ustar 00 <?php namespace YOOtheme\Translation; /** * @link https://github.com/symfony/Translation * * @copyright Copyright (c) Fabien Potencier <fabien@symfony.com> */ class Interval { /** * Tests if the given number is in the math interval. * * @param int $number A number * @param string $interval An interval * * @throws \InvalidArgumentException * * @return bool */ public static function test($number, $interval) { $interval = trim($interval); if (!preg_match('/^' . self::getIntervalRegexp() . '$/x', $interval, $matches)) { throw new \InvalidArgumentException( sprintf('"%s" is not a valid interval.', $interval), ); } if ($matches[1]) { foreach (explode(',', $matches[2]) as $n) { if ($number == $n) { return true; } } } else { $leftNumber = self::convertNumber($matches['left']); $rightNumber = self::convertNumber($matches['right']); return ('[' === $matches['left_delimiter'] ? $number >= $leftNumber : $number > $leftNumber) && (']' === $matches['right_delimiter'] ? $number <= $rightNumber : $number < $rightNumber); } return false; } /** * Returns a Regexp that matches valid intervals. * * @return string A Regexp (without the delimiters) */ public static function getIntervalRegexp() { return <<<EOF ({\s* (\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*) \s*}) | (?P<left_delimiter>[\[\]]) \s* (?P<left>-Inf|\-?\d+(\.\d+)?) \s*,\s* (?P<right>\+?Inf|\-?\d+(\.\d+)?) \s* (?P<right_delimiter>[\[\]]) EOF; } private static function convertNumber($number) { if ('-Inf' === $number) { return log(0); } if ('+Inf' === $number || 'Inf' === $number) { return -log(0); } return (float) $number; } } src/Translation/PluralizationRules.php 0000644 00000015663 15174212770 0014233 0 ustar 00 <?php namespace YOOtheme\Translation; /** * @link https://github.com/symfony/Translation * * @copyright Copyright (c) Fabien Potencier <fabien@symfony.com> */ class PluralizationRules { private static $rules = []; /** * Returns the plural position to use for the given locale and number. * * @param int $number The number * @param string $locale The locale * * @return int The plural position */ public static function get($number, $locale) { if ('pt_BR' === $locale) { // temporary set a locale for brazilian $locale = 'xbr'; } if (strlen($locale) > 3) { $locale = substr($locale, 0, -strlen(strrchr($locale, '_'))); } if (isset(self::$rules[$locale])) { $return = call_user_func(self::$rules[$locale], $number); if (!is_int($return) || $return < 0) { return 0; } return $return; } /* * The plural rules are derived from code of the Zend Framework (2010-09-25), * which is subject to the new BSD license (http://framework.zend.com/license/new-bsd). * Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) */ switch ($locale) { // case 'bo': // case 'dz': // case 'id': // case 'ja': // case 'jv': // case 'ka': // case 'km': // case 'kn': // case 'ko': // case 'ms': // case 'th': // case 'tr': // case 'vi': // case 'zh': // return 0; // break; case 'af': case 'az': case 'bn': case 'bg': case 'ca': case 'da': case 'de': case 'el': case 'en': case 'eo': case 'es': case 'et': case 'eu': case 'fa': case 'fi': case 'fo': case 'fur': case 'fy': case 'gl': case 'gu': case 'ha': case 'he': case 'hu': case 'is': case 'it': case 'ku': case 'lb': case 'ml': case 'mn': case 'mr': case 'nah': case 'nb': case 'ne': case 'nl': case 'nn': case 'no': case 'om': case 'or': case 'pa': case 'pap': case 'ps': case 'pt': case 'so': case 'sq': case 'sv': case 'sw': case 'ta': case 'te': case 'tk': case 'ur': case 'zu': return $number == 1 ? 0 : 1; case 'am': case 'bh': case 'fil': case 'fr': case 'gun': case 'hi': case 'ln': case 'mg': case 'nso': case 'xbr': case 'ti': case 'wa': return $number == 0 || $number == 1 ? 0 : 1; case 'be': case 'bs': case 'hr': case 'ru': case 'sr': case 'uk': return $number % 10 == 1 && $number % 100 != 11 ? 0 : ($number % 10 >= 2 && $number % 10 <= 4 && ($number % 100 < 10 || $number % 100 >= 20) ? 1 : 2); case 'cs': case 'sk': return $number == 1 ? 0 : ($number >= 2 && $number <= 4 ? 1 : 2); case 'ga': return $number == 1 ? 0 : ($number == 2 ? 1 : 2); case 'lt': return $number % 10 == 1 && $number % 100 != 11 ? 0 : ($number % 10 >= 2 && ($number % 100 < 10 || $number % 100 >= 20) ? 1 : 2); case 'sl': return $number % 100 == 1 ? 0 : ($number % 100 == 2 ? 1 : ($number % 100 == 3 || $number % 100 == 4 ? 2 : 3)); case 'mk': return $number % 10 == 1 ? 0 : 1; case 'mt': return $number == 1 ? 0 : ($number == 0 || ($number % 100 > 1 && $number % 100 < 11) ? 1 : ($number % 100 > 10 && $number % 100 < 20 ? 2 : 3)); case 'lv': return $number == 0 ? 0 : ($number % 10 == 1 && $number % 100 != 11 ? 1 : 2); case 'pl': return $number == 1 ? 0 : ($number % 10 >= 2 && $number % 10 <= 4 && ($number % 100 < 12 || $number % 100 > 14) ? 1 : 2); case 'cy': return $number == 1 ? 0 : ($number == 2 ? 1 : ($number == 8 || $number == 11 ? 2 : 3)); case 'ro': return $number == 1 ? 0 : ($number == 0 || ($number % 100 > 0 && $number % 100 < 20) ? 1 : 2); case 'ar': return $number == 0 ? 0 : ($number == 1 ? 1 : ($number == 2 ? 2 : ($number % 100 >= 3 && $number % 100 <= 10 ? 3 : 4))); default: return 0; } } /** * Overrides the default plural rule for a given locale. * * @param string $rule A PHP callable * @param string $locale The locale * * @throws \LogicException */ public static function set($rule, $locale) { if ('pt_BR' === $locale) { // temporary set a locale for brazilian $locale = 'xbr'; } if (strlen($locale) > 3) { $locale = substr($locale, 0, -strlen(strrchr($locale, '_'))); } if (!is_callable($rule)) { throw new \LogicException('The given rule can not be called'); } self::$rules[$locale] = $rule; } } src/Translation/Translator.php 0000644 00000010505 15174212770 0012502 0 ustar 00 <?php namespace YOOtheme\Translation; use YOOtheme\Translator as TranslatorInterface; class Translator implements TranslatorInterface { /** * @var string */ protected $locale; /** * @var array */ protected $resources = []; /** * @inheritdoc */ public function getLocale() { return $this->locale; } /** * @inheritdoc */ public function setLocale($locale) { $this->locale = $locale; } /** * Gets a Resource. * * @param string $locale * * @return array */ public function getResource($locale = null) { if ($locale === null) { $locale = $this->getLocale(); } return $this->resources[$locale] ?? []; } /** * @inheritdoc */ public function getResources() { return $this->resources; } /** * @inheritdoc */ public function addResource($resource, $locale = null) { if ($locale === null) { $locale = $this->getLocale(); } if (is_string($resource) && is_file($resource)) { $resource = json_decode(file_get_contents($resource), true); } else { $resource = []; } $this->resources[$locale] = isset($this->resources[$locale]) ? array_replace($this->resources[$locale], $resource) : $resource; return $this; } /** * @inheritdoc */ public function trans($id, array $parameters = [], $locale = null) { if ($locale === null) { $locale = $this->getLocale(); } $id = (string) $id; if (isset($this->resources[$locale][$id])) { return strtr($this->resources[$locale][$id], $parameters); } return strtr($id, $parameters); } /** * @inheritdoc */ public function transChoice($id, $number, array $parameters = [], $locale = null) { if (null === $locale) { $locale = $this->getLocale(); } return strtr( $this->choose($this->trans($id, [], $locale), (int) $number, $locale), $parameters, ); } /** * Returns the correct portion of the message based on the given number. * * @param string $message The message being translated * @param int $number The number of items represented for the message * @param string $locale The locale to use for choosing * * @throws \InvalidArgumentException * * @return string */ public function choose($message, $number, $locale) { $parts = explode('|', $message); $explicitRules = []; $standardRules = []; foreach ($parts as $part) { $part = trim($part); if ( preg_match( '/^(?P<interval>' . Interval::getIntervalRegexp() . ')\s*(?P<message>.*?)$/x', $part, $matches, ) ) { $explicitRules[$matches['interval']] = $matches['message']; } elseif (preg_match('/^\w+\:\s*(.*?)$/', $part, $matches)) { $standardRules[] = $matches[1]; } else { $standardRules[] = $part; } } // try to match an explicit rule, then fallback to the standard ones foreach ($explicitRules as $interval => $m) { if (Interval::test($number, $interval)) { return $m; } } $position = PluralizationRules::get($number, $locale); if (!isset($standardRules[$position])) { // when there's exactly one rule given, and that rule is a standard // rule, use this rule if (1 === count($parts) && isset($standardRules[0])) { return $standardRules[0]; } throw new \InvalidArgumentException( sprintf( 'Unable to choose a translation for "%s" with locale "%s" for value "%d". Double check that this translation has the correct plural options (e.g. "There is one apple|There are %%count%% apples").', $message, $locale, $number, ), ); } return $standardRules[$position]; } } functions.php 0000644 00000001766 15174212770 0007305 0 ustar 00 <?php namespace YOOtheme; /** * Translates the given message. * * @param string $id * @param array $parameters * @param string $locale * * @return string */ function trans($id, array $parameters = [], $locale = null) { $app = Application::getInstance(); return $app(Translator::class)->trans($id, $parameters, $locale); } /** * Translates the given choice message by choosing a translation according to a number. * * @param string $id The message id (may also be an object that can be cast to string) * @param int $number The number to use to find the indice of the message * @param array $parameters An array of parameters for the message * @param string|null $locale The locale or null to use the default * * @return string The translated string */ function transChoice($id, $number, array $parameters = [], $locale = null) { $app = Application::getInstance(); return $app(Translator::class)->trans($id, $number, $parameters, $locale); } bootstrap.php 0000644 00000000434 15174212770 0007301 0 ustar 00 <?php namespace YOOtheme; return [ 'services' => [ Translator::class => function (Config $config) { $translator = new Translation\Translator(); $translator->setLocale($config('locale.code')); return $translator; }, ], ];
| ver. 1.4 |
Github
|
.
| PHP 8.3.23 | Generation time: 0 |
proxy
|
phpinfo
|
Settings