You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

415 lines
16KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\i18n;
  8. use yii\base\Component;
  9. use yii\base\NotSupportedException;
  10. /**
  11. * MessageFormatter allows formatting messages via [ICU message format](http://userguide.icu-project.org/formatparse/messages)
  12. *
  13. * This class enhances the message formatter class provided by the PHP intl extension.
  14. *
  15. * The following enhancements are provided:
  16. *
  17. * - It accepts named arguments and mixed numeric and named arguments.
  18. * - Issues no error when an insufficient number of arguments have been provided. Instead, the placeholders will not be
  19. * substituted.
  20. * - Fixes PHP 5.5 weird placeholder replacement in case no arguments are provided at all (https://bugs.php.net/bug.php?id=65920).
  21. * - Offers limited support for message formatting in case PHP intl extension is not installed.
  22. * However it is highly recommended that you install [PHP intl extension](http://php.net/manual/en/book.intl.php) if you want
  23. * to use MessageFormatter features.
  24. *
  25. * The fallback implementation only supports the following message formats:
  26. * - plural formatting for english ('one' and 'other' selectors)
  27. * - select format
  28. * - simple parameters
  29. * - integer number parameters
  30. *
  31. * The fallback implementation does NOT support the ['apostrophe-friendly' syntax](http://www.php.net/manual/en/messageformatter.formatmessage.php).
  32. * Also messages that are working with the fallback implementation are not necessarily compatible with the
  33. * PHP intl MessageFormatter so do not rely on the fallback if you are able to install intl extension somehow.
  34. *
  35. * @property string $errorCode Code of the last error. This property is read-only.
  36. * @property string $errorMessage Description of the last error. This property is read-only.
  37. *
  38. * @author Alexander Makarov <sam@rmcreative.ru>
  39. * @author Carsten Brandt <mail@cebe.cc>
  40. * @since 2.0
  41. */
  42. class MessageFormatter extends Component
  43. {
  44. private $_errorCode = 0;
  45. private $_errorMessage = '';
  46. /**
  47. * Get the error code from the last operation
  48. * @link http://php.net/manual/en/messageformatter.geterrorcode.php
  49. * @return string Code of the last error.
  50. */
  51. public function getErrorCode()
  52. {
  53. return $this->_errorCode;
  54. }
  55. /**
  56. * Get the error text from the last operation
  57. * @link http://php.net/manual/en/messageformatter.geterrormessage.php
  58. * @return string Description of the last error.
  59. */
  60. public function getErrorMessage()
  61. {
  62. return $this->_errorMessage;
  63. }
  64. /**
  65. * Formats a message via [ICU message format](http://userguide.icu-project.org/formatparse/messages)
  66. *
  67. * It uses the PHP intl extension's [MessageFormatter](http://www.php.net/manual/en/class.messageformatter.php)
  68. * and works around some issues.
  69. * If PHP intl is not installed a fallback will be used that supports a subset of the ICU message format.
  70. *
  71. * @param string $pattern The pattern string to insert parameters into.
  72. * @param array $params The array of name value pairs to insert into the format string.
  73. * @param string $language The locale to use for formatting locale-dependent parts
  74. * @return string|boolean The formatted pattern string or `FALSE` if an error occurred
  75. */
  76. public function format($pattern, $params, $language)
  77. {
  78. $this->_errorCode = 0;
  79. $this->_errorMessage = '';
  80. if ($params === []) {
  81. return $pattern;
  82. }
  83. if (!class_exists('MessageFormatter', false)) {
  84. return $this->fallbackFormat($pattern, $params, $language);
  85. }
  86. if (version_compare(PHP_VERSION, '5.5.0', '<') || version_compare(INTL_ICU_VERSION, '4.8', '<')) {
  87. // replace named arguments
  88. $pattern = $this->replaceNamedArguments($pattern, $params, $newParams);
  89. $params = $newParams;
  90. }
  91. $formatter = new \MessageFormatter($language, $pattern);
  92. if ($formatter === null) {
  93. $this->_errorCode = intl_get_error_code();
  94. $this->_errorMessage = "Message pattern is invalid: " . intl_get_error_message();
  95. return false;
  96. }
  97. $result = $formatter->format($params);
  98. if ($result === false) {
  99. $this->_errorCode = $formatter->getErrorCode();
  100. $this->_errorMessage = $formatter->getErrorMessage();
  101. return false;
  102. } else {
  103. return $result;
  104. }
  105. }
  106. /**
  107. * Parses an input string according to an [ICU message format](http://userguide.icu-project.org/formatparse/messages) pattern.
  108. *
  109. * It uses the PHP intl extension's [MessageFormatter::parse()](http://www.php.net/manual/en/messageformatter.parsemessage.php)
  110. * and adds support for named arguments.
  111. * Usage of this method requires PHP intl extension to be installed.
  112. *
  113. * @param string $pattern The pattern to use for parsing the message.
  114. * @param string $message The message to parse, conforming to the pattern.
  115. * @param string $language The locale to use for formatting locale-dependent parts
  116. * @return array|boolean An array containing items extracted, or `FALSE` on error.
  117. * @throws \yii\base\NotSupportedException when PHP intl extension is not installed.
  118. */
  119. public function parse($pattern, $message, $language)
  120. {
  121. $this->_errorCode = 0;
  122. $this->_errorMessage = '';
  123. if (!class_exists('MessageFormatter', false)) {
  124. throw new NotSupportedException('You have to install PHP intl extension to use this feature.');
  125. }
  126. // replace named arguments
  127. if (($tokens = self::tokenizePattern($pattern)) === false) {
  128. $this->_errorCode = -1;
  129. $this->_errorMessage = "Message pattern is invalid.";
  130. return false;
  131. }
  132. $map = [];
  133. foreach ($tokens as $i => $token) {
  134. if (is_array($token)) {
  135. $param = trim($token[0]);
  136. if (!isset($map[$param])) {
  137. $map[$param] = count($map);
  138. }
  139. $token[0] = $map[$param];
  140. $tokens[$i] = '{' . implode(',', $token) . '}';
  141. }
  142. }
  143. $pattern = implode('', $tokens);
  144. $map = array_flip($map);
  145. $formatter = new \MessageFormatter($language, $pattern);
  146. if ($formatter === null) {
  147. $this->_errorCode = -1;
  148. $this->_errorMessage = "Message pattern is invalid.";
  149. return false;
  150. }
  151. $result = $formatter->parse($message);
  152. if ($result === false) {
  153. $this->_errorCode = $formatter->getErrorCode();
  154. $this->_errorMessage = $formatter->getErrorMessage();
  155. return false;
  156. } else {
  157. $values = [];
  158. foreach ($result as $key => $value) {
  159. $values[$map[$key]] = $value;
  160. }
  161. return $values;
  162. }
  163. }
  164. /**
  165. * Replace named placeholders with numeric placeholders and quote unused.
  166. *
  167. * @param string $pattern The pattern string to replace things into.
  168. * @param array $givenParams The array of values to insert into the format string.
  169. * @param array $resultingParams Modified array of parameters.
  170. * @param array $map
  171. * @return string The pattern string with placeholders replaced.
  172. */
  173. private function replaceNamedArguments($pattern, $givenParams, &$resultingParams, &$map = [])
  174. {
  175. if (($tokens = self::tokenizePattern($pattern)) === false) {
  176. return false;
  177. }
  178. foreach ($tokens as $i => $token) {
  179. if (!is_array($token)) {
  180. continue;
  181. }
  182. $param = trim($token[0]);
  183. if (isset($givenParams[$param])) {
  184. // if param is given, replace it with a number
  185. if (!isset($map[$param])) {
  186. $map[$param] = count($map);
  187. // make sure only used params are passed to format method
  188. $resultingParams[$map[$param]] = $givenParams[$param];
  189. }
  190. $token[0] = $map[$param];
  191. $quote = "";
  192. } else {
  193. // quote unused token
  194. $quote = "'";
  195. }
  196. $type = isset($token[1]) ? trim($token[1]) : 'none';
  197. // replace plural and select format recursively
  198. if ($type == 'plural' || $type == 'select') {
  199. if (!isset($token[2])) {
  200. return false;
  201. }
  202. if (($subtokens = self::tokenizePattern($token[2])) === false) {
  203. return false;
  204. }
  205. $c = count($subtokens);
  206. for ($k = 0; $k + 1 < $c; $k++) {
  207. if (is_array($subtokens[$k]) || !is_array($subtokens[++$k])) {
  208. return false;
  209. }
  210. $subpattern = $this->replaceNamedArguments(implode(',', $subtokens[$k]), $givenParams, $resultingParams, $map);
  211. $subtokens[$k] = $quote . '{' . $quote . $subpattern . $quote . '}' . $quote;
  212. }
  213. $token[2] = implode('', $subtokens);
  214. }
  215. $tokens[$i] = $quote . '{' . $quote . implode(',', $token) . $quote . '}' . $quote;
  216. }
  217. return implode('', $tokens);
  218. }
  219. /**
  220. * Fallback implementation for MessageFormatter::formatMessage
  221. * @param string $pattern The pattern string to insert things into.
  222. * @param array $args The array of values to insert into the format string
  223. * @param string $locale The locale to use for formatting locale-dependent parts
  224. * @return string|boolean The formatted pattern string or `FALSE` if an error occurred
  225. */
  226. protected function fallbackFormat($pattern, $args, $locale)
  227. {
  228. if (($tokens = self::tokenizePattern($pattern)) === false) {
  229. $this->_errorCode = -1;
  230. $this->_errorMessage = "Message pattern is invalid.";
  231. return false;
  232. }
  233. foreach ($tokens as $i => $token) {
  234. if (is_array($token)) {
  235. if (($tokens[$i] = $this->parseToken($token, $args, $locale)) === false) {
  236. $this->_errorCode = -1;
  237. $this->_errorMessage = "Message pattern is invalid.";
  238. return false;
  239. }
  240. }
  241. }
  242. return implode('', $tokens);
  243. }
  244. /**
  245. * Tokenizes a pattern by separating normal text from replaceable patterns
  246. * @param string $pattern patter to tokenize
  247. * @return array|boolean array of tokens or false on failure
  248. */
  249. private static function tokenizePattern($pattern)
  250. {
  251. $depth = 1;
  252. if (($start = $pos = mb_strpos($pattern, '{')) === false) {
  253. return [$pattern];
  254. }
  255. $tokens = [mb_substr($pattern, 0, $pos)];
  256. while (true) {
  257. $open = mb_strpos($pattern, '{', $pos + 1);
  258. $close = mb_strpos($pattern, '}', $pos + 1);
  259. if ($open === false && $close === false) {
  260. break;
  261. }
  262. if ($open === false) {
  263. $open = mb_strlen($pattern);
  264. }
  265. if ($close > $open) {
  266. $depth++;
  267. $pos = $open;
  268. } else {
  269. $depth--;
  270. $pos = $close;
  271. }
  272. if ($depth == 0) {
  273. $tokens[] = explode(',', mb_substr($pattern, $start + 1, $pos - $start - 1), 3);
  274. $start = $pos + 1;
  275. $tokens[] = mb_substr($pattern, $start, $open - $start);
  276. $start = $open;
  277. }
  278. }
  279. if ($depth != 0) {
  280. return false;
  281. }
  282. return $tokens;
  283. }
  284. /**
  285. * Parses a token
  286. * @param array $token the token to parse
  287. * @param array $args arguments to replace
  288. * @param string $locale the locale
  289. * @return bool|string parsed token or false on failure
  290. * @throws \yii\base\NotSupportedException when unsupported formatting is used.
  291. */
  292. private function parseToken($token, $args, $locale)
  293. {
  294. // parsing pattern based on ICU grammar:
  295. // http://icu-project.org/apiref/icu4c/classMessageFormat.html#details
  296. $param = trim($token[0]);
  297. if (isset($args[$param])) {
  298. $arg = $args[$param];
  299. } else {
  300. return '{' . implode(',', $token) . '}';
  301. }
  302. $type = isset($token[1]) ? trim($token[1]) : 'none';
  303. switch ($type) {
  304. case 'date':
  305. case 'time':
  306. case 'spellout':
  307. case 'ordinal':
  308. case 'duration':
  309. case 'choice':
  310. case 'selectordinal':
  311. throw new NotSupportedException("Message format '$type' is not supported. You have to install PHP intl extension to use this feature.");
  312. case 'number':
  313. if (is_int($arg) && (!isset($token[2]) || trim($token[2]) == 'integer')) {
  314. return $arg;
  315. }
  316. throw new NotSupportedException("Message format 'number' is only supported for integer values. You have to install PHP intl extension to use this feature.");
  317. case 'none':
  318. return $arg;
  319. case 'select':
  320. /* http://icu-project.org/apiref/icu4c/classicu_1_1SelectFormat.html
  321. selectStyle = (selector '{' message '}')+
  322. */
  323. if (!isset($token[2])) {
  324. return false;
  325. }
  326. $select = self::tokenizePattern($token[2]);
  327. $c = count($select);
  328. $message = false;
  329. for ($i = 0; $i + 1 < $c; $i++) {
  330. if (is_array($select[$i]) || !is_array($select[$i + 1])) {
  331. return false;
  332. }
  333. $selector = trim($select[$i++]);
  334. if ($message === false && $selector == 'other' || $selector == $arg) {
  335. $message = implode(',', $select[$i]);
  336. }
  337. }
  338. if ($message !== false) {
  339. return $this->fallbackFormat($message, $args, $locale);
  340. }
  341. break;
  342. case 'plural':
  343. /* http://icu-project.org/apiref/icu4c/classicu_1_1PluralFormat.html
  344. pluralStyle = [offsetValue] (selector '{' message '}')+
  345. offsetValue = "offset:" number
  346. selector = explicitValue | keyword
  347. explicitValue = '=' number // adjacent, no white space in between
  348. keyword = [^[[:Pattern_Syntax:][:Pattern_White_Space:]]]+
  349. message: see MessageFormat
  350. */
  351. if (!isset($token[2])) {
  352. return false;
  353. }
  354. $plural = self::tokenizePattern($token[2]);
  355. $c = count($plural);
  356. $message = false;
  357. $offset = 0;
  358. for ($i = 0; $i + 1 < $c; $i++) {
  359. if (is_array($plural[$i]) || !is_array($plural[$i + 1])) {
  360. return false;
  361. }
  362. $selector = trim($plural[$i++]);
  363. if ($i == 1 && strncmp($selector, 'offset:', 7) === 0) {
  364. $offset = (int) trim(mb_substr($selector, 7, ($pos = mb_strpos(str_replace(["\n", "\r", "\t"], ' ', $selector), ' ', 7)) - 7));
  365. $selector = trim(mb_substr($selector, $pos + 1));
  366. }
  367. if ($message === false && $selector == 'other' ||
  368. $selector[0] == '=' && (int) mb_substr($selector, 1) == $arg ||
  369. $selector == 'one' && $arg - $offset == 1
  370. ) {
  371. $message = implode(',', str_replace('#', $arg - $offset, $plural[$i]));
  372. }
  373. }
  374. if ($message !== false) {
  375. return $this->fallbackFormat($message, $args, $locale);
  376. }
  377. break;
  378. }
  379. return false;
  380. }
  381. }