Skip to content

Commit

Permalink
[7.8] [i18n] integrate new translations + new i18n check elastic#70193 (
Browse files Browse the repository at this point in the history
elastic#70423) (elastic#71389)

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
Bamieh and elasticmachine authored Jul 11, 2020
1 parent e288b03 commit 98a855c
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 63 deletions.
1 change: 1 addition & 0 deletions src/dev/i18n/integrate_locale_files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const defaultIntegrateOptions = {
sourceFileName: localePath,
dryRun: false,
ignoreIncompatible: false,
ignoreMalformed: false,
ignoreMissing: false,
ignoreUnused: false,
config: {
Expand Down
19 changes: 19 additions & 0 deletions src/dev/i18n/integrate_locale_files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
normalizePath,
readFileAsync,
writeFileAsync,
verifyICUMessage,
// @ts-ignore
} from './utils';

Expand All @@ -41,6 +42,7 @@ export interface IntegrateOptions {
sourceFileName: string;
targetFileName?: string;
dryRun: boolean;
ignoreMalformed: boolean;
ignoreIncompatible: boolean;
ignoreUnused: boolean;
ignoreMissing: boolean;
Expand Down Expand Up @@ -105,6 +107,23 @@ export function verifyMessages(
}
}

for (const messageId of localizedMessagesIds) {
const defaultMessage = defaultMessagesMap.get(messageId);
if (defaultMessage) {
try {
const message = localizedMessagesMap.get(messageId)!;
verifyICUMessage(message);
} catch (err) {
if (options.ignoreMalformed) {
localizedMessagesMap.delete(messageId);
options.log.warning(`Malformed translation ignored (${messageId}): ${err}`);
} else {
errorMessage += `\nMalformed translation (${messageId}): ${err}\n`;
}
}
}
}

if (errorMessage) {
throw createFailError(errorMessage);
}
Expand Down
4 changes: 3 additions & 1 deletion src/dev/i18n/tasks/check_compatibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ import { integrateLocaleFiles, I18nConfig } from '..';

export interface I18nFlags {
fix: boolean;
ignoreMalformed: boolean;
ignoreIncompatible: boolean;
ignoreUnused: boolean;
ignoreMissing: boolean;
}

export function checkCompatibility(config: I18nConfig, flags: I18nFlags, log: ToolingLog) {
const { fix, ignoreIncompatible, ignoreUnused, ignoreMissing } = flags;
const { fix, ignoreIncompatible, ignoreUnused, ignoreMalformed, ignoreMissing } = flags;
return config.translations.map((translationsPath) => ({
task: async ({ messages }: { messages: Map<string, { message: string }> }) => {
// If `fix` is set we should try apply all possible fixes and override translations file.
Expand All @@ -37,6 +38,7 @@ export function checkCompatibility(config: I18nConfig, flags: I18nFlags, log: To
ignoreIncompatible: fix || ignoreIncompatible,
ignoreUnused: fix || ignoreUnused,
ignoreMissing: fix || ignoreMissing,
ignoreMalformed: fix || ignoreMalformed,
sourceFileName: translationsPath,
targetFileName: fix ? translationsPath : undefined,
config,
Expand Down
22 changes: 22 additions & 0 deletions src/dev/i18n/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,28 @@ export function checkValuesProperty(prefixedValuesKeys, defaultMessage, messageI
}
}

/**
* Verifies valid ICU message.
* @param message ICU message.
* @param messageId ICU message id
* @returns {undefined}
*/
export function verifyICUMessage(message) {
try {
parser.parse(message);
} catch (error) {
if (error.name === 'SyntaxError') {
const errorWithContext = createParserErrorMessage(message, {
loc: {
line: error.location.start.line,
column: error.location.start.column - 1,
},
message: error.message,
});
throw errorWithContext;
}
}
}
/**
* Extracts value references from the ICU message.
* @param message ICU message.
Expand Down
5 changes: 4 additions & 1 deletion src/dev/run_i18n_check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ run(
async ({
flags: {
'ignore-incompatible': ignoreIncompatible,
'ignore-malformed': ignoreMalformed,
'ignore-missing': ignoreMissing,
'ignore-unused': ignoreUnused,
'include-config': includeConfig,
Expand All @@ -48,12 +49,13 @@ run(
fix &&
(ignoreIncompatible !== undefined ||
ignoreUnused !== undefined ||
ignoreMalformed !== undefined ||
ignoreMissing !== undefined)
) {
throw createFailError(
`${chalk.white.bgRed(
' I18N ERROR '
)} none of the --ignore-incompatible, --ignore-unused or --ignore-missing is allowed when --fix is set.`
)} none of the --ignore-incompatible, --ignore-malformed, --ignore-unused or --ignore-missing is allowed when --fix is set.`
);
}

Expand Down Expand Up @@ -99,6 +101,7 @@ run(
checkCompatibility(
config,
{
ignoreMalformed: !!ignoreMalformed,
ignoreIncompatible: !!ignoreIncompatible,
ignoreUnused: !!ignoreUnused,
ignoreMissing: !!ignoreMissing,
Expand Down
5 changes: 4 additions & 1 deletion src/dev/run_i18n_integrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ run(
'ignore-incompatible': ignoreIncompatible = false,
'ignore-missing': ignoreMissing = false,
'ignore-unused': ignoreUnused = false,
'ignore-malformed': ignoreMalformed = false,
'include-config': includeConfig,
path,
source,
Expand Down Expand Up @@ -66,12 +67,13 @@ run(
typeof ignoreIncompatible !== 'boolean' ||
typeof ignoreUnused !== 'boolean' ||
typeof ignoreMissing !== 'boolean' ||
typeof ignoreMalformed !== 'boolean' ||
typeof dryRun !== 'boolean'
) {
throw createFailError(
`${chalk.white.bgRed(
' I18N ERROR '
)} --ignore-incompatible, --ignore-unused, --ignore-missing, and --dry-run can't have values`
)} --ignore-incompatible, --ignore-unused, --ignore-malformed, --ignore-missing, and --dry-run can't have values`
);
}

Expand All @@ -97,6 +99,7 @@ run(
ignoreIncompatible,
ignoreUnused,
ignoreMissing,
ignoreMalformed,
config,
log,
});
Expand Down
Loading

0 comments on commit 98a855c

Please sign in to comment.