wp/wp-includes/pomo/translations.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
--- a/wp/wp-includes/pomo/translations.php	Tue Jun 09 11:14:17 2015 +0000
+++ b/wp/wp-includes/pomo/translations.php	Mon Oct 14 17:39:30 2019 +0200
@@ -2,14 +2,15 @@
 /**
  * Class for a set of entries for translation and their associated headers
  *
- * @version $Id: translations.php 718 2012-10-31 00:32:02Z nbachiyski $
+ * @version $Id: translations.php 1157 2015-11-20 04:30:11Z dd32 $
  * @package pomo
  * @subpackage translations
  */
 
+require_once dirname(__FILE__) . '/plural-forms.php';
 require_once dirname(__FILE__) . '/entry.php';
 
-if ( !class_exists( 'Translations' ) ):
+if ( ! class_exists( 'Translations', false ) ):
 class Translations {
 	var $entries = array();
 	var $headers = array();
@@ -17,7 +18,7 @@
 	/**
 	 * Add entry to the PO structure
 	 *
-	 * @param array|Translation_Entry &$entry
+	 * @param array|Translation_Entry $entry
 	 * @return bool true on success, false if the entry doesn't have a key
 	 */
 	function add_entry($entry) {
@@ -102,7 +103,7 @@
 	 * Here, in the base Translations class, the common logic for English is implemented:
 	 * 	0 if there is one element, 1 otherwise
 	 *
-	 * This function should be overrided by the sub-classes. For example MO/PO can derive the logic
+	 * This function should be overridden by the sub-classes. For example MO/PO can derive the logic
 	 * from their headers.
 	 *
 	 * @param integer $count number of items
@@ -111,6 +112,9 @@
 		return 1 == $count? 0 : 1;
 	}
 
+	/**
+	 * @return int
+	 */
 	function get_plural_forms_count() {
 		return 2;
 	}
@@ -137,7 +141,7 @@
 	/**
 	 * Merge $other in the current object.
 	 *
-	 * @param Object &$other Another Translation object, whose translations will be merged in this one
+	 * @param Object $other Another Translation object, whose translations will be merged in this one (passed by reference).
 	 * @return void
 	 **/
 	function merge_with(&$other) {
@@ -146,6 +150,9 @@
 		}
 	}
 
+	/**
+	 * @param object $other
+	 */
 	function merge_originals_with(&$other) {
 		foreach( $other->entries as $entry ) {
 			if ( !isset( $this->entries[$entry->key()] ) )
@@ -181,7 +188,7 @@
 	function nplurals_and_expression_from_header($header) {
 		if (preg_match('/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $header, $matches)) {
 			$nplurals = (int)$matches[1];
-			$expression = trim($this->parenthesize_plural_exression($matches[2]));
+			$expression = trim( $matches[2] );
 			return array($nplurals, $expression);
 		} else {
 			return array(2, 'n != 1');
@@ -195,11 +202,13 @@
 	 * @param string $expression
 	 */
 	function make_plural_form_function($nplurals, $expression) {
-		$expression = str_replace('n', '$n', $expression);
-		$func_body = "
-			\$index = (int)($expression);
-			return (\$index < $nplurals)? \$index : $nplurals - 1;";
-		return create_function('$n', $func_body);
+		try {
+			$handler = new Plural_Forms( rtrim( $expression, ';' ) );
+			return array( $handler, 'get' );
+		} catch ( Exception $e ) {
+			// Fall back to default plural-form function.
+			return $this->make_plural_form_function( 2, 'n != 1' );
+		}
 	}
 
 	/**
@@ -266,7 +275,7 @@
 }
 endif;
 
-if ( !class_exists( 'NOOP_Translations' ) ):
+if ( ! class_exists( 'NOOP_Translations', false ) ):
 /**
  * Provides the same interface as Translations, but doesn't do anything
  */
@@ -278,16 +287,33 @@
 		return true;
 	}
 
+	/**
+	 *
+	 * @param string $header
+	 * @param string $value
+	 */
 	function set_header($header, $value) {
 	}
 
+	/**
+	 *
+	 * @param array $headers
+	 */
 	function set_headers($headers) {
 	}
 
+	/**
+	 * @param string $header
+	 * @return false
+	 */
 	function get_header($header) {
 		return false;
 	}
 
+	/**
+	 * @param Translation_Entry $entry
+	 * @return false
+	 */
 	function translate_entry(&$entry) {
 		return false;
 	}
@@ -300,10 +326,18 @@
 		return $singular;
 	}
 
+	/**
+	 *
+	 * @param int $count
+	 * @return bool
+	 */
 	function select_plural_form($count) {
 		return 1 == $count? 0 : 1;
 	}
 
+	/**
+	 * @return int
+	 */
 	function get_plural_forms_count() {
 		return 2;
 	}
@@ -318,6 +352,9 @@
 			return 1 == $count? $singular : $plural;
 	}
 
+	/**
+	 * @param object $other
+	 */
 	function merge_with(&$other) {
 	}
 }