PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-includes/pomo/translations.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 379 lines | 204 code | 31 blank | 144 comment | 28 complexity | 057c94389f5661f1b3b0ac79a2ecb2fd MD5 | raw file
  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. var $entries = array();
  14. var $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. 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. 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. function set_header( $header, $value ) {
  62. $this->headers[ $header ] = $value;
  63. }
  64. /**
  65. * @param array $headers
  66. */
  67. 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. function get_header( $header ) {
  76. return isset( $this->headers[ $header ] ) ? $this->headers[ $header ] : false;
  77. }
  78. /**
  79. * @param Translation_Entry $entry
  80. */
  81. 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. 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 integer $count number of items
  110. */
  111. function select_plural_form( $count ) {
  112. return 1 == $count ? 0 : 1;
  113. }
  114. /**
  115. * @return int
  116. */
  117. 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. 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. * @return void
  150. */
  151. 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. 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. * The gettext implementation of select_plural_form.
  172. *
  173. * It lives in this class, because there are more than one descendand, which will use it and
  174. * they can't share it effectively.
  175. *
  176. * @param int $count
  177. */
  178. function gettext_select_plural_form( $count ) {
  179. if ( ! isset( $this->_gettext_select_plural_form ) || is_null( $this->_gettext_select_plural_form ) ) {
  180. list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header( $this->get_header( 'Plural-Forms' ) );
  181. $this->_nplurals = $nplurals;
  182. $this->_gettext_select_plural_form = $this->make_plural_form_function( $nplurals, $expression );
  183. }
  184. return call_user_func( $this->_gettext_select_plural_form, $count );
  185. }
  186. /**
  187. * @param string $header
  188. * @return array
  189. */
  190. function nplurals_and_expression_from_header( $header ) {
  191. if ( preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $header, $matches ) ) {
  192. $nplurals = (int) $matches[1];
  193. $expression = trim( $matches[2] );
  194. return array( $nplurals, $expression );
  195. } else {
  196. return array( 2, 'n != 1' );
  197. }
  198. }
  199. /**
  200. * Makes a function, which will return the right translation index, according to the
  201. * plural forms header
  202. *
  203. * @param int $nplurals
  204. * @param string $expression
  205. */
  206. function make_plural_form_function( $nplurals, $expression ) {
  207. try {
  208. $handler = new Plural_Forms( rtrim( $expression, ';' ) );
  209. return array( $handler, 'get' );
  210. } catch ( Exception $e ) {
  211. // Fall back to default plural-form function.
  212. return $this->make_plural_form_function( 2, 'n != 1' );
  213. }
  214. }
  215. /**
  216. * Adds parentheses to the inner parts of ternary operators in
  217. * plural expressions, because PHP evaluates ternary oerators from left to right
  218. *
  219. * @param string $expression the expression without parentheses
  220. * @return string the expression with parentheses added
  221. */
  222. function parenthesize_plural_exression( $expression ) {
  223. $expression .= ';';
  224. $res = '';
  225. $depth = 0;
  226. for ( $i = 0; $i < strlen( $expression ); ++$i ) {
  227. $char = $expression[ $i ];
  228. switch ( $char ) {
  229. case '?':
  230. $res .= ' ? (';
  231. $depth++;
  232. break;
  233. case ':':
  234. $res .= ') : (';
  235. break;
  236. case ';':
  237. $res .= str_repeat( ')', $depth ) . ';';
  238. $depth = 0;
  239. break;
  240. default:
  241. $res .= $char;
  242. }
  243. }
  244. return rtrim( $res, ';' );
  245. }
  246. /**
  247. * @param string $translation
  248. * @return array
  249. */
  250. function make_headers( $translation ) {
  251. $headers = array();
  252. // Sometimes \n's are used instead of real new lines.
  253. $translation = str_replace( '\n', "\n", $translation );
  254. $lines = explode( "\n", $translation );
  255. foreach ( $lines as $line ) {
  256. $parts = explode( ':', $line, 2 );
  257. if ( ! isset( $parts[1] ) ) {
  258. continue;
  259. }
  260. $headers[ trim( $parts[0] ) ] = trim( $parts[1] );
  261. }
  262. return $headers;
  263. }
  264. /**
  265. * @param string $header
  266. * @param string $value
  267. */
  268. function set_header( $header, $value ) {
  269. parent::set_header( $header, $value );
  270. if ( 'Plural-Forms' == $header ) {
  271. list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header( $this->get_header( 'Plural-Forms' ) );
  272. $this->_nplurals = $nplurals;
  273. $this->_gettext_select_plural_form = $this->make_plural_form_function( $nplurals, $expression );
  274. }
  275. }
  276. }
  277. endif;
  278. if ( ! class_exists( 'NOOP_Translations', false ) ) :
  279. /**
  280. * Provides the same interface as Translations, but doesn't do anything
  281. */
  282. class NOOP_Translations {
  283. var $entries = array();
  284. var $headers = array();
  285. function add_entry( $entry ) {
  286. return true;
  287. }
  288. /**
  289. * @param string $header
  290. * @param string $value
  291. */
  292. function set_header( $header, $value ) {
  293. }
  294. /**
  295. * @param array $headers
  296. */
  297. function set_headers( $headers ) {
  298. }
  299. /**
  300. * @param string $header
  301. * @return false
  302. */
  303. function get_header( $header ) {
  304. return false;
  305. }
  306. /**
  307. * @param Translation_Entry $entry
  308. * @return false
  309. */
  310. function translate_entry( &$entry ) {
  311. return false;
  312. }
  313. /**
  314. * @param string $singular
  315. * @param string $context
  316. */
  317. function translate( $singular, $context = null ) {
  318. return $singular;
  319. }
  320. /**
  321. * @param int $count
  322. * @return bool
  323. */
  324. function select_plural_form( $count ) {
  325. return 1 == $count ? 0 : 1;
  326. }
  327. /**
  328. * @return int
  329. */
  330. function get_plural_forms_count() {
  331. return 2;
  332. }
  333. /**
  334. * @param string $singular
  335. * @param string $plural
  336. * @param int $count
  337. * @param string $context
  338. */
  339. function translate_plural( $singular, $plural, $count, $context = null ) {
  340. return 1 == $count ? $singular : $plural;
  341. }
  342. /**
  343. * @param object $other
  344. */
  345. function merge_with( &$other ) {
  346. }
  347. }
  348. endif;