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

Integrate main i18n tool into build pipeline #22254

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 11 additions & 5 deletions src/dev/run_extract_default_translations.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@
* under the License.
*/

import chalk from 'chalk';
import { run } from './run';
import { extractDefaultTranslations } from './i18n/extract_default_translations';

run(async ({ flags: { path, output, 'output-format': outputFormat } }) => {
await extractDefaultTranslations({
paths: Array.isArray(path) ? path : [path || './'],
output,
outputFormat,
});
try {
await extractDefaultTranslations({
paths: Array.isArray(path) ? path : [path || './'],
output,
outputFormat,
});
} catch (e) {
console.error(`${chalk.white.bgRed(' I18N ERROR ')} ${e.message || e}`);
process.exit(1);
}
});
2 changes: 1 addition & 1 deletion src/ui/ui_render/bootstrap/template.js.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ window.onload = function () {
err.style['text-align'] = 'center';
err.style['background'] = '#F44336';
err.style['padding'] = '25px';
err.innerText = '{{i18n 'UI-WELCOME_ERROR' '{"defaultMessage": "Kibana did not load properly. Check the server output for more information."}'}}';
err.innerText = '{{i18n 'common.ui.welcomeError' '{"defaultMessage": "Kibana did not load properly. Check the server output for more information."}'}}';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: please update ids for all existing UI-WELCOME_ messages we have in Kibana (in knb-plugin-helpers tests) and remove src/ui/translations entirely for now.


document.body.innerHTML = err.outerHTML;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ui/ui_render/views/ui_app.pug
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,6 @@ block content
.kibanaWelcomeLogoCircle
.kibanaWelcomeLogo
.kibanaWelcomeText
| #{i18n('UI-WELCOME_MESSAGE', { defaultMessage: 'Loading Kibana' })}
| #{i18n('common.ui.welcomeMessage', { defaultMessage: 'Loading Kibana' })}

script(src=bootstrapScriptUrl)
99 changes: 0 additions & 99 deletions tasks/utils/i18n_verify_keys.js

This file was deleted.

86 changes: 21 additions & 65 deletions tasks/verify_translations.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,74 +17,30 @@
* under the License.
*/

// TODO: Integrate a new tool for translations checking
// https://github.com/elastic/kibana/pull/19826
import { i18nLoader } from '@kbn/i18n';
const { resolve } = require('path');

import { toArray } from 'rxjs/operators';
import { fromRoot, formatListAsProse } from '../src/utils';
import { findPluginSpecs } from '../src/plugin_discovery';
import { collectUiExports } from '../src/ui';

import * as i18nVerify from './utils/i18n_verify_keys';

export default function (grunt) {
grunt.registerTask('verifyTranslations', async function () {
module.exports = function (grunt) {
grunt.registerTask('verifyTranslations', function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: hmm, can you please make run:i18nCheck task exactly like run:typeCheck (define in config/run.js, run it right after 'run:typeCheck' in jenkins:unit and test grunt tasks, with !grunt.option('quick') for the latter task)?

And let's rename scripts/extract_default_translations.js to scripts/i18n_check for now and later on we'll figure out whether to keep this name for extraction purpose or not.

const done = this.async();

try {
const { spec$ } = findPluginSpecs({
env: 'production',
plugins: {
scanDirs: [fromRoot('src/core_plugins')]
const serverCmd = {
cmd: 'node',
args: [resolve(__dirname, '../scripts/extract_default_translations')],
opts: { stdio: 'inherit' },
};

new Promise((resolve, reject) => {
grunt.util.spawn(serverCmd, (error, result, code) => {
if (error || code !== 0) {
const error = new Error(`verifyTranslations exited with code ${code}`);
grunt.fail.fatal(error);
reject(error);
return;
}
});

const specs = await spec$.pipe(toArray()).toPromise();
const uiExports = collectUiExports(specs);
await verifyTranslations(uiExports);

done();
} catch (error) {
done(error);
}
grunt.log.writeln(result);
resolve();
});
}).then(done, done);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is likely more obsolete, I would use:

.then(done)
.catch(done)

});

}

async function verifyTranslations(uiExports) {
const keysUsedInViews = [];

// Search files for used translation keys
const translationPatterns = [
{ regexp: 'i18n\\(\'(.*)\'\\)',
parsePaths: [fromRoot('src/ui/ui_render/views/*.pug')] }
];
for (const { regexp, parsePaths } of translationPatterns) {
const keys = await i18nVerify.getTranslationKeys(regexp, parsePaths);
for (const key of keys) {
keysUsedInViews.push(key);
}
}

// get all of the translations from uiExports
const translations = await i18nLoader.getAllTranslationsFromPaths(uiExports.translationPaths);
const keysWithoutTranslations = Object.entries(
i18nVerify.getNonTranslatedKeys(keysUsedInViews, translations)
);

if (!keysWithoutTranslations.length) {
return;
}

throw new Error(
'\n' +
'\n' +
'The following keys are used in angular/pug views but are not translated:\n' +
keysWithoutTranslations.map(([locale, keys]) => (
` - ${locale}: ${formatListAsProse(keys)}`
)).join('\n') +
'\n' +
'\n'
);
}
};