PageRenderTime 48ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/i18n/gettext/CGettextPoFile.php

https://bitbucket.org/diegoaraujo/alexikeda
PHP | 89 lines | 58 code | 2 blank | 29 comment | 3 complexity | 61cbb4fa9a9382af692ebbfe44a353c8 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, BSD-2-Clause
  1. <?php
  2. /**
  3. * CGettextPoFile class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright &copy; 2008-2011 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CGettextPoFile represents a PO Gettext message file.
  12. *
  13. * @author Qiang Xue <qiang.xue@gmail.com>
  14. * @version $Id: CGettextPoFile.php 2798 2011-01-01 19:29:03Z qiang.xue $
  15. * @package system.i18n.gettext
  16. * @since 1.0
  17. */
  18. class CGettextPoFile extends CGettextFile
  19. {
  20. /**
  21. * Loads messages from a PO file.
  22. * @param string $file file path
  23. * @param string $context message context
  24. * @return array message translations (source message => translated message)
  25. */
  26. public function load($file,$context)
  27. {
  28. $pattern='/(msgctxt\s+"(.*?(?<!\\\\))")?'
  29. . '\s+msgid\s+"(.*?(?<!\\\\))"'
  30. . '\s+msgstr\s+"(.*?(?<!\\\\))"/';
  31. $content=file_get_contents($file);
  32. $n=preg_match_all($pattern,$content,$matches);
  33. $messages=array();
  34. for($i=0;$i<$n;++$i)
  35. {
  36. if($matches[2][$i]===$context)
  37. {
  38. $id=$this->decode($matches[3][$i]);
  39. $message=$this->decode($matches[4][$i]);
  40. $messages[$id]=$message;
  41. }
  42. }
  43. return $messages;
  44. }
  45. /**
  46. * Saves messages to a PO file.
  47. * @param string $file file path
  48. * @param array $messages message translations (message id => translated message).
  49. * Note if the message has a context, the message id must be prefixed with
  50. * the context with chr(4) as the separator.
  51. */
  52. public function save($file,$messages)
  53. {
  54. $content='';
  55. foreach($messages as $id=>$message)
  56. {
  57. if(($pos=strpos($id,chr(4)))!==false)
  58. {
  59. $content.='msgctxt "'.substr($id,0,$pos)."\"\n";
  60. $id=substr($id,$pos+1);
  61. }
  62. $content.='msgid "'.$this->encode($id)."\"\n";
  63. $content.='msgstr "'.$this->encode($message)."\"\n\n";
  64. }
  65. file_put_contents($file,$content);
  66. }
  67. /**
  68. * Encodes special characters in a message.
  69. * @param string $string message to be encoded
  70. * @return string the encoded message
  71. */
  72. protected function encode($string)
  73. {
  74. return str_replace(array('"', "\n", "\t", "\r"),array('\\"', "\\n", '\\t', '\\r'),$string);
  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. return str_replace(array('\\"', "\\n", '\\t', '\\r'),array('"', "\n", "\t", "\r"),$string);
  84. }
  85. }