| <?php |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| abstract class WP_Translation_File { |
| |
| |
| |
| |
| |
| |
| protected $headers = array(); |
|
|
| |
| |
| |
| |
| |
| |
| protected $parsed = false; |
|
|
| |
| |
| |
| |
| |
| |
| protected $error; |
|
|
| |
| |
| |
| |
| |
| |
| protected $file = ''; |
|
|
| |
| |
| |
| |
| |
| |
| protected $entries = array(); |
|
|
| |
| |
| |
| |
| |
| |
| protected $plural_forms = null; |
|
|
| |
| |
| |
| |
| |
| |
| |
| protected function __construct( string $file ) { |
| $this->file = $file; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static function create( string $file, ?string $filetype = null ) { |
| 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; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 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(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public function headers(): array { |
| if ( ! $this->parsed ) { |
| $this->parse_file(); |
| } |
| return $this->headers; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public function entries(): array { |
| if ( ! $this->parsed ) { |
| $this->parse_file(); |
| } |
|
|
| return $this->entries; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public function error() { |
| return $this->error; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public function get_file(): string { |
| return $this->file; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public function translate( string $text ) { |
| if ( ! $this->parsed ) { |
| $this->parse_file(); |
| } |
|
|
| return $this->entries[ $text ] ?? false; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public function get_plural_form( int $number ): int { |
| if ( ! $this->parsed ) { |
| $this->parse_file(); |
| } |
|
|
| 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 ) ) { |
| |
| |
| |
| |
| |
| $result = call_user_func( $this->plural_forms, $number ); |
|
|
| return $result; |
| } |
|
|
| |
| return ( 1 === $number ? 0 : 1 ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| 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] ); |
| } |
|
|
| return 'n != 1'; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected function make_plural_form_function( string $expression ): callable { |
| try { |
| $handler = new Plural_Forms( rtrim( $expression, ';' ) ); |
| return array( $handler, 'get' ); |
| } catch ( Exception $e ) { |
| |
| return $this->make_plural_form_function( 'n != 1' ); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| 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; |
| } |
|
|
| |
| |
| |
| |
| |
| abstract protected function parse_file(); |
|
|
| |
| |
| |
| |
| |
| |
| |
| abstract public function export(); |
| } |
|
|