Brak opisu

translations.php 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. /**
  3. * Class for a set of entries for translation and their associated headers
  4. *
  5. * @version $Id: translations.php 1157 2015-11-20 04:30:11Z dd32 $
  6. * @package pomo
  7. * @subpackage translations
  8. */
  9. require_once __DIR__ . '/plural-forms.php';
  10. require_once __DIR__ . '/entry.php';
  11. if ( ! class_exists( 'Translations', false ) ) :
  12. class Translations {
  13. public $entries = array();
  14. public $headers = array();
  15. /**
  16. * Add entry to the PO structure
  17. *
  18. * @param array|Translation_Entry $entry
  19. * @return bool true on success, false if the entry doesn't have a key
  20. */
  21. public function add_entry( $entry ) {
  22. if ( is_array( $entry ) ) {
  23. $entry = new Translation_Entry( $entry );
  24. }
  25. $key = $entry->key();
  26. if ( false === $key ) {
  27. return false;
  28. }
  29. $this->entries[ $key ] = &$entry;
  30. return true;
  31. }
  32. /**
  33. * @param array|Translation_Entry $entry
  34. * @return bool
  35. */
  36. public function add_entry_or_merge( $entry ) {
  37. if ( is_array( $entry ) ) {
  38. $entry = new Translation_Entry( $entry );
  39. }
  40. $key = $entry->key();
  41. if ( false === $key ) {
  42. return false;
  43. }
  44. if ( isset( $this->entries[ $key ] ) ) {
  45. $this->entries[ $key ]->merge_with( $entry );
  46. } else {
  47. $this->entries[ $key ] = &$entry;
  48. }
  49. return true;
  50. }
  51. /**
  52. * Sets $header PO header to $value
  53. *
  54. * If the header already exists, it will be overwritten
  55. *
  56. * TODO: this should be out of this class, it is gettext specific
  57. *
  58. * @param string $header header name, without trailing :
  59. * @param string $value header value, without trailing \n
  60. */
  61. public function set_header( $header, $value ) {
  62. $this->headers[ $header ] = $value;
  63. }
  64. /**
  65. * @param array $headers
  66. */
  67. public function set_headers( $headers ) {
  68. foreach ( $headers as $header => $value ) {
  69. $this->set_header( $header, $value );
  70. }
  71. }
  72. /**
  73. * @param string $header
  74. */
  75. public function get_header( $header ) {
  76. return isset( $this->headers[ $header ] ) ? $this->headers[ $header ] : false;
  77. }
  78. /**
  79. * @param Translation_Entry $entry
  80. */
  81. public function translate_entry( &$entry ) {
  82. $key = $entry->key();
  83. return isset( $this->entries[ $key ] ) ? $this->entries[ $key ] : false;
  84. }
  85. /**
  86. * @param string $singular
  87. * @param string $context
  88. * @return string
  89. */
  90. public function translate( $singular, $context = null ) {
  91. $entry = new Translation_Entry(
  92. array(
  93. 'singular' => $singular,
  94. 'context' => $context,
  95. )
  96. );
  97. $translated = $this->translate_entry( $entry );
  98. return ( $translated && ! empty( $translated->translations ) ) ? $translated->translations[0] : $singular;
  99. }
  100. /**
  101. * Given the number of items, returns the 0-based index of the plural form to use
  102. *
  103. * Here, in the base Translations class, the common logic for English is implemented:
  104. * 0 if there is one element, 1 otherwise
  105. *
  106. * This function should be overridden by the subclasses. For example MO/PO can derive the logic
  107. * from their headers.
  108. *
  109. * @param int $count number of items
  110. */
  111. public function select_plural_form( $count ) {
  112. return 1 == $count ? 0 : 1;
  113. }
  114. /**
  115. * @return int
  116. */
  117. public function get_plural_forms_count() {
  118. return 2;
  119. }
  120. /**
  121. * @param string $singular
  122. * @param string $plural
  123. * @param int $count
  124. * @param string $context
  125. */
  126. public function translate_plural( $singular, $plural, $count, $context = null ) {
  127. $entry = new Translation_Entry(
  128. array(
  129. 'singular' => $singular,
  130. 'plural' => $plural,
  131. 'context' => $context,
  132. )
  133. );
  134. $translated = $this->translate_entry( $entry );
  135. $index = $this->select_plural_form( $count );
  136. $total_plural_forms = $this->get_plural_forms_count();
  137. if ( $translated && 0 <= $index && $index < $total_plural_forms &&
  138. is_array( $translated->translations ) &&
  139. isset( $translated->translations[ $index ] ) ) {
  140. return $translated->translations[ $index ];
  141. } else {
  142. return 1 == $count ? $singular : $plural;
  143. }
  144. }
  145. /**
  146. * Merge $other in the current object.
  147. *
  148. * @param Object $other Another Translation object, whose translations will be merged in this one (passed by reference).
  149. */
  150. public function merge_with( &$other ) {
  151. foreach ( $other->entries as $entry ) {
  152. $this->entries[ $entry->key() ] = $entry;
  153. }
  154. }
  155. /**
  156. * @param object $other
  157. */
  158. public function merge_originals_with( &$other ) {
  159. foreach ( $other->entries as $entry ) {
  160. if ( ! isset( $this->entries[ $entry->key() ] ) ) {
  161. $this->entries[ $entry->key() ] = $entry;
  162. } else {
  163. $this->entries[ $entry->key() ]->merge_with( $entry );
  164. }
  165. }
  166. }
  167. }
  168. class Gettext_Translations extends Translations {
  169. /**
  170. * The gettext implementation of select_plural_form.
  171. *
  172. * It lives in this class, because there are more than one descendand, which will use it and
  173. * they can't share it effectively.
  174. *
  175. * @param int $count
  176. */
  177. public function gettext_select_plural_form( $count ) {
  178. if ( ! isset( $this->_gettext_select_plural_form ) || is_null( $this->_gettext_select_plural_form ) ) {
  179. list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header( $this->get_header( 'Plural-Forms' ) );
  180. $this->_nplurals = $nplurals;
  181. $this->_gettext_select_plural_form = $this->make_plural_form_function( $nplurals, $expression );
  182. }
  183. return call_user_func( $this->_gettext_select_plural_form, $count );
  184. }
  185. /**
  186. * @param string $header
  187. * @return array
  188. */
  189. public function nplurals_and_expression_from_header( $header ) {
  190. if ( preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $header, $matches ) ) {
  191. $nplurals = (int) $matches[1];
  192. $expression = trim( $matches[2] );
  193. return array( $nplurals, $expression );
  194. } else {
  195. return array( 2, 'n != 1' );
  196. }
  197. }
  198. /**
  199. * Makes a function, which will return the right translation index, according to the
  200. * plural forms header
  201. *
  202. * @param int $nplurals
  203. * @param string $expression
  204. */
  205. public function make_plural_form_function( $nplurals, $expression ) {
  206. try {
  207. $handler = new Plural_Forms( rtrim( $expression, ';' ) );
  208. return array( $handler, 'get' );
  209. } catch ( Exception $e ) {
  210. // Fall back to default plural-form function.
  211. return $this->make_plural_form_function( 2, 'n != 1' );
  212. }
  213. }
  214. /**
  215. * Adds parentheses to the inner parts of ternary operators in
  216. * plural expressions, because PHP evaluates ternary oerators from left to right
  217. *
  218. * @param string $expression the expression without parentheses
  219. * @return string the expression with parentheses added
  220. */
  221. public function parenthesize_plural_exression( $expression ) {
  222. $expression .= ';';
  223. $res = '';
  224. $depth = 0;
  225. for ( $i = 0; $i < strlen( $expression ); ++$i ) {
  226. $char = $expression[ $i ];
  227. switch ( $char ) {
  228. case '?':
  229. $res .= ' ? (';
  230. $depth++;
  231. break;
  232. case ':':
  233. $res .= ') : (';
  234. break;
  235. case ';':
  236. $res .= str_repeat( ')', $depth ) . ';';
  237. $depth = 0;
  238. break;
  239. default:
  240. $res .= $char;
  241. }
  242. }
  243. return rtrim( $res, ';' );
  244. }
  245. /**
  246. * @param string $translation
  247. * @return array
  248. */
  249. public function make_headers( $translation ) {
  250. $headers = array();
  251. // Sometimes \n's are used instead of real new lines.
  252. $translation = str_replace( '\n', "\n", $translation );
  253. $lines = explode( "\n", $translation );
  254. foreach ( $lines as $line ) {
  255. $parts = explode( ':', $line, 2 );
  256. if ( ! isset( $parts[1] ) ) {
  257. continue;
  258. }
  259. $headers[ trim( $parts[0] ) ] = trim( $parts[1] );
  260. }
  261. return $headers;
  262. }
  263. /**
  264. * @param string $header
  265. * @param string $value
  266. */
  267. public function set_header( $header, $value ) {
  268. parent::set_header( $header, $value );
  269. if ( 'Plural-Forms' === $header ) {
  270. list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header( $this->get_header( 'Plural-Forms' ) );
  271. $this->_nplurals = $nplurals;
  272. $this->_gettext_select_plural_form = $this->make_plural_form_function( $nplurals, $expression );
  273. }
  274. }
  275. }
  276. endif;
  277. if ( ! class_exists( 'NOOP_Translations', false ) ) :
  278. /**
  279. * Provides the same interface as Translations, but doesn't do anything
  280. */
  281. class NOOP_Translations {
  282. public $entries = array();
  283. public $headers = array();
  284. public function add_entry( $entry ) {
  285. return true;
  286. }
  287. /**
  288. * @param string $header
  289. * @param string $value
  290. */
  291. public function set_header( $header, $value ) {
  292. }
  293. /**
  294. * @param array $headers
  295. */
  296. public function set_headers( $headers ) {
  297. }
  298. /**
  299. * @param string $header
  300. * @return false
  301. */
  302. public function get_header( $header ) {
  303. return false;
  304. }
  305. /**
  306. * @param Translation_Entry $entry
  307. * @return false
  308. */
  309. public function translate_entry( &$entry ) {
  310. return false;
  311. }
  312. /**
  313. * @param string $singular
  314. * @param string $context
  315. */
  316. public function translate( $singular, $context = null ) {
  317. return $singular;
  318. }
  319. /**
  320. * @param int $count
  321. * @return bool
  322. */
  323. public function select_plural_form( $count ) {
  324. return 1 == $count ? 0 : 1;
  325. }
  326. /**
  327. * @return int
  328. */
  329. public function get_plural_forms_count() {
  330. return 2;
  331. }
  332. /**
  333. * @param string $singular
  334. * @param string $plural
  335. * @param int $count
  336. * @param string $context
  337. */
  338. public function translate_plural( $singular, $plural, $count, $context = null ) {
  339. return 1 == $count ? $singular : $plural;
  340. }
  341. /**
  342. * @param object $other
  343. */
  344. public function merge_with( &$other ) {
  345. }
  346. }
  347. endif;