Gettext_Translations

The WordPress Core Gettext Translations class.

Defined (1)

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

/wp-includes/pomo/translations.php  
  1. class Gettext_Translations extends Translations { 
  2. /** 
  3. * The gettext implementation of select_plural_form. 
  4. * It lives in this class, because there are more than one descendand, which will use it and 
  5. * they can't share it effectively. 
  6. * @param int $count 
  7. */ 
  8. function gettext_select_plural_form($count) { 
  9. if (!isset($this->_gettext_select_plural_form) || is_null($this->_gettext_select_plural_form)) { 
  10. list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header($this->get_header('Plural-Forms')); 
  11. $this->_nplurals = $nplurals; 
  12. $this->_gettext_select_plural_form = $this->make_plural_form_function($nplurals, $expression); 
  13. return call_user_func($this->_gettext_select_plural_form, $count); 
  14.  
  15. /** 
  16. * @param string $header 
  17. * @return array 
  18. */ 
  19. function nplurals_and_expression_from_header($header) { 
  20. if (preg_match('/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $header, $matches)) { 
  21. $nplurals = (int)$matches[1]; 
  22. $expression = trim($this->parenthesize_plural_exression($matches[2])); 
  23. return array($nplurals, $expression); 
  24. } else { 
  25. return array(2, 'n != 1'); 
  26.  
  27. /** 
  28. * Makes a function, which will return the right translation index, according to the 
  29. * plural forms header 
  30. * @param int $nplurals 
  31. * @param string $expression 
  32. */ 
  33. function make_plural_form_function($nplurals, $expression) { 
  34. $expression = str_replace('n', '$n', $expression); 
  35. $func_body = " 
  36. \$index = (int)($expression); 
  37. return (\$index < $nplurals)? \$index : $nplurals - 1;"; 
  38. return create_function('$n', $func_body); 
  39.  
  40. /** 
  41. * Adds parentheses to the inner parts of ternary operators in 
  42. * plural expressions, because PHP evaluates ternary oerators from left to right 
  43. * @param string $expression the expression without parentheses 
  44. * @return string the expression with parentheses added 
  45. */ 
  46. function parenthesize_plural_exression($expression) { 
  47. $expression .= ';'; 
  48. $res = ''; 
  49. $depth = 0; 
  50. for ($i = 0; $i < strlen($expression); ++$i) { 
  51. $char = $expression[$i]; 
  52. switch ($char) { 
  53. case '?': 
  54. $res .= ' ? ('; 
  55. $depth++; 
  56. break; 
  57. case ':': 
  58. $res .= ') : ('; 
  59. break; 
  60. case ';': 
  61. $res .= str_repeat(')', $depth) . ';'; 
  62. $depth= 0; 
  63. break; 
  64. default: 
  65. $res .= $char; 
  66. return rtrim($res, ';'); 
  67.  
  68. /** 
  69. * @param string $translation 
  70. * @return array 
  71. */ 
  72. function make_headers($translation) { 
  73. $headers = array(); 
  74. // sometimes \ns are used instead of real new lines 
  75. $translation = str_replace('\n', "\n", $translation); 
  76. $lines = explode("\n", $translation); 
  77. foreach($lines as $line) { 
  78. $parts = explode(':', $line, 2); 
  79. if (!isset($parts[1])) continue; 
  80. $headers[trim($parts[0])] = trim($parts[1]); 
  81. return $headers; 
  82.  
  83. /** 
  84. * @param string $header 
  85. * @param string $value 
  86. */ 
  87. function set_header($header, $value) { 
  88. parent::set_header($header, $value); 
  89. if ('Plural-Forms' == $header) { 
  90. list( $nplurals, $expression ) = $this->nplurals_and_expression_from_header($this->get_header('Plural-Forms')); 
  91. $this->_nplurals = $nplurals; 
  92. $this->_gettext_select_plural_form = $this->make_plural_form_function($nplurals, $expression);