|
| 1 | +<?php |
| 2 | +// phpcs:ignoreFile - This is an auxiliary build tool, and not part of the plugin. |
| 3 | + |
| 4 | +/** |
| 5 | + * Command line script for updating the file references of JS files in a .pot file. |
| 6 | + */ |
| 7 | + |
| 8 | +use phpDocumentor\Reflection\DocBlock\Tags\Var_; |
| 9 | + |
| 10 | +/** |
| 11 | + * Get the file name from the command line. |
| 12 | + */ |
| 13 | +if ( $argc !== 2 ) { |
| 14 | + echo "Usage: {$argc} php -f {$argv[0]} file.pot\n"; |
| 15 | + exit; |
| 16 | +} |
| 17 | + |
| 18 | +$pot_filename = $argv[1]; |
| 19 | + |
| 20 | +if ( ! is_file( $pot_filename ) ) { |
| 21 | + echo "[ERROR] File not found: {$pot_filename}\n"; |
| 22 | + exit; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Parses a .pot file into an array. |
| 27 | + * |
| 28 | + * @param string $file_name Pot file name. |
| 29 | + * @return array Translation messages |
| 30 | + */ |
| 31 | +function read_pot_translations( string $file_name ): array { |
| 32 | + $fh = fopen( $file_name, 'r' ); |
| 33 | + $originals = []; |
| 34 | + $references = []; |
| 35 | + $messages = []; |
| 36 | + $have_msgid = false; |
| 37 | + |
| 38 | + while ( ! feof( $fh ) ) { |
| 39 | + $line = trim( fgets( $fh ) ); |
| 40 | + if ( ! $line ) { |
| 41 | + $message = implode( "\n", $messages ); |
| 42 | + $originals[ $message ] = $references; |
| 43 | + $references = []; |
| 44 | + $messages = []; |
| 45 | + $have_msgid = false; |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + if ( 'msgid' == substr( $line, 0, 5 ) ) { |
| 50 | + $have_msgid = true; |
| 51 | + } |
| 52 | + |
| 53 | + if ( $have_msgid ) { |
| 54 | + $messages[] = $line; |
| 55 | + } else { |
| 56 | + $references[] = $line; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + fclose( $fh ); |
| 61 | + |
| 62 | + $message = implode( "\n", $messages ); |
| 63 | + $originals[ $message ] = $references; |
| 64 | + return $originals; |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Add the transpiled file path to the references of the translation messages. |
| 69 | + * |
| 70 | + * @param array $translations POT translations (including references/comments). |
| 71 | + * @return array Translation messages |
| 72 | + */ |
| 73 | +function add_transpiled_filepath_reference_to_comments( array $translations ): array { |
| 74 | + foreach ( $translations as $message => $references ) { |
| 75 | + // Check references for js/jsx/ts/tsx files |
| 76 | + $dist_js_to_add = []; |
| 77 | + foreach ( $references as $i => $ref ) { |
| 78 | + if ( preg_match( '%^#: (build.+)(\.(js|jsx|ts|tsx)):\d+$%', $ref, $m ) ) { |
| 79 | + if ( preg_match( '%\.min\.js$%', $m[1] ) ) { |
| 80 | + unset( $translations[ $message ][ $i ] ); |
| 81 | + continue; |
| 82 | + } |
| 83 | + if ( empty( $m[2] ) ) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + $dist_js_to_add[] = "#: {$m[1]}.min{$m[2]}:1"; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // Add the new file references to the top of the list. |
| 91 | + if ( ! empty( $dist_js_to_add ) ) { |
| 92 | + array_splice( $translations[ $message], 0, 0, array_unique( $dist_js_to_add ) ); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return $translations; |
| 97 | +} |
| 98 | + |
| 99 | +// Read the translation .pot file. |
| 100 | +$translations = read_pot_translations( $pot_filename ); |
| 101 | + |
| 102 | +// For transpiled JS client files, we need to add a reference to the generated build file. |
| 103 | +$translations = add_transpiled_filepath_reference_to_comments( $translations ); |
| 104 | + |
| 105 | +// Delete the original source. |
| 106 | +unlink( $pot_filename ); |
| 107 | + |
| 108 | +$fh = fopen( $pot_filename, 'w' ); |
| 109 | + |
| 110 | +foreach ( $translations as $message => $original ) { |
| 111 | + fwrite( $fh, implode( "\n", $original ) ); |
| 112 | + fwrite( $fh, "\n" . $message . "\n\n" ); |
| 113 | +} |
| 114 | + |
| 115 | +fclose( $fh ); |
| 116 | + |
| 117 | +echo "Updated {$pot_filename}\n"; |
0 commit comments