| 1 |
const {src, dest, series} = require('gulp'); |
| 2 |
const sort = require('gulp-sort'); |
| 3 |
const wpPot = require('gulp-wp-pot'); |
| 4 |
const path = require('node:path'); |
| 5 |
const log = require('fancy-log'); |
| 6 |
const fs = require('node:fs'); |
| 7 |
const {po, mo} = require('gettext-parser'); |
| 8 |
const chalk = require('chalk'); |
| 9 |
const {uploadEnglishPoToTransifex, getTranslation} = require('./transifex'); |
| 10 |
|
| 11 |
const root = path.resolve(__dirname, '..'); |
| 12 |
const languagesFolder = path.resolve(root, './languages'); |
| 13 |
const freemiusPotPath = path.resolve(languagesFolder, './freemius.pot'); |
| 14 |
|
| 15 |
const LANGUAGE_CODES = [ |
| 16 |
'cs_CZ', |
| 17 |
'da_DK', |
| 18 |
'de_DE', |
| 19 |
'es_ES', |
| 20 |
'fr_FR', |
| 21 |
'he_IL', |
| 22 |
'hu_HU', |
| 23 |
'it_IT', |
| 24 |
'ja', |
| 25 |
'nl_NL', |
| 26 |
'ru_RU', |
| 27 |
'ta', |
| 28 |
'zh_CN', |
| 29 |
]; |
| 30 |
|
| 31 |
function translatePHP() { |
| 32 |
return src('**/*.php') |
| 33 |
.pipe(sort()) |
| 34 |
.pipe(wpPot({ |
| 35 |
destFile : 'freemius.pot', |
| 36 |
package : 'freemius', |
| 37 |
bugReport : 'https://github.com/Freemius/wordpress-sdk/issues', |
| 38 |
lastTranslator: 'Vova Feldman <[email protected]>', |
| 39 |
team : 'Freemius Team <[email protected]>', |
| 40 |
|
| 41 |
gettextFunctions: [ |
| 42 |
// #region Methods from the Freemius class. |
| 43 |
{name: 'get_text_inline'}, |
| 44 |
{name: 'get_text_x_inline', context: 2}, |
| 45 |
|
| 46 |
{name: '$this->get_text_inline'}, |
| 47 |
{name: '$this->get_text_x_inline', context: 2}, |
| 48 |
|
| 49 |
{name: '$this->_fs->get_text_inline'}, |
| 50 |
{name: '$this->_fs->get_text_x_inline', context: 2}, |
| 51 |
|
| 52 |
{name: '$this->fs->get_text_inline'}, |
| 53 |
{name: '$this->fs->get_text_x_inline', context: 2}, |
| 54 |
|
| 55 |
{name: '$fs->get_text_inline'}, |
| 56 |
{name: '$fs->get_text_x_inline', context: 2}, |
| 57 |
|
| 58 |
{name: '$this->_parent->get_text_inline'}, |
| 59 |
{name: '$this->_parent->get_text_x_inline', context: 2}, |
| 60 |
|
| 61 |
// #endregion |
| 62 |
|
| 63 |
{name: 'fs_text_inline'}, |
| 64 |
{name: 'fs_echo_inline'}, |
| 65 |
{name: 'fs_esc_js_inline'}, |
| 66 |
{name: 'fs_esc_attr_inline'}, |
| 67 |
{name: 'fs_esc_attr_echo_inline'}, |
| 68 |
{name: 'fs_esc_html_inline'}, |
| 69 |
{name: 'fs_esc_html_echo_inline'}, |
| 70 |
|
| 71 |
{name: 'fs_text_x_inline', context: 2}, |
| 72 |
{name: 'fs_echo_x_inline', context: 2}, |
| 73 |
{name: 'fs_esc_attr_x_inline', context: 2}, |
| 74 |
{name: 'fs_esc_js_x_inline', context: 2}, |
| 75 |
{name: 'fs_esc_js_echo_x_inline', context: 2}, |
| 76 |
{name: 'fs_esc_html_x_inline', context: 2}, |
| 77 |
{name: 'fs_esc_html_echo_x_inline', context: 2}, |
| 78 |
], |
| 79 |
})) |
| 80 |
.pipe(dest(freemiusPotPath)); |
| 81 |
} |
| 82 |
|
| 83 |
function updateTranslationFiles(languageCode, translation) { |
| 84 |
const fileName = `freemius-${languageCode}`; |
| 85 |
// Do a parsing, just to be sure that the translation is valid. |
| 86 |
const parsedPo = po.parse(translation); |
| 87 |
|
| 88 |
const poFilePath = path.resolve(languagesFolder, `./${fileName}.po`); |
| 89 |
fs.writeFileSync(poFilePath, po.compile(parsedPo)); |
| 90 |
|
| 91 |
const moFilePath = path.resolve(languagesFolder, `./${fileName}.mo`); |
| 92 |
fs.writeFileSync(moFilePath, mo.compile(parsedPo)); |
| 93 |
} |
| 94 |
|
| 95 |
async function syncWithTransifex() { |
| 96 |
log('Updaing Transifex source...'); |
| 97 |
const resource = await uploadEnglishPoToTransifex(freemiusPotPath); |
| 98 |
|
| 99 |
if (false === resource) { |
| 100 |
log.error('Failed to upload the English po file to Transifex.'); |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
log('Transifex updated.'); |
| 105 |
|
| 106 |
// Loop over LANGUAGE_CODE and download and update every one of them |
| 107 |
for (const code of LANGUAGE_CODES) { |
| 108 |
log(`Updating ${chalk.cyan(code)}...`); |
| 109 |
try { |
| 110 |
const translation = await getTranslation(code, resource); |
| 111 |
|
| 112 |
// Update the po file in the file system |
| 113 |
updateTranslationFiles(code, translation); |
| 114 |
|
| 115 |
log(`Updated ${chalk.cyan(code)}.`); |
| 116 |
} catch (e) { |
| 117 |
log.error(`Failed to get translation of ${chalk.red(code)}, skipping...`); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
exports.createTranslation = series(translatePHP, syncWithTransifex); |
| 123 |
|
| 124 |
exports.createPot = translatePHP; |
| 125 |
|