114 lines
3.7KB

  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. $language = str_replace('-', '_', basename(dirname($filePath)));
  52. $headers = [
  53. 'msgid ""',
  54. 'msgstr ""',
  55. '"Project-Id-Version: \n"',
  56. '"POT-Creation-Date: \n"',
  57. '"PO-Revision-Date: \n"',
  58. '"Last-Translator: \n"',
  59. '"Language-Team: \n"',
  60. '"Language: ' . $language . '\n"',
  61. '"MIME-Version: 1.0\n"',
  62. '"Content-Type: text/plain; charset=' . Yii::$app->charset . '\n"',
  63. '"Content-Transfer-Encoding: 8bit\n"'
  64. ];
  65. $content = implode("\n", $headers) . "\n\n";
  66. foreach ($messages as $id => $message) {
  67. $separatorPosition = strpos($id, chr(4));
  68. if ($separatorPosition !== false) {
  69. $content .= 'msgctxt "' . substr($id, 0, $separatorPosition) . "\"\n";
  70. $id = substr($id, $separatorPosition + 1);
  71. }
  72. $content .= 'msgid "' . $this->encode($id) . "\"\n";
  73. $content .= 'msgstr "' . $this->encode($message) . "\"\n\n";
  74. }
  75. file_put_contents($filePath, $content);
  76. }
  77. /**
  78. * Encodes special characters in a message.
  79. * @param string $string message to be encoded
  80. * @return string the encoded message
  81. */
  82. protected function encode($string)
  83. {
  84. return str_replace(
  85. ['"', "\n", "\t", "\r"],
  86. ['\\"', '\\n', '\\t', '\\r'],
  87. $string
  88. );
  89. }
  90. /**
  91. * Decodes special characters in a message.
  92. * @param string $string message to be decoded
  93. * @return string the decoded message
  94. */
  95. protected function decode($string)
  96. {
  97. $string = preg_replace(
  98. ['/"\s+"/', '/\\\\n/', '/\\\\r/', '/\\\\t/', '/\\\\"/'],
  99. ['', "\n", "\r", "\t", '"'],
  100. $string
  101. );
  102. return substr(rtrim($string), 1, -1);
  103. }
  104. }