From 93b44fc72757329d0b0082a6f66a6fcb354875bc Mon Sep 17 00:00:00 2001 From: nzambello Date: Sat, 23 Oct 2021 17:33:19 +0200 Subject: [PATCH] chore: upgrade to volto 14, use @plone/script i18n --- babel.config.js | 18 ++- locales/de/LC_MESSAGES/volto.po | 2 +- locales/en/LC_MESSAGES/volto.po | 2 +- locales/es/LC_MESSAGES/volto.po | 2 +- locales/eu/LC_MESSAGES/volto.po | 2 +- locales/fr/LC_MESSAGES/volto.po | 2 +- locales/it/LC_MESSAGES/volto.po | 2 +- locales/ja/LC_MESSAGES/volto.po | 2 +- locales/nl/LC_MESSAGES/volto.po | 2 +- locales/pt/LC_MESSAGES/volto.po | 2 +- locales/pt_BR/LC_MESSAGES/volto.po | 2 +- locales/ro/LC_MESSAGES/volto.po | 2 +- locales/volto.pot | 26 ++--- package.json | 8 +- src/i18n.js | 180 ----------------------------- 15 files changed, 48 insertions(+), 206 deletions(-) delete mode 100644 src/i18n.js diff --git a/babel.config.js b/babel.config.js index a900a75..56309e9 100644 --- a/babel.config.js +++ b/babel.config.js @@ -1 +1,17 @@ -module.exports = require('@plone/volto/babel'); +module.exports = function (api) { + api.cache(true); + const presets = ['razzle/babel']; + const plugins = [ + [ + 'react-intl', // React Intl extractor, required for the whole i18n infrastructure to work + { + messagesDir: './build/messages/', + }, + ], + ]; + + return { + plugins, + presets, + }; + }; diff --git a/locales/de/LC_MESSAGES/volto.po b/locales/de/LC_MESSAGES/volto.po index 89f1a20..12b9a47 100644 --- a/locales/de/LC_MESSAGES/volto.po +++ b/locales/de/LC_MESSAGES/volto.po @@ -11,4 +11,4 @@ msgstr "" "Content-Transfer-Encoding: \n" "Plural-Forms: \n" - + diff --git a/locales/en/LC_MESSAGES/volto.po b/locales/en/LC_MESSAGES/volto.po index 89f1a20..12b9a47 100644 --- a/locales/en/LC_MESSAGES/volto.po +++ b/locales/en/LC_MESSAGES/volto.po @@ -11,4 +11,4 @@ msgstr "" "Content-Transfer-Encoding: \n" "Plural-Forms: \n" - + diff --git a/locales/es/LC_MESSAGES/volto.po b/locales/es/LC_MESSAGES/volto.po index 89f1a20..12b9a47 100644 --- a/locales/es/LC_MESSAGES/volto.po +++ b/locales/es/LC_MESSAGES/volto.po @@ -11,4 +11,4 @@ msgstr "" "Content-Transfer-Encoding: \n" "Plural-Forms: \n" - + diff --git a/locales/eu/LC_MESSAGES/volto.po b/locales/eu/LC_MESSAGES/volto.po index 89f1a20..12b9a47 100644 --- a/locales/eu/LC_MESSAGES/volto.po +++ b/locales/eu/LC_MESSAGES/volto.po @@ -11,4 +11,4 @@ msgstr "" "Content-Transfer-Encoding: \n" "Plural-Forms: \n" - + diff --git a/locales/fr/LC_MESSAGES/volto.po b/locales/fr/LC_MESSAGES/volto.po index 89f1a20..12b9a47 100644 --- a/locales/fr/LC_MESSAGES/volto.po +++ b/locales/fr/LC_MESSAGES/volto.po @@ -11,4 +11,4 @@ msgstr "" "Content-Transfer-Encoding: \n" "Plural-Forms: \n" - + diff --git a/locales/it/LC_MESSAGES/volto.po b/locales/it/LC_MESSAGES/volto.po index 89f1a20..12b9a47 100644 --- a/locales/it/LC_MESSAGES/volto.po +++ b/locales/it/LC_MESSAGES/volto.po @@ -11,4 +11,4 @@ msgstr "" "Content-Transfer-Encoding: \n" "Plural-Forms: \n" - + diff --git a/locales/ja/LC_MESSAGES/volto.po b/locales/ja/LC_MESSAGES/volto.po index 89f1a20..12b9a47 100644 --- a/locales/ja/LC_MESSAGES/volto.po +++ b/locales/ja/LC_MESSAGES/volto.po @@ -11,4 +11,4 @@ msgstr "" "Content-Transfer-Encoding: \n" "Plural-Forms: \n" - + diff --git a/locales/nl/LC_MESSAGES/volto.po b/locales/nl/LC_MESSAGES/volto.po index 89f1a20..12b9a47 100644 --- a/locales/nl/LC_MESSAGES/volto.po +++ b/locales/nl/LC_MESSAGES/volto.po @@ -11,4 +11,4 @@ msgstr "" "Content-Transfer-Encoding: \n" "Plural-Forms: \n" - + diff --git a/locales/pt/LC_MESSAGES/volto.po b/locales/pt/LC_MESSAGES/volto.po index 89f1a20..12b9a47 100644 --- a/locales/pt/LC_MESSAGES/volto.po +++ b/locales/pt/LC_MESSAGES/volto.po @@ -11,4 +11,4 @@ msgstr "" "Content-Transfer-Encoding: \n" "Plural-Forms: \n" - + diff --git a/locales/pt_BR/LC_MESSAGES/volto.po b/locales/pt_BR/LC_MESSAGES/volto.po index 89f1a20..12b9a47 100644 --- a/locales/pt_BR/LC_MESSAGES/volto.po +++ b/locales/pt_BR/LC_MESSAGES/volto.po @@ -11,4 +11,4 @@ msgstr "" "Content-Transfer-Encoding: \n" "Plural-Forms: \n" - + diff --git a/locales/ro/LC_MESSAGES/volto.po b/locales/ro/LC_MESSAGES/volto.po index 89f1a20..12b9a47 100644 --- a/locales/ro/LC_MESSAGES/volto.po +++ b/locales/ro/LC_MESSAGES/volto.po @@ -11,4 +11,4 @@ msgstr "" "Content-Transfer-Encoding: \n" "Plural-Forms: \n" - + diff --git a/locales/volto.pot b/locales/volto.pot index 717413c..3f6c046 100644 --- a/locales/volto.pot +++ b/locales/volto.pot @@ -1,16 +1,16 @@ msgid "" - msgstr "" - "Project-Id-Version: Plone\n" - "POT-Creation-Date: 2021-08-07T17:39:51.701Z\n" - "Last-Translator: Plone i18n \n" - "Language-Team: Plone i18n \n" - "MIME-Version: 1.0\n" - "Content-Type: text/plain; charset=utf-8\n" - "Content-Transfer-Encoding: 8bit\n" - "Plural-Forms: nplurals=1; plural=0;\n" - "Language-Code: en\n" - "Language-Name: English\n" - "Preferred-Encodings: utf-8\n" - "Domain: volto\n" +msgstr "" +"Project-Id-Version: Plone\n" +"POT-Creation-Date: 2021-10-23T15:33:06.536Z\n" +"Last-Translator: Plone i18n \n" +"Language-Team: Plone i18n \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=utf-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=1; plural=0;\n" +"Language-Code: en\n" +"Language-Name: English\n" +"Preferred-Encodings: utf-8\n" +"Domain: volto\n" diff --git a/package.json b/package.json index dba6f47..86bcc31 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "analytics" ], "scripts": { - "i18n": "NODE_ENV=production node src/i18n.js" + "i18n": "rm -rf build/messages && NODE_ENV=production i18n -a" }, "husky": { "hooks": { @@ -25,6 +25,12 @@ "pre-commit": "lint-staged" } }, + "dependencies": { + "@plone/scripts": "^1.0.3" + }, + "peerDependencies": { + "@plone/volto": ">=14.0.0" + }, "devDependencies": { "@commitlint/cli": "^12.1.4", "@commitlint/config-conventional": "^12.1.4", diff --git a/src/i18n.js b/src/i18n.js deleted file mode 100644 index 84a3775..0000000 --- a/src/i18n.js +++ /dev/null @@ -1,180 +0,0 @@ -/* eslint no-console: 0 */ -/** - * i18n script. - * @module scripts/i18n - */ - -const { find, keys, map, concat, reduce } = require('lodash'); -const glob = require('glob').sync; -const fs = require('fs'); -const Pofile = require('pofile'); -const babel = require('@babel/core'); - -/** - * Extract messages into separate JSON files - * @function extractMessages - * @return {undefined} - */ -function extractMessages() { - map(glob('src/**/*.js?(x)'), (filename) => { - babel.transformFileSync(filename, {}, (err) => { - if (err) { - console.log(err); - } - }); - }); -} - -/** - * Get messages from separate JSON files - * @function getMessages - * @return {Object} Object with messages - */ -function getMessages() { - return reduce( - concat( - {}, - ...map( - // We ignore the existing customized shadowed components ones, since most - // probably we won't be overriding them - // If so, we should do it in the config object or somewhere else - glob('build/messages/src/**/*.json', { - ignore: 'build/messages/src/customizations/**', - }), - (filename) => - map(JSON.parse(fs.readFileSync(filename, 'utf8')), (message) => ({ - ...message, - filename: filename.match(/build\/messages\/src\/(.*).json$/)[1], - })), - ), - ), - (current, value) => { - let result = current; - if (current.id) { - result = { - [current.id]: { - defaultMessage: current.defaultMessage, - filenames: [current.filename], - }, - }; - } - - if (result[value.id]) { - result[value.id].filenames.push(value.filename); - } else { - result[value.id] = { - defaultMessage: value.defaultMessage, - filenames: [value.filename], - }; - } - return result; - }, - ); -} - -/** - * Convert messages to pot format - * @function messagesToPot - * @param {Object} messages Messages - * @return {string} Formatted pot string - */ -function messagesToPot(messages) { - return map(keys(messages).sort(), (key) => - [ - ...map(messages[key].filenames, (filename) => `#: ${filename}`), - `# defaultMessage: ${messages[key].defaultMessage}`, - `msgid "${key}"`, - 'msgstr ""', - ].join('\n'), - ).join('\n\n'); -} - -/** - * Pot header - * @function potHeader - * @return {string} Formatted pot header - */ -function potHeader() { - return `msgid "" - msgstr "" - "Project-Id-Version: Plone\\n" - "POT-Creation-Date: ${new Date().toISOString()}\\n" - "Last-Translator: Plone i18n \\n" - "Language-Team: Plone i18n \\n" - "MIME-Version: 1.0\\n" - "Content-Type: text/plain; charset=utf-8\\n" - "Content-Transfer-Encoding: 8bit\\n" - "Plural-Forms: nplurals=1; plural=0;\\n" - "Language-Code: en\\n" - "Language-Name: English\\n" - "Preferred-Encodings: utf-8\\n" - "Domain: volto\\n" - -`; -} - -/** - * Format header - * @function formatHeader - * @param {Array} comments Array of comments - * @param {Object} headers Object of header items - * @return {string} Formatted header - */ -function formatHeader(comments, headers) { - return [ - ...map(comments, (comment) => `# ${comment}`), - 'msgid ""', - 'msgstr ""', - ...map(keys(headers), (key) => `"${key}: ${headers[key]}\\n"`), - '', - ].join('\n'); -} - -/** - * Sync po by the pot file - * @function syncPoByPot - * @return {undefined} - */ -function syncPoByPot() { - const pot = Pofile.parse(fs.readFileSync('locales/volto.pot', 'utf8')); - - map(glob('locales/**/*.po'), (filename) => { - const po = Pofile.parse(fs.readFileSync(filename, 'utf8')); - - fs.writeFileSync( - filename, - `${formatHeader(po.comments, po.headers)} - ${map(pot.items, (item) => { - const poItem = find(po.items, { msgid: item.msgid }); - return [ - `${map(item.references, (ref) => `#: ${ref}`).join('\n')}`, - `msgid "${item.msgid}"`, - `msgstr "${poItem ? poItem.msgstr : ''}"`, - ].join('\n'); - }).join('\n\n')}\n`, - ); - }); -} - -// Main tasks -console.log('Extracting messages from source files...'); -extractMessages(); -console.log('Synchronizing messages to pot file...'); -// We only write the pot file if it's really different -const newPot = `${potHeader()}${messagesToPot(getMessages())}\n`.replace( - /"POT-Creation-Date:(.*)\\n"/, - '', -); -const oldPot = fs - .readFileSync('locales/volto.pot', 'utf8') - .replace(/"POT-Creation-Date:(.*)\\n"/, ''); - -if (newPot !== oldPot) { - fs.writeFileSync( - 'locales/volto.pot', - `${potHeader()}${messagesToPot(getMessages())}\n`, - ); -} -console.log('Synchronizing messages to po files...'); -syncPoByPot(); -console.log('done!');