summaryrefslogtreecommitdiffstats
path: root/wp-includes/l10n/class-wp-translation-file.php
blob: e550f9c2f042a156621b4340038ff95d8fa127e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?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
	 */
	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;
		}
	}

	/**
	 * 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();
		}

		return $this->entries[ $text ] ?? false;
	}

	/**
	 * Returns the plural form for a given number.
	 *
	 * @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'] ) ) {
			$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 ) ) {
			/**
			 * Plural form.
			 *
			 * @var int $result Plural form.
			 */
			$result = call_user_func( $this->plural_forms, $number );

			return $result;
		}

		// Default plural form matches English, only "One" is considered singular.
		return ( 1 === $number ? 0 : 1 );
	}

	/**
	 * Returns the plural forms expression as a tuple.
	 *
	 * @since 6.5.0
	 *
	 * @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] );
		}

		return 'n != 1';
	}

	/**
	 * 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.
	 */
	protected function make_plural_form_function( string $expression ): callable {
		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();
}