e-th-a55d428c84e9a3179fe241ec600a0f03.json d01d7cf85d first commit 4 anos atrás woocommerce-th-a65b4ddf263e8c410e9bcbfd6e6d72e4.json d01d7cf85d first commit 4 anos atrás woocommerce-th-a8b73b087444470c8f4396b7142908f6.json d01d7cf85d first commit 4 anos atrás woocommerce-th-abb904b047b7be3be9d638e2c6144691.json d01d7cf85d first commit 4 anos atrás woocommerce-th-ae1c49c8c0597593a9c36e106a04bd60.json d01d7cf85d first commit 4 anos atrás woocommerce-th-aed4ce3ede1d44a8fecfe0edfbf8421c.json d01d7cf85d first commit 4 anos atrás woocommerce-th-af52ab8cb2e7713cf3037751dd593965.json d01d7cf85d first commit 4 anos atrás woocommerce-th-afbede9cda22f594a80ae8b178e0e98c.json d01d7cf85d first commit 4 anos atrás woocommerce-th-afd1e270efc2e8283acfd50ccab18d2b.json d01d7cf85d first commit 4 anos atrás woocommerce-th-b007baff76144318d6c33cd26f4bfdb9.json d01d7cf85d first commit 4 anos atrás woocommerce-th-b0a592b6fe83588c5d1b8ed6850b6a74.json d01d7cf85d first commit 4 anos atrás woocommerce-th-b155b7bf28890099b74a84417cbaafe9.json d01d7cf85d first commit 4 anos atrás woocommerce-th-b1bb8fbd24d68a9c893faf1a6f7b493b.json d01d7cf85d first commit 4 anos atrás woocommerce-th-b1bcbab1d6a7ca6b0433d67776f347f1.json d01d7cf85d first commit 4 anos atrás woocommerce-th-b239ff15197a133498a4b025f002dee5.json d01d7cf85d first commit 4 anos atrás woocommerce-th-b2e956ee35340f6e47f27f8a54309ac3.json d01d7cf85d first commit 4 anos atrás woocommerce-th-b31f51fa0bb40ceb060aaa9f77ca1771.json d01d7cf85d first commit 4 anos atrás woocommerce-th-b3f83696a2cb49f571043ce14a384a76.json d01d7cf85d first commit 4 anos atrás woocommerce-th-b4bf1e55ca6145f38f1b2f104201da21.json d01d7cf85d first commit 4 anos atrás woocommerce-th-b826befd7acca93e8e4c12b7cd2fc7f1.json d01d7cf85d first commit 4 anos atrás woocommerce-th-b9bf1ca91d393a30e2461ef709f8fa10.json d01d7cf85d first commit 4 anos atrás woocommerce-th-ba8d2fb7c85928310d4f3a907b7d9bdd.json d01d7cf85d first commit 4 anos atrás woocommerce-th-bb0f027faa1257de4b730158475093de.json d01d7cf85d first commit 4 anos atrás woocommerce-th-bb5c5935ec0d64e8880b1c7794470460.json d01d7cf85d first commit 4 anos atrás woocommerce-th-bc1f4f7aec77a0a6dd229cc910f601f1.json d01d7cf85d first commit 4 anos atrás woocommerce-th-bc9c3f9eda462d6d9968c1dca51e7c62.json d01d7cf85d first commit 4 anos atrás woocommerce-th-bcb06ab6e8931a862dec9c07c66df45b.json d01d7cf85d first commit 4 anos atrás woocommerce-th-c06829f63f73ae430c10ae700fb1eb6d.json d01d7cf85d first commit 4 anos atrás woocommerce-th-c0faba84c7842505f8893c2c327a9f78.json d01d7cf85d first commit 4 anos atrás woocommerce-th-c13acd243559529a685813d9871026a1.json d01d7cf85d first commit 4 anos atrás woocommerce-th-c1bc39bcdee2da3c0b62bcd5372120f4.json d01d7cf85d first commit 4 anos atrás woocommerce-th-c22945301f466e2d78d6e80404a22664.json d01d7cf85d first commit 4 anos atrás woocommerce-th-c2435bc8dfd186b147aba658416d115e.json d01d7cf85d first commit 4 anos atrás woocommerce-th-c257c024e3db4e010644d2c7be3c7090.json d01d7cf85d first commit 4 anos atrás woocommerce-th-c2d9a34cf502bd5b5a80ca646a1420b3.json d01d7cf85d first commit 4 anos atrás woocommerce-th-c5702ace609dd30ccabd4c999de43096.json d01d7cf85d first commit 4 anos atrás woocommerce-th-c608fe5deda8db7667c8431ca6fd222a.json d01d7cf85d first commit 4 anos atrás woocommerce-th-c6c5331d3afe1391f2ca83e991ccc086.json d01d7cf85d first commit 4 anos atrás woocommerce-th-c70671124b0e00661f88fe1616dfc9f0.json d01d7cf85d first commit 4 anos atrás woocommerce-th-c8cbe8ef95155db152b270d441fc8528.json d01d7cf85d first commit 4 anos atrás woocommerce-th-c8ddf2164475a213274d06873bfbb73a.json d01d7cf85d first commit 4 anos atrás woocommerce-th-c93706e596cbd46fb0d621f4fe3efb80.json d01d7cf85d first commit 4 anos atrás woocommerce-th-ca3cd4394cb082bde7555ebd3bb5f143.json d01d7cf85d first commit 4 anos atrás woocommerce-th-cb71ce5ddcc7c808172e80a0dc9833d5.json d01d7cf85d first commit 4 anos atrás woocommerce-th-cc16013df9d4b0a0ba45297b90b06f20.json d01d7cf85d first commit 4 anos atrás woocommerce-th-ccc500b0b722a0c96712f1634cbb7289.json d01d7cf85d first commit 4 anos atrás woocommerce-th-cd9fc1d9fc46b55bb32d0fd471457690.json d01d7cf85d first commit 4 anos atrás woocommerce-th-ce3f8f2f3e0f3c58f8aea5b7aa5cc6f4.json d01d7cf85d first commit 4 anos atrás woocommerce-th-d05ee634b22d05f0055b724443f05a68.json d01d7cf85d first commit 4 anos atrás woocommerce-th-d0d05e0dda872b433f260969b4d53b30.json d01d7cf85d first commit 4 anos atrás woocommerce-th-d0fa7cd1cd624e53cc7b4fde655c20af.json d01d7cf85d first commit 4 anos atrás woocommerce-th-d2b4400b6de0e69404e0f7f61d16bc55.json d01d7cf85d first commit 4 anos atrás woocommerce-th-d3e57c518feb58ab8ea4f9d2d8e66160.json d01d7cf85d first commit 4 anos atrás woocommerce-th-d4c505e96199e8c818fc6d24151d9c4a.json d01d7cf85d first commit 4 anos atrás woocommerce-th-d5ea33028f13b50f40bbcce807e37fc0.json d01d7cf85d first commit 4 anos atrás woocommerce-th-d6f4faa954a31ef3d5b74c2dfe13f141.json d01d7cf85d first commit 4 anos atrás woocommerce-th-d7bbe90eb59636eb8a6f31c7979ab6f5.json d01d7cf85d first commit 4 anos atrás woocommerce-th-d80020dab74131d7f0c108526c0fbebe.json d01d7cf85d first commit 4 anos atrás woocommerce-th-da92a9b2d4c08762c60d7c4c569eb5db.json d01d7cf85d first commit 4 anos atrás woocommerce-th-de12a4697822740a2fbaeb47c5062af1.json d01d7cf85d first commit 4 anos atrás woocommerce-th-deed2aed3392b43c83c7cea296b154af.json d01d7cf85d first commit 4 anos atrás woocommerce-th-df51042cba56a4e9fcac0e5db643906a.json d01d7cf85d first commit 4 anos atrás woocommerce-th-dfb09f9953676bbc1de4eb2b9f232c6d.json d01d7cf85d first commit 4 anos atrás woocommerce-th-dfbfe3d556f60ebbd62c2b5c8e0fa466.json d01d7cf85d first commit 4 anos atrás woocommerce-th-e03ec35386c2705c11cebd6288b33cf8.json d01d7cf85d first commit 4 anos atrás woocommerce-th-e0bde0ac58f55fc6b1426844e6697db3.json d01d7cf85d first commit 4 anos atrás woocommerce-th-e0f9c7d89b1b130a9023c5c2fde2815e.json d01d7cf85d first commit 4 anos atrás woocommerce-th-e41c58d5fb39f750efcbf65a2b55460b.json d01d7cf85d first commit 4 anos atrás woocommerce-th-e44671e4528b0da3a0fd6feaf8abadca.json d01d7cf85d first commit 4 anos atrás woocommerce-th-e4fbc8d7fc7a16bd8cf2e0522a199de0.json d01d7cf85d first commit 4 anos atrás woocommerce-th-e690486b33a7bb1944a37f6e734d734f.json d01d7cf85d first commit 4 anos atrás woocommerce-th-e80539b9960e930bf0dcd334d99eb128.json d01d7cf85d first commit 4 anos atrás woocommerce-th-e885442559cb44e8b827f0e0ca0a14c9.json d01d7cf85d first commit 4 anos atrás woocommerce-th-e88dc35c02f6fa832e90fac5a122323c.json d01d7cf85d first commit 4 anos atrás woocommerce-th-ea6fb371f81c4d3584bbc795fa4ac09c.json d01d7cf85d first commit 4 anos atrás woocommerce-th-ed9b16000a6d37c51bf23c61f4df878e.json d01d7cf85d first commit 4 anos atrás woocommerce-th-ef5798395d7601ab6fea0f1efd60f40a.json d01d7cf85d first commit 4 anos atrás woocommerce-th-f31b0aa0c71dd74b7103120610ca6253.json d01d7cf85d first commit 4 anos atrás woocommerce-th-f34924aeff974b0bdbfac124c23fbfe3.json d01d7cf85d first commit 4 anos atrás woocommerce-th-f39008456fd28b2f79cdb0964ff294e1.json d01d7cf85d first commit 4 anos atrás woocommerce-th-f3f1cc4f5811d4bf8a04a58feca06858.json d01d7cf85d first commit 4 anos atrás woocommerce-th-f56e66fd08c07abc78cd844f7e83af8c.json d01d7cf85d first commit 4 anos atrás woocommerce-th-f60049653c419cfb1e3dc8876e7a8bdb.json d01d7cf85d first commit 4 anos atrás woocommerce-th-f686a272add740773cdcb730afdb5454.json d01d7cf85d first commit 4 anos atrás woocommerce-th-f976da155dfdd080588c50ccda5af747.json d01d7cf85d first commit 4 anos atrás woocommerce-th-f9c56a0cbb6710505047e6c835953c82.json d01d7cf85d first commit 4 anos atrás woocommerce-th-fb2ef023bdd44902b5a309e25e0b7792.json d01d7cf85d first commit 4 anos atrás woocommerce-th-fc1fe56fadfdeeb25d1bc441581d92b6.json d01d7cf85d first commit 4 anos atrás woocommerce-th-fc4895b6508f32c16c4d8bc69f8162b6.json d01d7cf85d first commit 4 anos atrás woocommerce-th-fd20fc8ea1484781160e8ea0d229d5e9.json d01d7cf85d first commit 4 anos atrás woocommerce-th-fffdec5db595ec0ca7d043c87bae79b8.json d01d7cf85d first commit 4 anos atrás woocommerce-th-wc-admin-app.json d01d7cf85d first commit 4 anos atrás woocommerce-th.mo d01d7cf85d first commit 4 anos atrás woocommerce-th.po d01d7cf85d first commit 4 anos atrás tum/whitesports - Gogs: Simplico Git Service

Нема описа

translations.php 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. 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 int $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. public $entries = array();
  284. public $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;