Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

100 lines
3.1KB

  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;
  9. /**
  10. * GettextPoFile represents a PO Gettext message file.
  11. *
  12. * @author Qiang Xue <qiang.xue@gmail.com>
  13. * @since 2.0
  14. */
  15. class GettextPoFile extends GettextFile
  16. {
  17. /**
  18. * Loads messages from a PO file.
  19. * @param string $filePath file path
  20. * @param string $context message context
  21. * @return array message translations. Array keys are source messages and array values are translated messages:
  22. * source message => translated message.
  23. */
  24. public function load($filePath, $context)
  25. {
  26. $pattern = '/(msgctxt\s+"(.*?(?<!\\\\))")?\s+' // context
  27. . 'msgid\s+((?:".*(?<!\\\\)"\s*)+)\s+' // message ID, i.e. original string
  28. . 'msgstr\s+((?:".*(?<!\\\\)"\s*)+)/'; // translated string
  29. $content = file_get_contents($filePath);
  30. $matches = [];
  31. $matchCount = preg_match_all($pattern, $content, $matches);
  32. $messages = [];
  33. for ($i = 0; $i < $matchCount; ++$i) {
  34. if ($matches[2][$i] == $context) {
  35. $id = $this->decode($matches[3][$i]);
  36. $message = $this->decode($matches[4][$i]);
  37. $messages[$id] = $message;
  38. }
  39. }
  40. return $messages;
  41. }
  42. /**
  43. * Saves messages to a PO file.
  44. * @param string $filePath file path
  45. * @param array $messages message translations. Array keys are source messages and array values are
  46. * translated messages: source message => translated message. Note if the message has a context,
  47. * the message ID must be prefixed with the context with chr(4) as the separator.
  48. */
  49. public function save($filePath, $messages)
  50. {
  51. $content = '';
  52. foreach ($messages as $id => $message) {
  53. $separatorPosition = strpos($id, chr(4));
  54. if ($separatorPosition !== false) {
  55. $content .= 'msgctxt "' . substr($id, 0, $separatorPosition) . "\"\n";
  56. $id = substr($id, $separatorPosition + 1);
  57. }
  58. $content .= 'msgid "' . $this->encode($id) . "\"\n";
  59. $content .= 'msgstr "' . $this->encode($message) . "\"\n\n";
  60. }
  61. file_put_contents($filePath, $content);
  62. }
  63. /**
  64. * Encodes special characters in a message.
  65. * @param string $string message to be encoded
  66. * @return string the encoded message
  67. */
  68. protected function encode($string)
  69. {
  70. return str_replace(
  71. ['"', "\n", "\t", "\r"],
  72. ['\\"', '\\n', '\\t', '\\r'],
  73. $string
  74. );
  75. }
  76. /**
  77. * Decodes special characters in a message.
  78. * @param string $string message to be decoded
  79. * @return string the decoded message
  80. */
  81. protected function decode($string)
  82. {
  83. $string = preg_replace(
  84. ['/"\s+"/', '/\\\\n/', '/\\\\r/', '/\\\\t/', '/\\\\"/'],
  85. ['', "\n", "\r", "\t", '"'],
  86. $string
  87. );
  88. return substr(rtrim($string), 1, -1);
  89. }
  90. }