/**
  * Formats the message with the help of php intl extension.
  * 
  * @param string $locale
  * @param string $message
  * @param array(string=>mixed) $parameters
  * @return string
  * @throws CannotInstantiateFormatterException If the message pattern cannot be used.
  * @throws CannotFormatException If an error occurs during formatting.
  */
 public function format($locale, $message, array $parameters)
 {
     if (empty($message)) {
         // Empty strings are not accepted as message pattern by the \MessageFormatter.
         return $message;
     }
     try {
         $formatter = new \MessageFormatter($locale, $message);
     } catch (\Exception $e) {
         throw new CannotInstantiateFormatterException($e->getMessage(), $e->getCode(), $e);
     }
     if (!$formatter) {
         throw new CannotInstantiateFormatterException(intl_get_error_message(), intl_get_error_code());
     }
     $result = $formatter->format($parameters);
     if ($result === false) {
         throw new CannotFormatException($formatter->getErrorMessage(), $formatter->getErrorCode());
     }
     return $result;
 }
예제 #2
0
 /**
  * Parses an input string according to an [ICU message format](http://userguide.icu-project.org/formatparse/messages) pattern.
  *
  * It uses the PHP intl extension's [MessageFormatter::parse()](http://www.php.net/manual/en/messageformatter.parsemessage.php)
  * and adds support for named arguments.
  * Usage of this method requires PHP intl extension to be installed.
  *
  * @param string $pattern The pattern to use for parsing the message.
  * @param string $message The message to parse, conforming to the pattern.
  * @param string $language The locale to use for formatting locale-dependent parts
  * @return array|boolean An array containing items extracted, or `FALSE` on error.
  * @throws \yii\base\NotSupportedException when PHP intl extension is not installed.
  */
 public function parse($pattern, $message, $language)
 {
     $this->_errorCode = 0;
     $this->_errorMessage = '';
     if (!class_exists('MessageFormatter', false)) {
         throw new NotSupportedException('You have to install PHP intl extension to use this feature.');
     }
     // replace named arguments
     if (($tokens = self::tokenizePattern($pattern)) === false) {
         $this->_errorCode = -1;
         $this->_errorMessage = "Message pattern is invalid.";
         return false;
     }
     $map = [];
     foreach ($tokens as $i => $token) {
         if (is_array($token)) {
             $param = trim($token[0]);
             if (!isset($map[$param])) {
                 $map[$param] = count($map);
             }
             $token[0] = $map[$param];
             $tokens[$i] = '{' . implode(',', $token) . '}';
         }
     }
     $pattern = implode('', $tokens);
     $map = array_flip($map);
     $formatter = new \MessageFormatter($language, $pattern);
     if ($formatter === null) {
         $this->_errorCode = -1;
         $this->_errorMessage = "Message pattern is invalid.";
         return false;
     }
     $result = $formatter->parse($message);
     if ($result === false) {
         $this->_errorCode = $formatter->getErrorCode();
         $this->_errorMessage = $formatter->getErrorMessage();
         return false;
     } else {
         $values = [];
         foreach ($result as $key => $value) {
             $values[$map[$key]] = $value;
         }
         return $values;
     }
 }