diff -r 7b1b88e27a20 -r 48c4eec2b7e6 wp/wp-includes/pomo/translations.php --- a/wp/wp-includes/pomo/translations.php Thu Sep 29 08:06:27 2022 +0200 +++ b/wp/wp-includes/pomo/translations.php Fri Sep 05 18:40:08 2025 +0200 @@ -5,21 +5,45 @@ * @version $Id: translations.php 1157 2015-11-20 04:30:11Z dd32 $ * @package pomo * @subpackage translations + * @since 2.8.0 */ require_once __DIR__ . '/plural-forms.php'; require_once __DIR__ . '/entry.php'; if ( ! class_exists( 'Translations', false ) ) : + /** + * Translations class. + * + * @since 2.8.0 + */ + #[AllowDynamicProperties] class Translations { + /** + * List of translation entries. + * + * @since 2.8.0 + * + * @var Translation_Entry[] + */ public $entries = array(); + + /** + * List of translation headers. + * + * @since 2.8.0 + * + * @var array + */ public $headers = array(); /** - * Add entry to the PO structure + * Adds an entry to the PO structure. + * + * @since 2.8.0 * * @param array|Translation_Entry $entry - * @return bool true on success, false if the entry doesn't have a key + * @return bool True on success, false if the entry doesn't have a key. */ public function add_entry( $entry ) { if ( is_array( $entry ) ) { @@ -34,8 +58,12 @@ } /** + * Adds or merges an entry to the PO structure. + * + * @since 2.8.0 + * * @param array|Translation_Entry $entry - * @return bool + * @return bool True on success, false if the entry doesn't have a key. */ public function add_entry_or_merge( $entry ) { if ( is_array( $entry ) ) { @@ -60,6 +88,8 @@ * * TODO: this should be out of this class, it is gettext specific * + * @since 2.8.0 + * * @param string $header header name, without trailing : * @param string $value header value, without trailing \n */ @@ -68,7 +98,11 @@ } /** - * @param array $headers + * Sets translation headers. + * + * @since 2.8.0 + * + * @param array $headers Associative array of headers. */ public function set_headers( $headers ) { foreach ( $headers as $header => $value ) { @@ -77,14 +111,24 @@ } /** + * Returns a given translation header. + * + * @since 2.8.0 + * * @param string $header + * @return string|false Header if it exists, false otherwise. */ public function get_header( $header ) { return isset( $this->headers[ $header ] ) ? $this->headers[ $header ] : false; } /** - * @param Translation_Entry $entry + * Returns a given translation entry. + * + * @since 2.8.0 + * + * @param Translation_Entry $entry Translation entry. + * @return Translation_Entry|false Translation entry if it exists, false otherwise. */ public function translate_entry( &$entry ) { $key = $entry->key(); @@ -92,6 +136,10 @@ } /** + * Translates a singular string. + * + * @since 2.8.0 + * * @param string $singular * @param string $context * @return string @@ -116,24 +164,36 @@ * This function should be overridden by the subclasses. For example MO/PO can derive the logic * from their headers. * - * @param int $count number of items + * @since 2.8.0 + * + * @param int $count Number of items. + * @return int Plural form to use. */ public function select_plural_form( $count ) { - return 1 == $count ? 0 : 1; + return 1 === (int) $count ? 0 : 1; } /** - * @return int + * Returns the plural forms count. + * + * @since 2.8.0 + * + * @return int Plural forms count. */ public function get_plural_forms_count() { return 2; } /** + * Translates a plural string. + * + * @since 2.8.0 + * * @param string $singular * @param string $plural * @param int $count * @param string $context + * @return string */ public function translate_plural( $singular, $plural, $count, $context = null ) { $entry = new Translation_Entry( @@ -151,14 +211,16 @@ isset( $translated->translations[ $index ] ) ) { return $translated->translations[ $index ]; } else { - return 1 == $count ? $singular : $plural; + return 1 === (int) $count ? $singular : $plural; } } /** - * Merge $other in the current object. + * Merges other translations into the current one. * - * @param Object $other Another Translation object, whose translations will be merged in this one (passed by reference). + * @since 2.8.0 + * + * @param Translations $other Another Translation object, whose translations will be merged in this one (passed by reference). */ public function merge_with( &$other ) { foreach ( $other->entries as $entry ) { @@ -167,7 +229,11 @@ } /** - * @param object $other + * Merges originals with existing entries. + * + * @since 2.8.0 + * + * @param Translations $other */ public function merge_originals_with( &$other ) { foreach ( $other->entries as $entry ) { @@ -180,14 +246,41 @@ } } + /** + * Gettext_Translations class. + * + * @since 2.8.0 + */ class Gettext_Translations extends Translations { + + /** + * Number of plural forms. + * + * @var int + * + * @since 2.8.0 + */ + public $_nplurals; + + /** + * Callback to retrieve the plural form. + * + * @var callable + * + * @since 2.8.0 + */ + public $_gettext_select_plural_form; + /** * The gettext implementation of select_plural_form. * - * It lives in this class, because there are more than one descendand, which will use it and + * It lives in this class, because there are more than one descendant, which will use it and * they can't share it effectively. * - * @param int $count + * @since 2.8.0 + * + * @param int $count Plural forms count. + * @return int Plural form to use. */ public function gettext_select_plural_form( $count ) { if ( ! isset( $this->_gettext_select_plural_form ) || is_null( $this->_gettext_select_plural_form ) ) { @@ -199,8 +292,12 @@ } /** + * Returns the nplurals and plural forms expression from the Plural-Forms header. + * + * @since 2.8.0 + * * @param string $header - * @return array + * @return array{0: int, 1: string} */ public function nplurals_and_expression_from_header( $header ) { if ( preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $header, $matches ) ) { @@ -214,10 +311,13 @@ /** * Makes a function, which will return the right translation index, according to the - * plural forms header + * plural forms header. + * + * @since 2.8.0 * * @param int $nplurals * @param string $expression + * @return callable */ public function make_plural_form_function( $nplurals, $expression ) { try { @@ -231,7 +331,12 @@ /** * Adds parentheses to the inner parts of ternary operators in - * plural expressions, because PHP evaluates ternary oerators from left to right + * plural expressions, because PHP evaluates ternary operators from left to right + * + * @since 2.8.0 + * @deprecated 6.5.0 Use the Plural_Forms class instead. + * + * @see Plural_Forms * * @param string $expression the expression without parentheses * @return string the expression with parentheses added @@ -245,7 +350,7 @@ switch ( $char ) { case '?': $res .= ' ? ('; - $depth++; + ++$depth; break; case ':': $res .= ') : ('; @@ -262,8 +367,12 @@ } /** + * Prepare translation headers. + * + * @since 2.8.0 + * * @param string $translation - * @return array + * @return array Translation headers */ public function make_headers( $translation ) { $headers = array(); @@ -281,6 +390,10 @@ } /** + * Sets translation headers. + * + * @since 2.8.0 + * * @param string $header * @param string $value */ @@ -297,10 +410,28 @@ if ( ! class_exists( 'NOOP_Translations', false ) ) : /** - * Provides the same interface as Translations, but doesn't do anything + * Provides the same interface as Translations, but doesn't do anything. + * + * @since 2.8.0 */ + #[AllowDynamicProperties] class NOOP_Translations { + /** + * List of translation entries. + * + * @since 2.8.0 + * + * @var Translation_Entry[] + */ public $entries = array(); + + /** + * List of translation headers. + * + * @since 2.8.0 + * + * @var array + */ public $headers = array(); public function add_entry( $entry ) { @@ -308,6 +439,10 @@ } /** + * Sets a translation header. + * + * @since 2.8.0 + * * @param string $header * @param string $value */ @@ -315,12 +450,20 @@ } /** + * Sets translation headers. + * + * @since 2.8.0 + * * @param array $headers */ public function set_headers( $headers ) { } /** + * Returns a translation header. + * + * @since 2.8.0 + * * @param string $header * @return false */ @@ -329,6 +472,10 @@ } /** + * Returns a given translation entry. + * + * @since 2.8.0 + * * @param Translation_Entry $entry * @return false */ @@ -337,6 +484,10 @@ } /** + * Translates a singular string. + * + * @since 2.8.0 + * * @param string $singular * @param string $context */ @@ -345,14 +496,22 @@ } /** + * Returns the plural form to use. + * + * @since 2.8.0 + * * @param int $count - * @return bool + * @return int */ public function select_plural_form( $count ) { - return 1 == $count ? 0 : 1; + return 1 === (int) $count ? 0 : 1; } /** + * Returns the plural forms count. + * + * @since 2.8.0 + * * @return int */ public function get_plural_forms_count() { @@ -360,17 +519,26 @@ } /** + * Translates a plural string. + * + * @since 2.8.0 + * * @param string $singular * @param string $plural * @param int $count * @param string $context + * @return string */ public function translate_plural( $singular, $plural, $count, $context = null ) { - return 1 == $count ? $singular : $plural; + return 1 === (int) $count ? $singular : $plural; } /** - * @param object $other + * Merges other translations into the current one. + * + * @since 2.8.0 + * + * @param Translations $other */ public function merge_with( &$other ) { }