: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* I18N: WP_Translation_File class.
* Class WP_Translation_File.
abstract class WP_Translation_File {
* @var array<string, string>
protected $headers = array();
* Whether file has been parsed.
protected $parsed = false;
* @var string|null Error message or null if no error.
* @var array<string, string>
protected $entries = array();
* @var callable|null Plural forms.
protected $plural_forms = null;
* @param string $file File to load.
protected function __construct( string $file ) {
* Creates a new WP_Translation_File instance for a given file.
* @param string $file File name.
* @param string|null $filetype Optional. File type. Default inferred from file name.
* @return false|WP_Translation_File
public static function create( string $file, ?string $filetype = null ) {
if ( ! is_readable( $file ) ) {
if ( null === $filetype ) {
$pos = strrpos( $file, '.' );
$filetype = substr( $file, $pos + 1 );
return new WP_Translation_File_MO( $file );
return new WP_Translation_File_PHP( $file );
* Creates a new WP_Translation_File instance for a given file.
* @param string $file Source file name.
* @param string $filetype Desired target file type.
* @return string|false Transformed translation file contents on success, false otherwise.
public static function transform( string $file, string $filetype ) {
$source = self::create( $file );
if ( false === $source ) {
$destination = new WP_Translation_File_MO( '' );
$destination = new WP_Translation_File_PHP( '' );
$success = $destination->import( $source );
return $destination->export();
* @return array<string, string> Headers.
public function headers(): array {
* @return array<string, string[]> Entries.
public function entries(): array {
* Returns the current error information.
* @return string|null Error message or null if no error.
public function error() {
* @return string File name.
public function get_file(): string {
* Translates a given string.
* @param string $text String to translate.
* @return false|string Translation(s) on success, false otherwise.
public function translate( string $text ) {
return $this->entries[ $text ] ?? false;
* Returns the plural form for a given number.
* @param int $number Count.
* @return int Plural form.
public function get_plural_form( int $number ): int {
if ( null === $this->plural_forms && isset( $this->headers['plural-forms'] ) ) {
$expression = $this->get_plural_expression_from_header( $this->headers['plural-forms'] );
$this->plural_forms = $this->make_plural_form_function( $expression );
if ( is_callable( $this->plural_forms ) ) {
* @var int $result Plural form.
$result = call_user_func( $this->plural_forms, $number );
// Default plural form matches English, only "One" is considered singular.
return ( 1 === $number ? 0 : 1 );
* Returns the plural forms expression as a tuple.
* @param string $header Plural-Forms header string.
* @return string Plural forms expression.
protected function get_plural_expression_from_header( string $header ): string {
if ( preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $header, $matches ) ) {
return trim( $matches[2] );
* Makes a function, which will return the right translation index, according to the
* @param string $expression Plural form expression.
* @return callable(int $num): int Plural forms function.
protected function make_plural_form_function( string $expression ): callable {
$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( 'n != 1' );
* Imports translations from another file.
* @param WP_Translation_File $source Source file.
* @return bool True on success, false otherwise.
protected function import( WP_Translation_File $source ): bool {
if ( null !== $source->error() ) {
$this->headers = $source->headers();
$this->entries = $source->entries();
$this->error = $source->error();
return null === $this->error;
abstract protected function parse_file();
* Exports translation contents as a string.
* @return string Translation file contents.
abstract public function export();