🌐(i18n) create package i18n

The package i18n will handle the
internationalization of the applications.
It will parse the frontend code and extract
the translations to be send to the crowdin
platform.
This commit is contained in:
Anthony LC
2024-04-02 16:19:35 +02:00
committed by Anthony LC
parent 3098b9f4fc
commit dfafd335f4
11 changed files with 297 additions and 0 deletions

View File

@@ -0,0 +1,104 @@
import { execSync } from 'child_process';
import fs from 'fs';
import path from 'path';
describe('integration testing on i18n package', () => {
afterAll(() => {
fs.rmSync('./locales/tests', { recursive: true, force: true });
});
test('cmd extract-translation:impress', () => {
// To be sure the file is not here
fs.rmSync('./locales/impress/translations-crowdin.json', {
recursive: true,
force: true,
});
expect(
fs.existsSync('./locales/impress/translations-crowdin.json'),
).toBeFalsy();
// Generate the file
execSync('yarn extract-translation:impress');
expect(
fs.existsSync('./locales/impress/translations-crowdin.json'),
).toBeTruthy();
});
test('cmd format-deploy', () => {
// To be sure the tests folder is not here
fs.rmSync('./locales/tests', { recursive: true, force: true });
expect(fs.existsSync('./locales/tests')).toBeFalsy();
// Generate english json file
fs.mkdirSync('./locales/tests/en/', { recursive: true });
fs.writeFileSync(
'./locales/tests/en/translations.json',
JSON.stringify({ test: { message: 'My test' } }),
'utf8',
);
expect(fs.existsSync('./locales/tests/en/translations.json')).toBeTruthy();
fs.mkdirSync('./locales/tests/fr/', { recursive: true });
fs.writeFileSync(
'./locales/tests/fr/translations.json',
JSON.stringify({ test: { message: 'Mon test' } }),
'utf8',
);
expect(fs.existsSync('./locales/tests/fr/translations.json')).toBeTruthy();
// Execute format-deploy command
const output = './locales/tests/translations.json';
execSync(`node ./format-deploy.mjs --app=tests --output=${output}`);
const json = JSON.parse(fs.readFileSync(output, 'utf8'));
expect(json).toEqual({
en: {
translation: { test: 'My test' },
},
fr: {
translation: { test: 'Mon test' },
},
});
});
test('cmd format-deploy throws an error when translation file is not found', () => {
// To be sure the tests folder is not here
fs.rmSync('./locales/tests', { recursive: true, force: true });
expect(fs.existsSync('./locales/tests')).toBeFalsy();
// Generate english json file
fs.mkdirSync('./locales/tests/en/', { recursive: true });
// Execute format-deploy command
const output = './locales/tests/translations.json';
const cmd = () => {
execSync(`node ./format-deploy.mjs --app=tests --output=${output}`, {
stdio: 'pipe',
});
};
expect(cmd).toThrow(
`Error: File locales${path.sep}tests${path.sep}en${path.sep}translations.json not found!`,
);
});
test('cmd format-deploy throws an error when no translation to deploy', () => {
// To be sure the tests folder is not here
fs.rmSync('./locales/tests', { recursive: true, force: true });
expect(fs.existsSync('./locales/tests')).toBeFalsy();
// Generate english json file
fs.mkdirSync('./locales/tests/', { recursive: true });
// Execute format-deploy command
const output = './locales/tests/translations.json';
const cmd = () => {
execSync(`node ./format-deploy.mjs --app=tests --output=${output}`, {
stdio: 'pipe',
});
};
expect(cmd).toThrow('Error: No translation to deploy');
});
});

View File

@@ -0,0 +1,47 @@
import { execSync } from 'child_process';
import fs from 'fs';
describe('checks all the frontend translation are made', () => {
it('checks missing translation. If this test fails, go to https://crowdin.com/', () => {
// Extract the translations
execSync(
'yarn extract-translation:impress -c ./i18next-parser.config.jest.mjs',
);
const outputCrowdin = './locales/impress/translations-crowdin.json';
const jsonCrowdin = JSON.parse(fs.readFileSync(outputCrowdin, 'utf8'));
const listKeysCrowdin = Object.keys(jsonCrowdin).sort();
// Check the translations in the app impress
const outputimpress = '../../apps/impress/src/i18n/translations.json';
const jsonimpress = JSON.parse(fs.readFileSync(outputimpress, 'utf8'));
// Our keys are in english, so we don't need to check the english translation
Object.keys(jsonimpress)
.filter((key) => key !== 'en')
.forEach((key) => {
const listKeysimpress = Object.keys(jsonimpress[key].translation).sort();
const missingKeys = listKeysCrowdin.filter(
(element) => !listKeysimpress.includes(element),
);
const additionalKeys = listKeysimpress.filter(
(element) => !listKeysCrowdin.includes(element),
);
if (missingKeys.length > 0) {
console.log(
`Missing keys in impress translations that should be translated in Crowdin, got to https://crowdin.com/ :`,
missingKeys,
);
}
if (additionalKeys.length > 0) {
console.log(
`Additional keys in impress translations that seems not present in this branch:`,
additionalKeys,
);
}
expect(missingKeys.length).toBe(0);
});
});
});