PoItem.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. /**
  3. * @file
  4. * Definition of Drupal\Component\Gettext\PoItem.
  5. */
  6. /**
  7. * PoItem handles one translation.
  8. */
  9. class PoItem {
  10. /**
  11. * The language code this translation is in.
  12. *
  13. * @car string
  14. */
  15. private $_langcode;
  16. /**
  17. * The context this translation belongs to.
  18. *
  19. * @var string
  20. */
  21. private $_context = '';
  22. /**
  23. * The source string or array of strings if it has plurals.
  24. *
  25. * @var string or array
  26. * @see $_plural
  27. */
  28. private $_source;
  29. /**
  30. * Flag indicating if this translation has plurals.
  31. *
  32. * @var boolean
  33. */
  34. private $_plural;
  35. /**
  36. * The comment of this translation.
  37. *
  38. * @var string
  39. */
  40. private $_comment;
  41. /**
  42. * The translation string or array of strings if it has plurals.
  43. *
  44. * @var string or array
  45. * @see $_plural
  46. */
  47. private $_translation;
  48. /**
  49. * Get the language code of the currently used language.
  50. *
  51. * @return string with langcode
  52. */
  53. function getLangcode() {
  54. return $this->_langcode;
  55. }
  56. /**
  57. * Set the language code of the current language.
  58. *
  59. * @param string $langcode
  60. */
  61. function setLangcode($langcode) {
  62. $this->_langcode = $langcode;
  63. }
  64. /**
  65. * Get the context this translation belongs to.
  66. *
  67. * @return string $context
  68. */
  69. function getContext() {
  70. return $this->_context;
  71. }
  72. /**
  73. * Set the context this translation belongs to.
  74. *
  75. * @param string $context
  76. */
  77. function setContext($context) {
  78. $this->_context = $context;
  79. }
  80. /**
  81. * Get the source string or the array of strings if the translation has
  82. * plurals.
  83. *
  84. * @return string or array $translation
  85. */
  86. function getSource() {
  87. return $this->_source;
  88. }
  89. /**
  90. * Set the source string or the array of strings if the translation has
  91. * plurals.
  92. *
  93. * @param string or array $source
  94. */
  95. function setSource($source) {
  96. $this->_source = $source;
  97. }
  98. /**
  99. * Get the translation string or the array of strings if the translation has
  100. * plurals.
  101. *
  102. * @return string or array $translation
  103. */
  104. function getTranslation() {
  105. return $this->_translation;
  106. }
  107. /**
  108. * Set the translation string or the array of strings if the translation has
  109. * plurals.
  110. *
  111. * @param string or array $translation
  112. */
  113. function setTranslation($translation) {
  114. $this->_translation = $translation;
  115. }
  116. /**
  117. * Set if the translation has plural values.
  118. *
  119. * @param boolean $plural
  120. */
  121. function setPlural($plural) {
  122. $this->_plural = $plural;
  123. }
  124. /**
  125. * Get if the translation has plural values.
  126. *
  127. * @return boolean $plural
  128. */
  129. function isPlural() {
  130. return $this->_plural;
  131. }
  132. /**
  133. * Get the comment of this translation.
  134. *
  135. * @return String $comment
  136. */
  137. function getComment() {
  138. return $this->_comment;
  139. }
  140. /**
  141. * Set the comment of this translation.
  142. *
  143. * @param String $comment
  144. */
  145. function setComment($comment) {
  146. $this->_comment = $comment;
  147. }
  148. /**
  149. * Create the PoItem from a structured array.
  150. *
  151. * @param array values
  152. */
  153. public function setFromArray(array $values = array()) {
  154. if (isset($values['context'])) {
  155. $this->setContext($values['context']);
  156. }
  157. if (isset($values['source'])) {
  158. $this->setSource($values['source']);
  159. }
  160. if (isset($values['translation'])) {
  161. $this->setTranslation($values['translation']);
  162. }
  163. if (isset($values['comment'])){
  164. $this->setComment($values['comment']);
  165. }
  166. if (isset($this->_source) &&
  167. strpos($this->_source, L10N_UPDATE_PLURAL_DELIMITER) !== FALSE) {
  168. $this->setSource(explode(L10N_UPDATE_PLURAL_DELIMITER, $this->_source));
  169. $this->setTranslation(explode(L10N_UPDATE_PLURAL_DELIMITER, $this->_translation));
  170. $this->setPlural(count($this->_translation) > 1);
  171. }
  172. }
  173. /**
  174. * Output the PoItem as a string.
  175. */
  176. public function __toString() {
  177. return $this->formatItem();
  178. }
  179. /**
  180. * Format the POItem as a string.
  181. */
  182. private function formatItem() {
  183. $output = '';
  184. // Format string context.
  185. if (!empty($this->_context)) {
  186. $output .= 'msgctxt ' . $this->formatString($this->_context);
  187. }
  188. // Format translation.
  189. if ($this->_plural) {
  190. $output .= $this->formatPlural();
  191. }
  192. else {
  193. $output .= $this->formatSingular();
  194. }
  195. // Add one empty line to separate the translations.
  196. $output .= "\n";
  197. return $output;
  198. }
  199. /**
  200. * Formats a plural translation.
  201. */
  202. private function formatPlural() {
  203. $output = '';
  204. // Format source strings.
  205. $output .= 'msgid ' . $this->formatString($this->_source[0]);
  206. $output .= 'msgid_plural ' . $this->formatString($this->_source[1]);
  207. foreach ($this->_translation as $i => $trans) {
  208. if (isset($this->_translation[$i])) {
  209. $output .= 'msgstr[' . $i . '] ' . $this->formatString($trans);
  210. }
  211. else {
  212. $output .= 'msgstr[' . $i . '] ""' . "\n";
  213. }
  214. }
  215. return $output;
  216. }
  217. /**
  218. * Formats a singular translation.
  219. */
  220. private function formatSingular() {
  221. $output = '';
  222. $output .= 'msgid ' . $this->formatString($this->_source);
  223. $output .= 'msgstr ' . (isset($this->_translation) ? $this->formatString($this->_translation) : '""');
  224. return $output;
  225. }
  226. /**
  227. * Formats a string for output on multiple lines.
  228. */
  229. private function formatString($string) {
  230. // Escape characters for processing.
  231. $string = addcslashes($string, "\0..\37\\\"");
  232. // Always include a line break after the explicit \n line breaks from
  233. // the source string. Otherwise wrap at 70 chars to accommodate the extra
  234. // format overhead too.
  235. $parts = explode("\n", wordwrap(str_replace('\n', "\\n\n", $string), 70, " \n"));
  236. // Multiline string should be exported starting with a "" and newline to
  237. // have all lines aligned on the same column.
  238. if (count($parts) > 1) {
  239. return "\"\"\n\"" . implode("\"\n\"", $parts) . "\"\n";
  240. }
  241. // Single line strings are output on the same line.
  242. else {
  243. return "\"$parts[0]\"\n";
  244. }
  245. }
  246. }