fix(v2): fix bad theme pluralization rules for some labels (#4304)

* Pluralization test!

* Simplify usePluralForm usage with | plural message separator

* fix interpolate bug with falsy values like 0

* fix interpolate bug with falsy values like 0

* Order plural forms + allow to not provide the last plural forms if they are not used

* fix typo

* revert test!

* plurals and typo of the SearchPage

* update some labels

* improve the update-code-translations cli + update translations

* pluralize blog reading time label

* ensure base.json contains message descriptions: helps the user to provide the translations

* remove russian production locale
This commit is contained in:
Sébastien Lorber 2021-03-03 17:05:21 +01:00 committed by GitHub
parent 6c73f51f94
commit 364d4dbf01
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 358 additions and 75 deletions

View file

@ -11,6 +11,16 @@ const fs = require('fs-extra');
const globby = require('globby');
const {mapValues, difference} = require('lodash');
const CodeDirPaths = [
path.join(__dirname, 'lib-next'),
// TODO other themes should rather define their own translations in the future?
path.join(__dirname, '..', 'docusaurus-theme-search-algolia', 'src', 'theme'),
path.join(__dirname, '..', 'docusaurus-theme-live-codeblock', 'src', 'theme'),
path.join(__dirname, '..', 'docusaurus-plugin-pwa', 'src', 'theme'),
];
console.log('Will scan folders for code translations:', CodeDirPaths);
function sortObjectKeys(obj) {
const keys = Object.keys(obj);
keys.sort();
@ -38,9 +48,8 @@ async function extractThemeCodeMessages() {
extractAllSourceCodeFileTranslations,
} = require('@docusaurus/core/lib/server/translations/translationsExtractor');
const codeDirPaths = [path.join(__dirname, 'lib-next')];
const filePaths = (
await globSourceCodeFilePaths(codeDirPaths)
await globSourceCodeFilePaths(CodeDirPaths)
).filter((filePath) => ['.js', '.jsx'].includes(path.extname(filePath)));
const filesExtractedTranslations = await extractAllSourceCodeFileTranslations(
@ -63,12 +72,7 @@ async function extractThemeCodeMessages() {
{},
);
const translationMessages = mapValues(
translations,
(translation) => translation.message,
);
return translationMessages;
return translations;
}
async function readMessagesFile(filePath) {
@ -77,7 +81,9 @@ async function readMessagesFile(filePath) {
async function writeMessagesFile(filePath, messages) {
const sortedMessages = sortObjectKeys(messages);
await fs.writeFile(filePath, JSON.stringify(sortedMessages, null, 2));
const content = `${JSON.stringify(sortedMessages, null, 2)}\n`; // \n makes prettier happy
await fs.writeFile(filePath, content);
console.log(
`${path.basename(filePath)} updated (${
Object.keys(sortedMessages).length
@ -98,7 +104,11 @@ async function getCodeTranslationFiles() {
async function updateBaseFile(baseFile) {
const baseMessages = await readMessagesFile(baseFile);
const codeMessages = await extractThemeCodeMessages();
const codeExtractedTranslations = await extractThemeCodeMessages();
const codeMessages = mapValues(
codeExtractedTranslations,
(translation) => translation.message,
);
const unknownMessages = difference(
Object.keys(baseMessages),
@ -118,7 +128,22 @@ ${logKeys(unknownMessages)}`),
...codeMessages,
};
await writeMessagesFile(baseFile, newBaseMessages);
const newBaseMessagesDescriptions = Object.entries(newBaseMessages).reduce(
(acc, [key]) => {
return {
...acc,
[`${key}___DESCRIPTION`]: codeExtractedTranslations[key].description,
};
},
{},
);
const newBaseMessagesWitDescription = {
...newBaseMessages,
...newBaseMessagesDescriptions,
};
await writeMessagesFile(baseFile, newBaseMessagesWitDescription);
return newBaseMessages;
}