I18N: Improve docs for pomo library classes.

Props subrataemfluence, pento, hrshahin.
Fixes #44424.
Built from https://develop.svn.wordpress.org/trunk@57734


git-svn-id: http://core.svn.wordpress.org/trunk@57235 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Pascal Birchler 2024-02-28 09:31:12 +00:00
parent fb4f209a8c
commit fc91a29646
3 changed files with 177 additions and 20 deletions

View File

@ -10,6 +10,8 @@
if ( ! class_exists( 'Translation_Entry', false ) ) : if ( ! class_exists( 'Translation_Entry', false ) ) :
/** /**
* Translation_Entry class encapsulates a translatable string. * Translation_Entry class encapsulates a translatable string.
*
* @since 2.8.0
*/ */
#[AllowDynamicProperties] #[AllowDynamicProperties]
class Translation_Entry { class Translation_Entry {
@ -75,6 +77,7 @@ if ( ! class_exists( 'Translation_Entry', false ) ) :
/** /**
* PHP4 constructor. * PHP4 constructor.
* *
* @since 2.8.0
* @deprecated 5.4.0 Use __construct() instead. * @deprecated 5.4.0 Use __construct() instead.
* *
* @see Translation_Entry::__construct() * @see Translation_Entry::__construct()
@ -87,6 +90,8 @@ if ( ! class_exists( 'Translation_Entry', false ) ) :
/** /**
* Generates a unique key for this entry. * Generates a unique key for this entry.
* *
* @since 2.8.0
*
* @return string|false The key or false if the entry is null. * @return string|false The key or false if the entry is null.
*/ */
public function key() { public function key() {
@ -103,7 +108,11 @@ if ( ! class_exists( 'Translation_Entry', false ) ) :
} }
/** /**
* @param object $other * Merges another translation entry with the current one.
*
* @since 2.8.0
*
* @param Translation_Entry $other Other translation entry.
*/ */
public function merge_with( &$other ) { public function merge_with( &$other ) {
$this->flags = array_unique( array_merge( $this->flags, $other->flags ) ); $this->flags = array_unique( array_merge( $this->flags, $other->flags ) );

View File

@ -5,22 +5,45 @@
* @version $Id: translations.php 1157 2015-11-20 04:30:11Z dd32 $ * @version $Id: translations.php 1157 2015-11-20 04:30:11Z dd32 $
* @package pomo * @package pomo
* @subpackage translations * @subpackage translations
* @since 2.8.0
*/ */
require_once __DIR__ . '/plural-forms.php'; require_once __DIR__ . '/plural-forms.php';
require_once __DIR__ . '/entry.php'; require_once __DIR__ . '/entry.php';
if ( ! class_exists( 'Translations', false ) ) : if ( ! class_exists( 'Translations', false ) ) :
/**
* Translations class.
*
* @since 2.8.0
*/
#[AllowDynamicProperties] #[AllowDynamicProperties]
class Translations { class Translations {
/**
* List of translation entries.
*
* @since 2.8.0
*
* @var Translation_Entry[]
*/
public $entries = array(); public $entries = array();
/**
* List of translation headers.
*
* @since 2.8.0
*
* @var array<string, string>
*/
public $headers = 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 * @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 ) { public function add_entry( $entry ) {
if ( is_array( $entry ) ) { if ( is_array( $entry ) ) {
@ -35,8 +58,12 @@ if ( ! class_exists( 'Translations', false ) ) :
} }
/** /**
* Adds or merges an entry to the PO structure.
*
* @since 2.8.0
*
* @param array|Translation_Entry $entry * @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 ) { public function add_entry_or_merge( $entry ) {
if ( is_array( $entry ) ) { if ( is_array( $entry ) ) {
@ -61,6 +88,8 @@ if ( ! class_exists( 'Translations', false ) ) :
* *
* TODO: this should be out of this class, it is gettext specific * 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 $header header name, without trailing :
* @param string $value header value, without trailing \n * @param string $value header value, without trailing \n
*/ */
@ -69,7 +98,11 @@ if ( ! class_exists( 'Translations', false ) ) :
} }
/** /**
* @param array $headers * Sets translation headers.
*
* @since 2.8.0
*
* @param array $headers Associative array of headers.
*/ */
public function set_headers( $headers ) { public function set_headers( $headers ) {
foreach ( $headers as $header => $value ) { foreach ( $headers as $header => $value ) {
@ -78,14 +111,24 @@ if ( ! class_exists( 'Translations', false ) ) :
} }
/** /**
* Returns a given translation header.
*
* @since 2.8.0
*
* @param string $header * @param string $header
* @return string|false Header if it exists, false otherwise.
*/ */
public function get_header( $header ) { public function get_header( $header ) {
return isset( $this->headers[ $header ] ) ? $this->headers[ $header ] : false; 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 ) { public function translate_entry( &$entry ) {
$key = $entry->key(); $key = $entry->key();
@ -93,6 +136,10 @@ if ( ! class_exists( 'Translations', false ) ) :
} }
/** /**
* Translates a singular string.
*
* @since 2.8.0
*
* @param string $singular * @param string $singular
* @param string $context * @param string $context
* @return string * @return string
@ -117,24 +164,36 @@ if ( ! class_exists( 'Translations', false ) ) :
* This function should be overridden by the subclasses. For example MO/PO can derive the logic * This function should be overridden by the subclasses. For example MO/PO can derive the logic
* from their headers. * 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 ) { public function select_plural_form( $count ) {
return 1 === (int) $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() { public function get_plural_forms_count() {
return 2; return 2;
} }
/** /**
* Translates a plural string.
*
* @since 2.8.0
*
* @param string $singular * @param string $singular
* @param string $plural * @param string $plural
* @param int $count * @param int $count
* @param string $context * @param string $context
* @return string
*/ */
public function translate_plural( $singular, $plural, $count, $context = null ) { public function translate_plural( $singular, $plural, $count, $context = null ) {
$entry = new Translation_Entry( $entry = new Translation_Entry(
@ -157,9 +216,11 @@ if ( ! class_exists( 'Translations', false ) ) :
} }
/** /**
* 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 ) { public function merge_with( &$other ) {
foreach ( $other->entries as $entry ) { foreach ( $other->entries as $entry ) {
@ -168,7 +229,11 @@ if ( ! class_exists( 'Translations', false ) ) :
} }
/** /**
* @param object $other * Merges originals with existing entries.
*
* @since 2.8.0
*
* @param Translations $other
*/ */
public function merge_originals_with( &$other ) { public function merge_originals_with( &$other ) {
foreach ( $other->entries as $entry ) { foreach ( $other->entries as $entry ) {
@ -181,12 +246,19 @@ if ( ! class_exists( 'Translations', false ) ) :
} }
} }
/**
* Gettext_Translations class.
*
* @since 2.8.0
*/
class Gettext_Translations extends Translations { class Gettext_Translations extends Translations {
/** /**
* Number of plural forms. * Number of plural forms.
* *
* @var int * @var int
*
* @since 2.8.0
*/ */
public $_nplurals; public $_nplurals;
@ -194,16 +266,21 @@ if ( ! class_exists( 'Translations', false ) ) :
* Callback to retrieve the plural form. * Callback to retrieve the plural form.
* *
* @var callable * @var callable
*
* @since 2.8.0
*/ */
public $_gettext_select_plural_form; public $_gettext_select_plural_form;
/** /**
* The gettext implementation of 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. * 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 ) { public function gettext_select_plural_form( $count ) {
if ( ! isset( $this->_gettext_select_plural_form ) || is_null( $this->_gettext_select_plural_form ) ) { if ( ! isset( $this->_gettext_select_plural_form ) || is_null( $this->_gettext_select_plural_form ) ) {
@ -215,8 +292,12 @@ if ( ! class_exists( 'Translations', false ) ) :
} }
/** /**
* Returns the nplurals and plural forms expression from the Plural-Forms header.
*
* @since 2.8.0
*
* @param string $header * @param string $header
* @return array * @return array{0: int, 1: string}
*/ */
public function nplurals_and_expression_from_header( $header ) { public function nplurals_and_expression_from_header( $header ) {
if ( preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $header, $matches ) ) { if ( preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $header, $matches ) ) {
@ -230,10 +311,13 @@ if ( ! class_exists( 'Translations', false ) ) :
/** /**
* Makes a function, which will return the right translation index, according to the * 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 int $nplurals
* @param string $expression * @param string $expression
* @return callable
*/ */
public function make_plural_form_function( $nplurals, $expression ) { public function make_plural_form_function( $nplurals, $expression ) {
try { try {
@ -249,7 +333,9 @@ if ( ! class_exists( 'Translations', false ) ) :
* Adds parentheses to the inner parts of ternary operators in * Adds parentheses to the inner parts of ternary operators in
* plural expressions, because PHP evaluates ternary operators 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. * @deprecated 6.5.0 Use the Plural_Forms class instead.
*
* @see Plural_Forms * @see Plural_Forms
* *
* @param string $expression the expression without parentheses * @param string $expression the expression without parentheses
@ -281,8 +367,12 @@ if ( ! class_exists( 'Translations', false ) ) :
} }
/** /**
* Prepare translation headers.
*
* @since 2.8.0
*
* @param string $translation * @param string $translation
* @return array * @return array<string, string> Translation headers
*/ */
public function make_headers( $translation ) { public function make_headers( $translation ) {
$headers = array(); $headers = array();
@ -300,6 +390,10 @@ if ( ! class_exists( 'Translations', false ) ) :
} }
/** /**
* Sets translation headers.
*
* @since 2.8.0
*
* @param string $header * @param string $header
* @param string $value * @param string $value
*/ */
@ -316,11 +410,28 @@ endif;
if ( ! class_exists( 'NOOP_Translations', false ) ) : 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] #[AllowDynamicProperties]
class NOOP_Translations { class NOOP_Translations {
/**
* List of translation entries.
*
* @since 2.8.0
*
* @var Translation_Entry[]
*/
public $entries = array(); public $entries = array();
/**
* List of translation headers.
*
* @since 2.8.0
*
* @var array<string, string>
*/
public $headers = array(); public $headers = array();
public function add_entry( $entry ) { public function add_entry( $entry ) {
@ -328,6 +439,10 @@ if ( ! class_exists( 'NOOP_Translations', false ) ) :
} }
/** /**
* Sets a translation header.
*
* @since 2.8.0
*
* @param string $header * @param string $header
* @param string $value * @param string $value
*/ */
@ -335,12 +450,20 @@ if ( ! class_exists( 'NOOP_Translations', false ) ) :
} }
/** /**
* Sets translation headers.
*
* @since 2.8.0
*
* @param array $headers * @param array $headers
*/ */
public function set_headers( $headers ) { public function set_headers( $headers ) {
} }
/** /**
* Returns a translation header.
*
* @since 2.8.0
*
* @param string $header * @param string $header
* @return false * @return false
*/ */
@ -349,6 +472,10 @@ if ( ! class_exists( 'NOOP_Translations', false ) ) :
} }
/** /**
* Returns a given translation entry.
*
* @since 2.8.0
*
* @param Translation_Entry $entry * @param Translation_Entry $entry
* @return false * @return false
*/ */
@ -357,6 +484,10 @@ if ( ! class_exists( 'NOOP_Translations', false ) ) :
} }
/** /**
* Translates a singular string.
*
* @since 2.8.0
*
* @param string $singular * @param string $singular
* @param string $context * @param string $context
*/ */
@ -365,14 +496,22 @@ if ( ! class_exists( 'NOOP_Translations', false ) ) :
} }
/** /**
* Returns the plural form to use.
*
* @since 2.8.0
*
* @param int $count * @param int $count
* @return bool * @return int
*/ */
public function select_plural_form( $count ) { public function select_plural_form( $count ) {
return 1 === (int) $count ? 0 : 1; return 1 === (int) $count ? 0 : 1;
} }
/** /**
* Returns the plural forms count.
*
* @since 2.8.0
*
* @return int * @return int
*/ */
public function get_plural_forms_count() { public function get_plural_forms_count() {
@ -380,17 +519,26 @@ if ( ! class_exists( 'NOOP_Translations', false ) ) :
} }
/** /**
* Translates a plural string.
*
* @since 2.8.0
*
* @param string $singular * @param string $singular
* @param string $plural * @param string $plural
* @param int $count * @param int $count
* @param string $context * @param string $context
* @return string
*/ */
public function translate_plural( $singular, $plural, $count, $context = null ) { public function translate_plural( $singular, $plural, $count, $context = null ) {
return 1 === (int) $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 ) { public function merge_with( &$other ) {
} }

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.5-beta3-57733'; $wp_version = '6.5-beta3-57734';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.