mirror of
https://github.com/WordPress/WordPress.git
synced 2024-11-14 22:56:19 +01:00
c6ccfb1242
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
84 lines
1.9 KiB
PHP
84 lines
1.9 KiB
PHP
<?php
|
|
/**
|
|
* I18N: WP_Translation_File_PHP class.
|
|
*
|
|
* @package WordPress
|
|
* @subpackage I18N
|
|
* @since 6.5.0
|
|
*/
|
|
|
|
/**
|
|
* Class WP_Translation_File_PHP.
|
|
*
|
|
* @since 6.5.0
|
|
*/
|
|
class WP_Translation_File_PHP extends WP_Translation_File {
|
|
/**
|
|
* Parses the file.
|
|
*
|
|
* @since 6.5.0
|
|
*/
|
|
protected function parse_file() {
|
|
$this->parsed = true;
|
|
|
|
$result = include $this->file;
|
|
if ( ! $result || ! is_array( $result ) ) {
|
|
$this->error = 'Invalid data';
|
|
return;
|
|
}
|
|
|
|
if ( isset( $result['messages'] ) && is_array( $result['messages'] ) ) {
|
|
foreach ( $result['messages'] as $singular => $translations ) {
|
|
if ( is_array( $translations ) ) {
|
|
$this->entries[ $singular ] = implode( "\0", $translations );
|
|
} elseif ( is_string( $translations ) ) {
|
|
$this->entries[ $singular ] = $translations;
|
|
}
|
|
}
|
|
unset( $result['messages'] );
|
|
}
|
|
|
|
$this->headers = array_change_key_case( $result );
|
|
}
|
|
|
|
/**
|
|
* Exports translation contents as a string.
|
|
*
|
|
* @since 6.5.0
|
|
*
|
|
* @return string Translation file contents.
|
|
*/
|
|
public function export(): string {
|
|
$data = array_merge( $this->headers, array( 'messages' => $this->entries ) );
|
|
|
|
return '<?php' . PHP_EOL . 'return ' . $this->var_export( $data ) . ';' . PHP_EOL;
|
|
}
|
|
|
|
/**
|
|
* Outputs or returns a parsable string representation of a variable.
|
|
*
|
|
* Like {@see var_export()} but "minified", using short array syntax
|
|
* and no newlines.
|
|
*
|
|
* @since 6.5.0
|
|
*
|
|
* @param mixed $value The variable you want to export.
|
|
* @return string The variable representation.
|
|
*/
|
|
private function var_export( $value ): string {
|
|
if ( ! is_array( $value ) ) {
|
|
return var_export( $value, true );
|
|
}
|
|
|
|
$entries = array();
|
|
|
|
$is_list = array_is_list( $value );
|
|
|
|
foreach ( $value as $key => $val ) {
|
|
$entries[] = $is_list ? $this->var_export( $val ) : var_export( $key, true ) . '=>' . $this->var_export( $val );
|
|
}
|
|
|
|
return '[' . implode( ',', $entries ) . ']';
|
|
}
|
|
}
|