Translations

The WordPress Core Translations class.

Defined (1)

The class is defined in the following location(s).

/wp-includes/pomo/translations.php  
  1. class Translations { 
  2. var $entries = array(); 
  3. var $headers = array(); 
  4.  
  5. /** 
  6. * Add entry to the PO structure 
  7. * @param array|Translation_Entry &$entry 
  8. * @return bool true on success, false if the entry doesn't have a key 
  9. */ 
  10. function add_entry($entry) { 
  11. if (is_array($entry)) { 
  12. $entry = new Translation_Entry($entry); 
  13. $key = $entry->key(); 
  14. if (false === $key) return false; 
  15. $this->entries[$key] = &$entry; 
  16. return true; 
  17.  
  18. /** 
  19. * @param array|Translation_Entry $entry 
  20. * @return bool 
  21. */ 
  22. function add_entry_or_merge($entry) { 
  23. if (is_array($entry)) { 
  24. $entry = new Translation_Entry($entry); 
  25. $key = $entry->key(); 
  26. if (false === $key) return false; 
  27. if (isset($this->entries[$key])) 
  28. $this->entries[$key]->merge_with($entry); 
  29. else 
  30. $this->entries[$key] = &$entry; 
  31. return true; 
  32.  
  33. /** 
  34. * Sets $header PO header to $value 
  35. * If the header already exists, it will be overwritten 
  36. * TODO: this should be out of this class, it is gettext specific 
  37. * @param string $header header name, without trailing : 
  38. * @param string $value header value, without trailing \n 
  39. */ 
  40. function set_header($header, $value) { 
  41. $this->headers[$header] = $value; 
  42.  
  43. /** 
  44. * @param array $headers 
  45. */ 
  46. function set_headers($headers) { 
  47. foreach($headers as $header => $value) { 
  48. $this->set_header($header, $value); 
  49.  
  50. /** 
  51. * @param string $header 
  52. */ 
  53. function get_header($header) { 
  54. return isset($this->headers[$header])? $this->headers[$header] : false; 
  55.  
  56. /** 
  57. * @param Translation_Entry $entry 
  58. */ 
  59. function translate_entry(&$entry) { 
  60. $key = $entry->key(); 
  61. return isset($this->entries[$key])? $this->entries[$key] : false; 
  62.  
  63. /** 
  64. * @param string $singular 
  65. * @param string $context 
  66. * @return string 
  67. */ 
  68. function translate($singular, $context=null) { 
  69. $entry = new Translation_Entry(array('singular' => $singular, 'context' => $context)); 
  70. $translated = $this->translate_entry($entry); 
  71. return ($translated && !empty($translated->translations))? $translated->translations[0] : $singular; 
  72.  
  73. /** 
  74. * Given the number of items, returns the 0-based index of the plural form to use 
  75. * Here, in the base Translations class, the common logic for English is implemented: 
  76. * 0 if there is one element, 1 otherwise 
  77. * This function should be overridden by the sub-classes. For example MO/PO can derive the logic 
  78. * from their headers. 
  79. * @param integer $count number of items 
  80. */ 
  81. function select_plural_form($count) { 
  82. return 1 == $count? 0 : 1; 
  83.  
  84. /** 
  85. * @return int 
  86. */ 
  87. function get_plural_forms_count() { 
  88. return 2; 
  89.  
  90. /** 
  91. * @param string $singular 
  92. * @param string $plural 
  93. * @param int $count 
  94. * @param string $context 
  95. */ 
  96. function translate_plural($singular, $plural, $count, $context = null) { 
  97. $entry = new Translation_Entry(array('singular' => $singular, 'plural' => $plural, 'context' => $context)); 
  98. $translated = $this->translate_entry($entry); 
  99. $index = $this->select_plural_form($count); 
  100. $total_plural_forms = $this->get_plural_forms_count(); 
  101. if ($translated && 0 <= $index && $index < $total_plural_forms && 
  102. is_array($translated->translations) && 
  103. isset($translated->translations[$index])) 
  104. return $translated->translations[$index]; 
  105. else 
  106. return 1 == $count? $singular : $plural; 
  107.  
  108. /** 
  109. * Merge $other in the current object. 
  110. * @param Object &$other Another Translation object, whose translations will be merged in this one 
  111. * @return void 
  112. **/ 
  113. function merge_with(&$other) { 
  114. foreach( $other->entries as $entry ) { 
  115. $this->entries[$entry->key()] = $entry; 
  116.  
  117. /** 
  118. * @param object $other 
  119. */ 
  120. function merge_originals_with(&$other) { 
  121. foreach( $other->entries as $entry ) { 
  122. if ( !isset( $this->entries[$entry->key()] ) ) 
  123. $this->entries[$entry->key()] = $entry; 
  124. else 
  125. $this->entries[$entry->key()]->merge_with($entry);