translations.php 9.8 KB

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