equal
deleted
inserted
replaced
11 |
11 |
12 if ( ! defined( 'PO_MAX_LINE_LEN' ) ) { |
12 if ( ! defined( 'PO_MAX_LINE_LEN' ) ) { |
13 define( 'PO_MAX_LINE_LEN', 79 ); |
13 define( 'PO_MAX_LINE_LEN', 79 ); |
14 } |
14 } |
15 |
15 |
16 ini_set( 'auto_detect_line_endings', 1 ); |
16 /* |
|
17 * The `auto_detect_line_endings` setting has been deprecated in PHP 8.1, |
|
18 * but will continue to work until PHP 9.0. |
|
19 * For now, we're silencing the deprecation notice as there may still be |
|
20 * translation files around which haven't been updated in a long time and |
|
21 * which still use the old MacOS standalone `\r` as a line ending. |
|
22 * This fix should be revisited when PHP 9.0 is in alpha/beta. |
|
23 */ |
|
24 @ini_set( 'auto_detect_line_endings', 1 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
17 |
25 |
18 /** |
26 /** |
19 * Routines for working with PO files |
27 * Routines for working with PO files |
20 */ |
28 */ |
21 if ( ! class_exists( 'PO', false ) ) : |
29 if ( ! class_exists( 'PO', false ) ) : |
26 /** |
34 /** |
27 * Exports headers to a PO entry |
35 * Exports headers to a PO entry |
28 * |
36 * |
29 * @return string msgid/msgstr PO entry for this PO file headers, doesn't contain newline at the end |
37 * @return string msgid/msgstr PO entry for this PO file headers, doesn't contain newline at the end |
30 */ |
38 */ |
31 function export_headers() { |
39 public function export_headers() { |
32 $header_string = ''; |
40 $header_string = ''; |
33 foreach ( $this->headers as $header => $value ) { |
41 foreach ( $this->headers as $header => $value ) { |
34 $header_string .= "$header: $value\n"; |
42 $header_string .= "$header: $value\n"; |
35 } |
43 } |
36 $poified = PO::poify( $header_string ); |
44 $poified = PO::poify( $header_string ); |
45 /** |
53 /** |
46 * Exports all entries to PO format |
54 * Exports all entries to PO format |
47 * |
55 * |
48 * @return string sequence of mgsgid/msgstr PO strings, doesn't containt newline at the end |
56 * @return string sequence of mgsgid/msgstr PO strings, doesn't containt newline at the end |
49 */ |
57 */ |
50 function export_entries() { |
58 public function export_entries() { |
51 // TODO: Sorting. |
59 // TODO: Sorting. |
52 return implode( "\n\n", array_map( array( 'PO', 'export_entry' ), $this->entries ) ); |
60 return implode( "\n\n", array_map( array( 'PO', 'export_entry' ), $this->entries ) ); |
53 } |
61 } |
54 |
62 |
55 /** |
63 /** |
56 * Exports the whole PO file as a string |
64 * Exports the whole PO file as a string |
57 * |
65 * |
58 * @param bool $include_headers whether to include the headers in the export |
66 * @param bool $include_headers whether to include the headers in the export |
59 * @return string ready for inclusion in PO file string for headers and all the enrtries |
67 * @return string ready for inclusion in PO file string for headers and all the enrtries |
60 */ |
68 */ |
61 function export( $include_headers = true ) { |
69 public function export( $include_headers = true ) { |
62 $res = ''; |
70 $res = ''; |
63 if ( $include_headers ) { |
71 if ( $include_headers ) { |
64 $res .= $this->export_headers(); |
72 $res .= $this->export_headers(); |
65 $res .= "\n\n"; |
73 $res .= "\n\n"; |
66 } |
74 } |
73 * |
81 * |
74 * @param string $filename Where to write the PO string. |
82 * @param string $filename Where to write the PO string. |
75 * @param bool $include_headers Whether to include the headers in the export. |
83 * @param bool $include_headers Whether to include the headers in the export. |
76 * @return bool true on success, false on error |
84 * @return bool true on success, false on error |
77 */ |
85 */ |
78 function export_to_file( $filename, $include_headers = true ) { |
86 public function export_to_file( $filename, $include_headers = true ) { |
79 $fh = fopen( $filename, 'w' ); |
87 $fh = fopen( $filename, 'w' ); |
80 if ( false === $fh ) { |
88 if ( false === $fh ) { |
81 return false; |
89 return false; |
82 } |
90 } |
83 $export = $this->export( $include_headers ); |
91 $export = $this->export( $include_headers ); |
93 * |
101 * |
94 * Doesn't need to include # in the beginning of lines, these are added automatically |
102 * Doesn't need to include # in the beginning of lines, these are added automatically |
95 * |
103 * |
96 * @param string $text Text to include as a comment. |
104 * @param string $text Text to include as a comment. |
97 */ |
105 */ |
98 function set_comment_before_headers( $text ) { |
106 public function set_comment_before_headers( $text ) { |
99 $this->comments_before_headers = $text; |
107 $this->comments_before_headers = $text; |
100 } |
108 } |
101 |
109 |
102 /** |
110 /** |
103 * Formats a string in PO-style |
111 * Formats a string in PO-style |
161 $unpoified .= isset( $escapes[ $char ] ) ? $escapes[ $char ] : $char; |
169 $unpoified .= isset( $escapes[ $char ] ) ? $escapes[ $char ] : $char; |
162 } |
170 } |
163 } |
171 } |
164 } |
172 } |
165 |
173 |
166 // Standardise the line endings on imported content, technically PO files shouldn't contain \r. |
174 // Standardize the line endings on imported content, technically PO files shouldn't contain \r. |
167 $unpoified = str_replace( array( "\r\n", "\r" ), "\n", $unpoified ); |
175 $unpoified = str_replace( array( "\r\n", "\r" ), "\n", $unpoified ); |
168 |
176 |
169 return $unpoified; |
177 return $unpoified; |
170 } |
178 } |
171 |
179 |
283 |
291 |
284 /** |
292 /** |
285 * @param string $filename |
293 * @param string $filename |
286 * @return bool |
294 * @return bool |
287 */ |
295 */ |
288 function import_from_file( $filename ) { |
296 public function import_from_file( $filename ) { |
289 $f = fopen( $filename, 'r' ); |
297 $f = fopen( $filename, 'r' ); |
290 if ( ! $f ) { |
298 if ( ! $f ) { |
291 return false; |
299 return false; |
292 } |
300 } |
293 $lineno = 0; |
301 $lineno = 0; |
325 /** |
333 /** |
326 * @param resource $f |
334 * @param resource $f |
327 * @param int $lineno |
335 * @param int $lineno |
328 * @return null|false|array |
336 * @return null|false|array |
329 */ |
337 */ |
330 function read_entry( $f, $lineno = 0 ) { |
338 public function read_entry( $f, $lineno = 0 ) { |
331 $entry = new Translation_Entry(); |
339 $entry = new Translation_Entry(); |
332 // Where were we in the last step. |
340 // Where were we in the last step. |
333 // Can be: comment, msgctxt, msgid, msgid_plural, msgstr, msgstr_plural. |
341 // Can be: comment, msgctxt, msgid, msgid_plural, msgstr, msgstr_plural. |
334 $context = ''; |
342 $context = ''; |
335 $msgstr_index = 0; |
343 $msgstr_index = 0; |
454 /** |
462 /** |
455 * @param resource $f |
463 * @param resource $f |
456 * @param string $action |
464 * @param string $action |
457 * @return bool |
465 * @return bool |
458 */ |
466 */ |
459 function read_line( $f, $action = 'read' ) { |
467 public function read_line( $f, $action = 'read' ) { |
460 static $last_line = ''; |
468 static $last_line = ''; |
461 static $use_last_line = false; |
469 static $use_last_line = false; |
462 if ( 'clear' === $action ) { |
470 if ( 'clear' === $action ) { |
463 $last_line = ''; |
471 $last_line = ''; |
464 return true; |
472 return true; |
476 |
484 |
477 /** |
485 /** |
478 * @param Translation_Entry $entry |
486 * @param Translation_Entry $entry |
479 * @param string $po_comment_line |
487 * @param string $po_comment_line |
480 */ |
488 */ |
481 function add_comment_to_entry( &$entry, $po_comment_line ) { |
489 public function add_comment_to_entry( &$entry, $po_comment_line ) { |
482 $first_two = substr( $po_comment_line, 0, 2 ); |
490 $first_two = substr( $po_comment_line, 0, 2 ); |
483 $comment = trim( substr( $po_comment_line, 2 ) ); |
491 $comment = trim( substr( $po_comment_line, 2 ) ); |
484 if ( '#:' === $first_two ) { |
492 if ( '#:' === $first_two ) { |
485 $entry->references = array_merge( $entry->references, preg_split( '/\s+/', $comment ) ); |
493 $entry->references = array_merge( $entry->references, preg_split( '/\s+/', $comment ) ); |
486 } elseif ( '#.' === $first_two ) { |
494 } elseif ( '#.' === $first_two ) { |