Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
parent
7ed1fe05d7
commit
2212beba68
|
@ -21,7 +21,7 @@ import { mockMakeDirAsync, mockWriteFileAsync } from './integrate_locale_files.t
|
|||
|
||||
import path from 'path';
|
||||
import { integrateLocaleFiles, verifyMessages } from './integrate_locale_files';
|
||||
// @ts-ignore
|
||||
// @ts-expect-error
|
||||
import { normalizePath } from './utils';
|
||||
|
||||
const localePath = path.resolve(__dirname, '__fixtures__', 'integrate_locale_files', 'fr.json');
|
||||
|
@ -36,6 +36,7 @@ const defaultIntegrateOptions = {
|
|||
sourceFileName: localePath,
|
||||
dryRun: false,
|
||||
ignoreIncompatible: false,
|
||||
ignoreMalformed: false,
|
||||
ignoreMissing: false,
|
||||
ignoreUnused: false,
|
||||
config: {
|
||||
|
|
|
@ -31,7 +31,8 @@ import {
|
|||
normalizePath,
|
||||
readFileAsync,
|
||||
writeFileAsync,
|
||||
// @ts-ignore
|
||||
verifyICUMessage,
|
||||
// @ts-expect-error
|
||||
} from './utils';
|
||||
|
||||
import { I18nConfig } from './config';
|
||||
|
@ -41,6 +42,7 @@ export interface IntegrateOptions {
|
|||
sourceFileName: string;
|
||||
targetFileName?: string;
|
||||
dryRun: boolean;
|
||||
ignoreMalformed: boolean;
|
||||
ignoreIncompatible: boolean;
|
||||
ignoreUnused: boolean;
|
||||
ignoreMissing: boolean;
|
||||
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
@ -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,
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -36,6 +36,7 @@ run(
|
|||
async ({
|
||||
flags: {
|
||||
'ignore-incompatible': ignoreIncompatible,
|
||||
'ignore-malformed': ignoreMalformed,
|
||||
'ignore-missing': ignoreMissing,
|
||||
'ignore-unused': ignoreUnused,
|
||||
'include-config': includeConfig,
|
||||
|
@ -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.`
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -99,6 +101,7 @@ run(
|
|||
checkCompatibility(
|
||||
config,
|
||||
{
|
||||
ignoreMalformed: !!ignoreMalformed,
|
||||
ignoreIncompatible: !!ignoreIncompatible,
|
||||
ignoreUnused: !!ignoreUnused,
|
||||
ignoreMissing: !!ignoreMissing,
|
||||
|
|
|
@ -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,
|
||||
|
@ -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`
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -97,6 +99,7 @@ run(
|
|||
ignoreIncompatible,
|
||||
ignoreUnused,
|
||||
ignoreMissing,
|
||||
ignoreMalformed,
|
||||
config,
|
||||
log,
|
||||
});
|
||||
|
|
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue