I18N: Introduce a more performant localization library.
This introduces a more lightweight library for loading `.mo` translation files which offers increased speed and lower memory usage.
It also supports loading multiple locales at the same time, which makes locale switching faster too.
For plugins interacting with the `$l10n` global variable in core, a shim is added to retain backward compatibility with the existing `pomo` library.
In addition to that, this library supports translations contained in PHP files, avoiding a binary file format and leveraging OPCache if available.
If an `.mo` translation file has a corresponding `.l10n.php` file, the latter will be loaded instead.
This behavior can be adjusted using the new `translation_file_format` and `load_translation_file` filters.
PHP translation files will be typically created by downloading language packs, but can also be generated by plugins.
See https://make.wordpress.org/core/2023/11/08/merging-performant-translations-into-core/ for more context.
Props dd32, swissspidy, flixos90, joemcgill, westonruter, akirk, SergeyBiryukov.
Fixes #59656.
Built from https://develop.svn.wordpress.org/trunk@57337
git-svn-id: http://core.svn.wordpress.org/trunk@56843 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-23 08:34:11 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* I18N: WP_Translation_File class.
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @subpackage I18N
|
|
|
|
* @since 6.5.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class WP_Translation_File.
|
|
|
|
*
|
|
|
|
* @since 6.5.0
|
|
|
|
*/
|
|
|
|
abstract class WP_Translation_File {
|
|
|
|
/**
|
|
|
|
* List of headers.
|
|
|
|
*
|
|
|
|
* @since 6.5.0
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
|
|
|
protected $headers = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether file has been parsed.
|
|
|
|
*
|
|
|
|
* @since 6.5.0
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $parsed = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Error information.
|
|
|
|
*
|
|
|
|
* @since 6.5.0
|
|
|
|
* @var string|null Error message or null if no error.
|
|
|
|
*/
|
|
|
|
protected $error;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* File name.
|
|
|
|
*
|
|
|
|
* @since 6.5.0
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $file = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translation entries.
|
|
|
|
*
|
|
|
|
* @since 6.5.0
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
|
|
|
protected $entries = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Plural forms function.
|
|
|
|
*
|
|
|
|
* @since 6.5.0
|
|
|
|
* @var callable|null Plural forms.
|
|
|
|
*/
|
|
|
|
protected $plural_forms = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @since 6.5.0
|
|
|
|
*
|
|
|
|
* @param string $file File to load.
|
|
|
|
*/
|
|
|
|
protected function __construct( string $file ) {
|
|
|
|
$this->file = $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new WP_Translation_File instance for a given file.
|
|
|
|
*
|
|
|
|
* @since 6.5.0
|
|
|
|
*
|
|
|
|
* @param string $file File name.
|
|
|
|
* @param string|null $filetype Optional. File type. Default inferred from file name.
|
|
|
|
* @return false|WP_Translation_File
|
|
|
|
*/
|
Code Modernization: Fix implicit nullable parameter type deprecation on PHP 8.4.
In PHP 8.4, declaring function or method parameters with a default value of `null` is deprecated if the type is not nullable.
PHP applications are recommended to ''explicitly'' declare the type as nullable. All type declarations that have a default value of `null`, but without declaring `null` in the type declaration, will emit a deprecation notice:
{{{
function test( array $value = null ) {}
}}}
`Deprecated: Implicitly marking parameter $value as nullable is deprecated, the explicit nullable type must be used instead`
**Recommended Changes**
Change the implicit nullable type declaration to a nullable type declaration, available since PHP 7.1:
{{{#!diff
- function test( string $test = null ) {}
+ function test( ?string $test = null ) {}
}}}
This commit updates the affected instances in core to use a nullable type declaration.
References:
* [https://wiki.php.net/rfc/deprecate-implicitly-nullable-types PHP RFC: Deprecate implicitly nullable parameter types]
* [https://php.watch/versions/8.4/implicitly-marking-parameter-type-nullable-deprecated PHP.Watch: PHP 8.4: Implicitly nullable parameter declarations deprecated]
Follow-up to [28731], [50552], [57337], [57985].
Props ayeshrajans, jrf, audrasjb, jorbin.
Fixes #60786.
Built from https://develop.svn.wordpress.org/trunk@58009
git-svn-id: http://core.svn.wordpress.org/trunk@57480 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-04-15 16:03:09 -04:00
|
|
|
public static function create( string $file, ?string $filetype = null ) {
|
I18N: Introduce a more performant localization library.
This introduces a more lightweight library for loading `.mo` translation files which offers increased speed and lower memory usage.
It also supports loading multiple locales at the same time, which makes locale switching faster too.
For plugins interacting with the `$l10n` global variable in core, a shim is added to retain backward compatibility with the existing `pomo` library.
In addition to that, this library supports translations contained in PHP files, avoiding a binary file format and leveraging OPCache if available.
If an `.mo` translation file has a corresponding `.l10n.php` file, the latter will be loaded instead.
This behavior can be adjusted using the new `translation_file_format` and `load_translation_file` filters.
PHP translation files will be typically created by downloading language packs, but can also be generated by plugins.
See https://make.wordpress.org/core/2023/11/08/merging-performant-translations-into-core/ for more context.
Props dd32, swissspidy, flixos90, joemcgill, westonruter, akirk, SergeyBiryukov.
Fixes #59656.
Built from https://develop.svn.wordpress.org/trunk@57337
git-svn-id: http://core.svn.wordpress.org/trunk@56843 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-23 08:34:11 -05:00
|
|
|
if ( ! is_readable( $file ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( null === $filetype ) {
|
|
|
|
$pos = strrpos( $file, '.' );
|
|
|
|
if ( false !== $pos ) {
|
|
|
|
$filetype = substr( $file, $pos + 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( $filetype ) {
|
|
|
|
case 'mo':
|
|
|
|
return new WP_Translation_File_MO( $file );
|
|
|
|
case 'php':
|
|
|
|
return new WP_Translation_File_PHP( $file );
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new WP_Translation_File instance for a given file.
|
|
|
|
*
|
|
|
|
* @since 6.5.0
|
|
|
|
*
|
|
|
|
* @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 ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( $filetype ) {
|
|
|
|
case 'mo':
|
|
|
|
$destination = new WP_Translation_File_MO( '' );
|
|
|
|
break;
|
|
|
|
case 'php':
|
|
|
|
$destination = new WP_Translation_File_PHP( '' );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$success = $destination->import( $source );
|
|
|
|
|
|
|
|
if ( ! $success ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $destination->export();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all headers.
|
|
|
|
*
|
|
|
|
* @since 6.5.0
|
|
|
|
*
|
|
|
|
* @return array<string, string> Headers.
|
|
|
|
*/
|
|
|
|
public function headers(): array {
|
|
|
|
if ( ! $this->parsed ) {
|
|
|
|
$this->parse_file();
|
|
|
|
}
|
|
|
|
return $this->headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all entries.
|
|
|
|
*
|
|
|
|
* @since 6.5.0
|
|
|
|
*
|
|
|
|
* @return array<string, string[]> Entries.
|
|
|
|
*/
|
|
|
|
public function entries(): array {
|
|
|
|
if ( ! $this->parsed ) {
|
|
|
|
$this->parse_file();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current error information.
|
|
|
|
*
|
|
|
|
* @since 6.5.0
|
|
|
|
*
|
|
|
|
* @return string|null Error message or null if no error.
|
|
|
|
*/
|
|
|
|
public function error() {
|
|
|
|
return $this->error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the file name.
|
|
|
|
*
|
|
|
|
* @since 6.5.0
|
|
|
|
*
|
|
|
|
* @return string File name.
|
|
|
|
*/
|
|
|
|
public function get_file(): string {
|
|
|
|
return $this->file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Translates a given string.
|
|
|
|
*
|
|
|
|
* @since 6.5.0
|
|
|
|
*
|
|
|
|
* @param string $text String to translate.
|
|
|
|
* @return false|string Translation(s) on success, false otherwise.
|
|
|
|
*/
|
|
|
|
public function translate( string $text ) {
|
|
|
|
if ( ! $this->parsed ) {
|
|
|
|
$this->parse_file();
|
|
|
|
}
|
|
|
|
|
2024-01-31 16:51:14 -05:00
|
|
|
return $this->entries[ $text ] ?? false;
|
I18N: Introduce a more performant localization library.
This introduces a more lightweight library for loading `.mo` translation files which offers increased speed and lower memory usage.
It also supports loading multiple locales at the same time, which makes locale switching faster too.
For plugins interacting with the `$l10n` global variable in core, a shim is added to retain backward compatibility with the existing `pomo` library.
In addition to that, this library supports translations contained in PHP files, avoiding a binary file format and leveraging OPCache if available.
If an `.mo` translation file has a corresponding `.l10n.php` file, the latter will be loaded instead.
This behavior can be adjusted using the new `translation_file_format` and `load_translation_file` filters.
PHP translation files will be typically created by downloading language packs, but can also be generated by plugins.
See https://make.wordpress.org/core/2023/11/08/merging-performant-translations-into-core/ for more context.
Props dd32, swissspidy, flixos90, joemcgill, westonruter, akirk, SergeyBiryukov.
Fixes #59656.
Built from https://develop.svn.wordpress.org/trunk@57337
git-svn-id: http://core.svn.wordpress.org/trunk@56843 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-23 08:34:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-02-01 15:59:14 -05:00
|
|
|
* Returns the plural form for a given number.
|
I18N: Introduce a more performant localization library.
This introduces a more lightweight library for loading `.mo` translation files which offers increased speed and lower memory usage.
It also supports loading multiple locales at the same time, which makes locale switching faster too.
For plugins interacting with the `$l10n` global variable in core, a shim is added to retain backward compatibility with the existing `pomo` library.
In addition to that, this library supports translations contained in PHP files, avoiding a binary file format and leveraging OPCache if available.
If an `.mo` translation file has a corresponding `.l10n.php` file, the latter will be loaded instead.
This behavior can be adjusted using the new `translation_file_format` and `load_translation_file` filters.
PHP translation files will be typically created by downloading language packs, but can also be generated by plugins.
See https://make.wordpress.org/core/2023/11/08/merging-performant-translations-into-core/ for more context.
Props dd32, swissspidy, flixos90, joemcgill, westonruter, akirk, SergeyBiryukov.
Fixes #59656.
Built from https://develop.svn.wordpress.org/trunk@57337
git-svn-id: http://core.svn.wordpress.org/trunk@56843 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-23 08:34:11 -05:00
|
|
|
*
|
|
|
|
* @since 6.5.0
|
|
|
|
*
|
|
|
|
* @param int $number Count.
|
|
|
|
* @return int Plural form.
|
|
|
|
*/
|
|
|
|
public function get_plural_form( int $number ): int {
|
|
|
|
if ( ! $this->parsed ) {
|
|
|
|
$this->parse_file();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( null === $this->plural_forms && isset( $this->headers['plural-forms'] ) ) {
|
2024-02-01 15:59:14 -05:00
|
|
|
$expression = $this->get_plural_expression_from_header( $this->headers['plural-forms'] );
|
|
|
|
$this->plural_forms = $this->make_plural_form_function( $expression );
|
I18N: Introduce a more performant localization library.
This introduces a more lightweight library for loading `.mo` translation files which offers increased speed and lower memory usage.
It also supports loading multiple locales at the same time, which makes locale switching faster too.
For plugins interacting with the `$l10n` global variable in core, a shim is added to retain backward compatibility with the existing `pomo` library.
In addition to that, this library supports translations contained in PHP files, avoiding a binary file format and leveraging OPCache if available.
If an `.mo` translation file has a corresponding `.l10n.php` file, the latter will be loaded instead.
This behavior can be adjusted using the new `translation_file_format` and `load_translation_file` filters.
PHP translation files will be typically created by downloading language packs, but can also be generated by plugins.
See https://make.wordpress.org/core/2023/11/08/merging-performant-translations-into-core/ for more context.
Props dd32, swissspidy, flixos90, joemcgill, westonruter, akirk, SergeyBiryukov.
Fixes #59656.
Built from https://develop.svn.wordpress.org/trunk@57337
git-svn-id: http://core.svn.wordpress.org/trunk@56843 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-23 08:34:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( is_callable( $this->plural_forms ) ) {
|
|
|
|
/**
|
|
|
|
* Plural form.
|
|
|
|
*
|
|
|
|
* @var int $result Plural form.
|
|
|
|
*/
|
|
|
|
$result = call_user_func( $this->plural_forms, $number );
|
2024-02-01 15:59:14 -05:00
|
|
|
|
I18N: Introduce a more performant localization library.
This introduces a more lightweight library for loading `.mo` translation files which offers increased speed and lower memory usage.
It also supports loading multiple locales at the same time, which makes locale switching faster too.
For plugins interacting with the `$l10n` global variable in core, a shim is added to retain backward compatibility with the existing `pomo` library.
In addition to that, this library supports translations contained in PHP files, avoiding a binary file format and leveraging OPCache if available.
If an `.mo` translation file has a corresponding `.l10n.php` file, the latter will be loaded instead.
This behavior can be adjusted using the new `translation_file_format` and `load_translation_file` filters.
PHP translation files will be typically created by downloading language packs, but can also be generated by plugins.
See https://make.wordpress.org/core/2023/11/08/merging-performant-translations-into-core/ for more context.
Props dd32, swissspidy, flixos90, joemcgill, westonruter, akirk, SergeyBiryukov.
Fixes #59656.
Built from https://develop.svn.wordpress.org/trunk@57337
git-svn-id: http://core.svn.wordpress.org/trunk@56843 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-23 08:34:11 -05:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default plural form matches English, only "One" is considered singular.
|
|
|
|
return ( 1 === $number ? 0 : 1 );
|
|
|
|
}
|
|
|
|
|
2024-02-01 15:59:14 -05:00
|
|
|
/**
|
|
|
|
* Returns the plural forms expression as a tuple.
|
|
|
|
*
|
|
|
|
* @since 6.5.0
|
|
|
|
*
|
|
|
|
* @param string $header Plural-Forms header string.
|
|
|
|
* @return string Plural forms expression.
|
|
|
|
*/
|
2024-02-02 04:07:14 -05:00
|
|
|
protected function get_plural_expression_from_header( string $header ): string {
|
2024-02-01 15:59:14 -05:00
|
|
|
if ( preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s+plural\s*=\s*(.+)$/', $header, $matches ) ) {
|
|
|
|
return trim( $matches[2] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'n != 1';
|
|
|
|
}
|
|
|
|
|
I18N: Introduce a more performant localization library.
This introduces a more lightweight library for loading `.mo` translation files which offers increased speed and lower memory usage.
It also supports loading multiple locales at the same time, which makes locale switching faster too.
For plugins interacting with the `$l10n` global variable in core, a shim is added to retain backward compatibility with the existing `pomo` library.
In addition to that, this library supports translations contained in PHP files, avoiding a binary file format and leveraging OPCache if available.
If an `.mo` translation file has a corresponding `.l10n.php` file, the latter will be loaded instead.
This behavior can be adjusted using the new `translation_file_format` and `load_translation_file` filters.
PHP translation files will be typically created by downloading language packs, but can also be generated by plugins.
See https://make.wordpress.org/core/2023/11/08/merging-performant-translations-into-core/ for more context.
Props dd32, swissspidy, flixos90, joemcgill, westonruter, akirk, SergeyBiryukov.
Fixes #59656.
Built from https://develop.svn.wordpress.org/trunk@57337
git-svn-id: http://core.svn.wordpress.org/trunk@56843 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-23 08:34:11 -05:00
|
|
|
/**
|
|
|
|
* Makes a function, which will return the right translation index, according to the
|
|
|
|
* plural forms header.
|
|
|
|
*
|
|
|
|
* @since 6.5.0
|
|
|
|
*
|
|
|
|
* @param string $expression Plural form expression.
|
|
|
|
* @return callable(int $num): int Plural forms function.
|
|
|
|
*/
|
2024-02-01 15:59:14 -05:00
|
|
|
protected function make_plural_form_function( string $expression ): callable {
|
I18N: Introduce a more performant localization library.
This introduces a more lightweight library for loading `.mo` translation files which offers increased speed and lower memory usage.
It also supports loading multiple locales at the same time, which makes locale switching faster too.
For plugins interacting with the `$l10n` global variable in core, a shim is added to retain backward compatibility with the existing `pomo` library.
In addition to that, this library supports translations contained in PHP files, avoiding a binary file format and leveraging OPCache if available.
If an `.mo` translation file has a corresponding `.l10n.php` file, the latter will be loaded instead.
This behavior can be adjusted using the new `translation_file_format` and `load_translation_file` filters.
PHP translation files will be typically created by downloading language packs, but can also be generated by plugins.
See https://make.wordpress.org/core/2023/11/08/merging-performant-translations-into-core/ for more context.
Props dd32, swissspidy, flixos90, joemcgill, westonruter, akirk, SergeyBiryukov.
Fixes #59656.
Built from https://develop.svn.wordpress.org/trunk@57337
git-svn-id: http://core.svn.wordpress.org/trunk@56843 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-23 08:34:11 -05:00
|
|
|
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( 'n != 1' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Imports translations from another file.
|
|
|
|
*
|
|
|
|
* @since 6.5.0
|
|
|
|
*
|
|
|
|
* @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() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->headers = $source->headers();
|
|
|
|
$this->entries = $source->entries();
|
|
|
|
$this->error = $source->error();
|
|
|
|
|
|
|
|
return null === $this->error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses the file.
|
|
|
|
*
|
|
|
|
* @since 6.5.0
|
|
|
|
*/
|
|
|
|
abstract protected function parse_file();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exports translation contents as a string.
|
|
|
|
*
|
|
|
|
* @since 6.5.0
|
|
|
|
*
|
|
|
|
* @return string Translation file contents.
|
|
|
|
*/
|
|
|
|
abstract public function export();
|
|
|
|
}
|