Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add infrastructure support for i18n and locales folder (#23) #25

Merged
merged 2 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ All notable changes to this project will be documented in this file. Dates are d

Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

#### [3.4.2](https://github.com/eea/volto-accordion-block/compare/3.4.1...3.4.2)

- Add infrastructure support for i18n and locales folder [`#23`](https://github.com/eea/volto-accordion-block/pull/23)

#### [3.4.1](https://github.com/eea/volto-accordion-block/compare/3.4.0...3.4.1)

> 10 September 2021

- Add missing onChangeField to make it work with Metadata section block [`#22`](https://github.com/eea/volto-accordion-block/pull/22)
- Add missing onChangeField to make it work with Metadata section block - refs #137434 [`5253b74`](https://github.com/eea/volto-accordion-block/commit/5253b74a52043a3ad7e2e813abaf2d27f0e83a7a)
- Add Sonarqube tag using ims-frontend addons list [`1e4d3dc`](https://github.com/eea/volto-accordion-block/commit/1e4d3dc937bc63825bd1fc6156eca9b6d073a40b)

Expand Down
1 change: 1 addition & 0 deletions babel.config.js-use-when-running-i18n
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@plone/volto/babel');
65 changes: 65 additions & 0 deletions locales/de/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
msgid ""
msgstr ""
"Project-Id-Version: Plone\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-04-27T19:30:59.079Z\n"
"PO-Revision-Date: 2016-10-22 16:41-0500\n"
"Last-Translator: German <plone-i18n@lists.sf.net>\n"
"Language: de\n"
"Language-Team: German <plone-i18n@lists.sf.net>\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=1; plural=0;\n"
"MIME-Version: 1.0\n"
"Language-Code: de\n"
"Language-Name: Deutsch\n"
"Preferred-Encodings: utf-8 latin1\n"
"X-Is-Fallback-For: de-at de-li de-lu de-ch de-de\n"

#: components/manage/Blocks/Accordion/Schema
msgid "Accordion"
msgstr "Akkordeon"

#: components/manage/Blocks/Accordion/Schema
msgid "Accordion Title size"
msgstr "Akkordeon Schriftgröße Titel"

#: components/manage/Blocks/Accordion/Schema
msgid "Allow multiple panels open at a time"
msgstr "Erlaube mehrere Panele zur gleichen Zeit geöffnet zu haben"

#: components/manage/Blocks/Accordion/Schema
msgid "Collapsed by default"
msgstr "Eingeklappt ist der Standard"

#: components/manage/Blocks/Accordion/Schema
msgid "Friendly name"
msgstr "Kurzname"

#: components/manage/Blocks/Accordion/Schema
msgid "Non exclusive"
msgstr "Nicht ausschließend"

#: components/manage/Blocks/Accordion/Schema
msgid "Style"
msgstr "Aussehen"

#: components/manage/Blocks/Accordion/Schema
msgid "Title"
msgstr "Titel"

#: components/manage/Blocks/Accordion/Schema
msgid "Title Icon on the right"
msgstr "Titel-Icon auf der rechten Seite anzeigen"

#: components/manage/Blocks/Accordion/Schema
msgid "Title size"
msgstr "Schriftgröße Titel"

#: components/manage/Blocks/Accordion/EditBlockWrapper
msgid "Unknown Block"
msgstr "Unbekannter Block"

#: components/manage/Blocks/Accordion/EditBlockWrapper
msgid "delete"
msgstr "löschen"
74 changes: 74 additions & 0 deletions locales/volto.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
msgid ""
msgstr ""
"Project-Id-Version: Plone\n"
"POT-Creation-Date: 2021-09-16T09:40:55.321Z\n"
"Last-Translator: Plone i18n <plone-i18n@lists.sourceforge.net>\n"
"Language-Team: Plone i18n <plone-i18n@lists.sourceforge.net>\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"

#: components/manage/Blocks/Accordion/Schema
# defaultMessage: Accordion
msgid "Accordion"
msgstr ""

#: components/manage/Blocks/Accordion/Schema
# defaultMessage: Accordion Title size
msgid "Accordion Title size"
msgstr ""

#: components/manage/Blocks/Accordion/Schema
# defaultMessage: Allow multiple panels open at a time
msgid "Allow multiple panels open at a time"
msgstr ""

#: components/manage/Blocks/Accordion/Schema
# defaultMessage: Collapsed by default
msgid "Collapsed by default"
msgstr ""

#: components/manage/Blocks/Accordion/Schema
# defaultMessage: Friendly name
msgid "Friendly name"
msgstr ""

#: components/manage/Blocks/Accordion/Schema
# defaultMessage: Non exclusive
msgid "Non exclusive"
msgstr ""

#: components/manage/Blocks/Accordion/Schema
# defaultMessage: Style
msgid "Style"
msgstr ""

#: components/manage/Blocks/Accordion/Schema
# defaultMessage: Title
msgid "Title"
msgstr ""

#: components/manage/Blocks/Accordion/Schema
# defaultMessage: Title Icon on the right
msgid "Title Icon on the right"
msgstr ""

#: components/manage/Blocks/Accordion/Schema
# defaultMessage: Title size
msgid "Title size"
msgstr ""

#: components/manage/Blocks/Accordion/EditBlockWrapper
# defaultMessage: Unknown Block {block}
msgid "Unknown Block"
msgstr ""

#: components/manage/Blocks/Accordion/EditBlockWrapper
# defaultMessage: delete
msgid "delete"
msgstr ""
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eeacms/volto-accordion-block",
"version": "3.4.1",
"version": "3.4.2",
"description": "volto-accordion-block: Volto accordion block",
"main": "src/index.js",
"author": "European Environment Agency: IDM2 A-Team",
Expand All @@ -26,6 +26,7 @@
"scripts": {
"release": "release-it",
"bootstrap": "npm install -g ejs; npm link ejs; node bootstrap",
"i18n": "rm -rf build/messages && NODE_ENV=production node src/i18n.js",
"test": "make test",
"test:fix": "make test-update",
"stylelint": "../../../node_modules/stylelint/bin/stylelint.js --allow-empty-input 'src/**/*.{css,less}'",
Expand Down
180 changes: 180 additions & 0 deletions src/i18n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/* 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 <plone-i18n@lists.sourceforge.net>\\n"
"Language-Team: Plone i18n <plone-i18n@lists.sourceforge.net>\\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!');