|
| 1 | +/** |
| 2 | + * External dependencies |
| 3 | + */ |
| 4 | +const path = require( 'path' ); |
| 5 | +const glob = require( 'fast-glob' ); |
| 6 | +const fs = require( 'fs' ); |
| 7 | +const { EOL } = require( 'os' ); |
| 8 | + |
| 9 | +/** |
| 10 | + * Internal dependencies |
| 11 | + */ |
| 12 | +const { log, formats } = require( '../lib/logger' ); |
| 13 | +const config = require( '../config' ); |
| 14 | + |
| 15 | +const TAB = '\t'; |
| 16 | +const NEWLINE = EOL; |
| 17 | +const FILE_HEADER = `<?php |
| 18 | +/* THIS IS A GENERATED FILE. DO NOT EDIT DIRECTLY. */ |
| 19 | +$generated_i18n_strings = array( |
| 20 | +`; |
| 21 | +const FILE_FOOTER = ` |
| 22 | +); |
| 23 | +/* THIS IS THE END OF THE GENERATED FILE */ |
| 24 | +`; |
| 25 | + |
| 26 | +/** |
| 27 | + * @typedef WPTranslationsCommandOptions |
| 28 | + * |
| 29 | + * @property {string=} directory Optional directory, default is the root `/modules` directory. |
| 30 | + * @property {string=} output Optional output PHP file, default is the root `/module-i18n.php` file. |
| 31 | + */ |
| 32 | + |
| 33 | +/** |
| 34 | + * @typedef WPTranslationsSettings |
| 35 | + * |
| 36 | + * @property {string} textDomain Plugin text domain. |
| 37 | + * @property {string} directory Modules directory. |
| 38 | + * @property {string} output Output PHP file. |
| 39 | + */ |
| 40 | + |
| 41 | +/** |
| 42 | + * @typedef WPTranslationEntry |
| 43 | + * |
| 44 | + * @property {string} text String to translate. |
| 45 | + * @property {string} context Context for translators. |
| 46 | + */ |
| 47 | + |
| 48 | +exports.options = [ |
| 49 | + { |
| 50 | + argname: '-d, --directory <directory>', |
| 51 | + description: 'Modules directory', |
| 52 | + }, |
| 53 | + { |
| 54 | + argname: '-d, --output <output>', |
| 55 | + description: 'Output file', |
| 56 | + }, |
| 57 | +]; |
| 58 | + |
| 59 | +/** |
| 60 | + * Command that generates a PHP file from module header translation strings. |
| 61 | + * |
| 62 | + * @param {WPTranslationsCommandOptions} opt |
| 63 | + */ |
| 64 | +exports.handler = async ( opt ) => { |
| 65 | + await createTranslations( { |
| 66 | + textDomain: config.textDomain, |
| 67 | + directory: opt.directory || 'modules', |
| 68 | + output: opt.output || 'module-i18n.php', |
| 69 | + } ); |
| 70 | +}; |
| 71 | + |
| 72 | +/** |
| 73 | + * Parses module header translation strings. |
| 74 | + * |
| 75 | + * @param {WPTranslationsSettings} settings Translations settings. |
| 76 | + * |
| 77 | + * @return {[]WPTranslationEntry} List of translation entries. |
| 78 | + */ |
| 79 | +async function getTranslations( settings ) { |
| 80 | + const moduleFilePattern = path.join( settings.directory, '*/load.php' ); |
| 81 | + const moduleFiles = await glob( path.resolve( '.', moduleFilePattern ) ); |
| 82 | + |
| 83 | + const moduleTranslations = moduleFiles |
| 84 | + .map( ( moduleFile ) => { |
| 85 | + // Map of module header => translator context. |
| 86 | + const headers = { |
| 87 | + 'Module Name': 'module name', |
| 88 | + Description: 'module description', |
| 89 | + }; |
| 90 | + const translationEntries = []; |
| 91 | + |
| 92 | + const fileContent = fs.readFileSync( moduleFile, 'utf8' ); |
| 93 | + const regex = new RegExp( |
| 94 | + `^(?:[ \t]*<?php)?[ \t/*#@]*(${ Object.keys( headers ).join( |
| 95 | + '|' |
| 96 | + ) }):(.*)$`, |
| 97 | + 'gmi' |
| 98 | + ); |
| 99 | + let match = regex.exec( fileContent ); |
| 100 | + while ( match ) { |
| 101 | + const text = match[ 2 ].trim(); |
| 102 | + const context = headers[ match[ 1 ] ]; |
| 103 | + if ( text && context ) { |
| 104 | + translationEntries.push( { |
| 105 | + text, |
| 106 | + context, |
| 107 | + } ); |
| 108 | + } |
| 109 | + match = regex.exec( fileContent ); |
| 110 | + } |
| 111 | + |
| 112 | + return translationEntries; |
| 113 | + } ) |
| 114 | + .filter( ( translations ) => !! translations.length ); |
| 115 | + |
| 116 | + return moduleTranslations.flat(); |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Parses module header translation strings. |
| 121 | + * |
| 122 | + * @param {[]WPTranslationEntry} translations List of translation entries. |
| 123 | + * @param {WPTranslationsSettings} settings Translations settings. |
| 124 | + */ |
| 125 | +function createTranslationsPHPFile( translations, settings ) { |
| 126 | + const output = translations.map( ( translation ) => { |
| 127 | + // Escape single quotes. |
| 128 | + return `${ TAB }_x( '${ translation.text.replace( /'/g, "\\'" ) }', '${ |
| 129 | + translation.context |
| 130 | + }', '${ settings.textDomain }' ),`; |
| 131 | + } ); |
| 132 | + |
| 133 | + const fileOutput = `${ FILE_HEADER }${ output.join( |
| 134 | + NEWLINE |
| 135 | + ) }${ FILE_FOOTER }`; |
| 136 | + fs.writeFileSync( path.join( '.', settings.output ), fileOutput ); |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * Parses module header translation strings and generates a PHP file with them. |
| 141 | + * |
| 142 | + * @param {WPTranslationsSettings} settings Translations settings. |
| 143 | + */ |
| 144 | +async function createTranslations( settings ) { |
| 145 | + log( |
| 146 | + formats.title( |
| 147 | + `\n💃Preparing module translations for "${ settings.directory }" in "${ settings.output }"\n\n` |
| 148 | + ) |
| 149 | + ); |
| 150 | + |
| 151 | + try { |
| 152 | + const translations = await getTranslations( settings ); |
| 153 | + createTranslationsPHPFile( translations, settings ); |
| 154 | + } catch ( error ) { |
| 155 | + if ( error instanceof Error ) { |
| 156 | + log( formats.error( error.stack ) ); |
| 157 | + return; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + log( |
| 162 | + formats.success( |
| 163 | + `\n💃Module translations successfully set in "${ settings.output }"\n\n` |
| 164 | + ) |
| 165 | + ); |
| 166 | +} |
0 commit comments