Skip to content

Commit 0f6b0f1

Browse files
committed
[translation] [update] Add informative log and a manual confirmation before uploading + Remove safety check + Bring node-fetch back since it is experimental and showing warnings.
1 parent 3ecbea7 commit 0f6b0f1

File tree

10 files changed

+196
-18
lines changed

10 files changed

+196
-18
lines changed

gulptasks/transifex.js

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
const {transifexApi} = require('@transifex/api');
88
const fs = require('node:fs');
99
const log = require('fancy-log');
10+
const prompts = require('prompts');
11+
const {po} = require('gettext-parser');
12+
const chalk = require('chalk');
13+
const fetch = require('node-fetch'); // @todo - Remove once fetch is stabilized in NodeJS.
1014

1115
transifexApi.setup({
1216
auth: process.env.TRANSIFEX_API
@@ -16,18 +20,13 @@ const SOURCE_SLUG = 'freemius-enpo';
1620
const SOURCE_NAME = 'freemius-en.po';
1721

1822
async function getOrganization() {
19-
// Safety check, unless we feel 100% confident, this wouldn't break the existing resources.
20-
if ('wordpress-sdk' === process.env.TRANSIFEX_ORGANIZATION) {
21-
throw new Error('Can not use the production organization yet!');
22-
}
23-
2423
const organization = await transifexApi.Organization.get({slug: process.env.TRANSIFEX_ORGANIZATION});
2524

2625
if (!organization) {
2726
throw new Error(`Organization "${process.env.TRANSIFEX_ORGANIZATION}" not found!`);
2827
}
2928

30-
log(`Using organization "${organization.attributes.name}"`);
29+
log(`Using organization "${chalk.bold.bgBlack.yellow(organization.attributes.name)}"`);
3130

3231
return organization;
3332
}
@@ -44,7 +43,7 @@ async function getProject(organization) {
4443
throw new Error(`Project "${process.env.TRANSIFEX_PROJECT}" not found!`);
4544
}
4645

47-
log(`Using project "${project.attributes.name}"`);
46+
log(`Using project "${chalk.bold.bgBlack.yellow(project.attributes.name)}"`);
4847

4948
return project;
5049
}
@@ -78,10 +77,10 @@ async function getResourceStringForUpload(project, name, slug, i18nFormat) {
7877

7978
try {
8079
resource = await resources.get({slug});
81-
log(`Resource "${name}" already exists, updating...`)
80+
log(`Resource "${chalk.bold.bgBlack.yellow(name)}" already exists, updating...`)
8281
} catch (e) {
8382
// No resources yet
84-
log(`Creating resource "${name}"`);
83+
log(`Creating resource "${chalk.bold.bgBlack.yellow(name)}"`);
8584
resource = await transifexApi.Resource.create({
8685
name,
8786
slug,
@@ -93,17 +92,60 @@ async function getResourceStringForUpload(project, name, slug, i18nFormat) {
9392
return resource;
9493
}
9594

95+
function getLinesCount(parsedPot) {
96+
let sentenceCount = 0;
97+
let wordCount = 0;
98+
99+
Object.values(parsedPot.translations).forEach((messages) => {
100+
Object.keys((messages)).forEach((source) => {
101+
if ('' === source) {
102+
return;
103+
}
104+
105+
sentenceCount += 1;
106+
wordCount += source.split(' ').length;
107+
});
108+
});
109+
110+
return {sentenceCount, wordCount};
111+
}
112+
96113
async function uploadEnglishPoToTransifex(poPath) {
97114
const {organization, project} = await getOrgAndProject();
98115
const content = fs.readFileSync(poPath, {encoding: 'utf-8'});
99116

117+
// Get total number of lines in the file
118+
const parsedPot = po.parse(content);
119+
120+
// Get total lines count from the parsedPot
121+
const {sentenceCount, wordCount} = getLinesCount(parsedPot);
122+
100123
const i18nFormat = await transifexApi.i18n_formats.get({
101124
organization,
102125
name: 'PO'
103126
});
104127

105128
const resource = await getResourceStringForUpload(project, SOURCE_NAME, SOURCE_SLUG, i18nFormat);
106129

130+
log(`ATTENTION: ${chalk.bold.red('UPLOADING')}!!`);
131+
log(`You are about to upload the freemius-en.po file to the production organization!.`);
132+
log(`In ${chalk.bgYellow.black('Transifex')} freemius-en.po file has ${chalk.bold.magenta(resource.attributes.string_count)} lines and ${chalk.bold.cyan(resource.attributes.word_count)} words.`);
133+
log(`In ${chalk.bgYellow.black('Local')} freemius-en.po file has ${chalk.bold.magenta(sentenceCount)} lines and ${chalk.bold.cyan(wordCount)} words.`);
134+
log(`Please make sure you have already tested the content of the file.`);
135+
log(chalk.bold.red('Any data loss could be permanent.'));
136+
137+
const confirmation = await prompts({
138+
type: 'confirm',
139+
message: `Do you want to upload the file freemius-en.po to Transifex?`,
140+
initial: false,
141+
name: 'value',
142+
});
143+
144+
if (!confirmation.value) {
145+
log('Aborting upload');
146+
return false;
147+
}
148+
107149
await transifexApi.ResourceStringsAsyncUpload.upload({
108150
resource,
109151
content,

gulptasks/translate.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const path = require('node:path');
55
const log = require('fancy-log');
66
const fs = require('node:fs');
77
const {po, mo} = require('gettext-parser');
8+
const chalk = require('chalk');
89
const {uploadEnglishPoToTransifex, getTranslation} = require('./transifex');
910

1011
const root = path.resolve(__dirname, '..');
@@ -94,20 +95,26 @@ function updateTranslationFiles(languageCode, translation) {
9495
async function syncWithTransifex() {
9596
log('Updaing Transifex source...');
9697
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+
97104
log('Transifex updated.');
98105

99106
// Loop over LANGUAGE_CODE and download and update every one of them
100107
for (const code of LANGUAGE_CODES) {
101-
log(`Updating ${code}...`);
108+
log(`Updating ${chalk.cyan(code)}...`);
102109
try {
103110
const translation = await getTranslation(code, resource);
104111

105112
// Update the po file in the file system
106113
updateTranslationFiles(code, translation);
107114

108-
log(`Updated ${code}.`);
115+
log(`Updated ${chalk.cyan(code)}.`);
109116
} catch (e) {
110-
log.error(`Failed to get translation of ${code}, skipping...`);
117+
log.error(`Failed to get translation of ${chalk.red(code)}, skipping...`);
111118
}
112119
}
113120
}

languages/freemius-de_DE.po

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ msgid ""
77
msgstr ""
88
"Project-Id-Version: freemius\n"
99
"Report-Msgid-Bugs-To: https://github.com/Freemius/wordpress-sdk/issues\n"
10-
"POT-Creation-Date: 2024-01-30 15:07+0000\n"
10+
"POT-Creation-Date: 2024-03-31 08:37+0000\n"
1111
"Last-Translator: Swashata Ghosh, 2024\n"
1212
"Language-Team: German (Germany) "
1313
"(https://app.transifex.com/freemius/teams/184351/de_DE/)\n"

languages/freemius-es_ES.po

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ msgid ""
77
msgstr ""
88
"Project-Id-Version: freemius\n"
99
"Report-Msgid-Bugs-To: https://github.com/Freemius/wordpress-sdk/issues\n"
10-
"POT-Creation-Date: 2024-01-30 15:07+0000\n"
10+
"POT-Creation-Date: 2024-03-31 08:37+0000\n"
1111
"Last-Translator: Swashata Ghosh, 2024\n"
1212
"Language-Team: Spanish (Spain) "
1313
"(https://app.transifex.com/freemius/teams/184351/es_ES/)\n"

languages/freemius-fr_FR.po

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ msgid ""
77
msgstr ""
88
"Project-Id-Version: freemius\n"
99
"Report-Msgid-Bugs-To: https://github.com/Freemius/wordpress-sdk/issues\n"
10-
"POT-Creation-Date: 2024-01-30 15:07+0000\n"
10+
"POT-Creation-Date: 2024-03-31 08:37+0000\n"
1111
"Last-Translator: Swashata Ghosh, 2024\n"
1212
"Language-Team: French (France) "
1313
"(https://app.transifex.com/freemius/teams/184351/fr_FR/)\n"

languages/freemius-nl_NL.po

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ msgid ""
77
msgstr ""
88
"Project-Id-Version: freemius\n"
99
"Report-Msgid-Bugs-To: https://github.com/Freemius/wordpress-sdk/issues\n"
10-
"POT-Creation-Date: 2024-01-30 15:42+0000\n"
10+
"POT-Creation-Date: 2024-03-31 08:37+0000\n"
1111
"Last-Translator: Swashata Ghosh, 2024\n"
1212
"Language-Team: Dutch (Netherlands) "
1313
"(https://app.transifex.com/freemius/teams/184351/nl_NL/)\n"

languages/freemius-ru_RU.po

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ msgid ""
77
msgstr ""
88
"Project-Id-Version: freemius\n"
99
"Report-Msgid-Bugs-To: https://github.com/Freemius/wordpress-sdk/issues\n"
10-
"POT-Creation-Date: 2024-01-30 15:42+0000\n"
10+
"POT-Creation-Date: 2024-03-31 08:37+0000\n"
1111
"Last-Translator: Swashata Ghosh, 2024\n"
1212
"Language-Team: Russian (Russia) "
1313
"(https://app.transifex.com/freemius/teams/184351/ru_RU/)\n"

languages/freemius.pot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgstr ""
88
"Content-Transfer-Encoding: 8bit\n"
99
"Language-Team: Freemius Team <[email protected]>\n"
1010
"Last-Translator: Vova Feldman <[email protected]>\n"
11-
"POT-Creation-Date: 2024-01-31 07:21+0000\n"
11+
"POT-Creation-Date: 2024-03-31 09:35+0000\n"
1212
"Report-Msgid-Bugs-To: https://github.com/Freemius/wordpress-sdk/issues\n"
1313
"X-Poedit-Basepath: ..\n"
1414
"X-Poedit-KeywordsList: get_text_inline;get_text_x_inline:1,2c;$this->get_text_inline;$this->get_text_x_inline:1,2c;$this->_fs->get_text_inline;$this->_fs->get_text_x_inline:1,2c;$this->fs->get_text_inline;$this->fs->get_text_x_inline:1,2c;$fs->get_text_inline;$fs->get_text_x_inline:1,2c;$this->_parent->get_text_inline;$this->_parent->get_text_x_inline:1,2c;fs_text_inline;fs_echo_inline;fs_esc_js_inline;fs_esc_attr_inline;fs_esc_attr_echo_inline;fs_esc_html_inline;fs_esc_html_echo_inline;fs_text_x_inline:1,2c;fs_echo_x_inline:1,2c;fs_esc_attr_x_inline:1,2c;fs_esc_js_x_inline:1,2c;fs_esc_js_echo_x_inline:1,2c;fs_esc_html_x_inline:1,2c;fs_esc_html_echo_x_inline:1,2c\n"

0 commit comments

Comments
 (0)