Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function extractValueReferencesFromMessage(message, messageId) {
// Skip validation if message doesn't use ICU.
if (!message.includes('{')) {
return [];
}
let messageAST;
try {
messageAST = 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 createFailError(
`Couldn't parse default message ("${messageId}"):\n${errorWithContext}`
);
}
throw error;
many {{var0, number} message}
other {{var0, number} messages}
}`;
formattedMessages.push(
);
}
ReactDOMServer.renderToString(
{formattedMessages}
);
});
const cachedAst = parse(
`{var0, plural,
zero {{var0, number} message}
one {{var0, number} message}
few {{var0, number} message}
many {{var0, number} message}
other {{var0, number} messages}
}`
);
suite.add(
'100 x with placeholder, cached in AST form',
function() {
let messages: Record = {};
let formattedMessages = [];
for (let i = 0, len = 100; i < len; i += 1) {
messages[i] = cachedAst;
formattedMessages.push(
config = {
locale: 'en',
messages: {
no_args: 'Hello, World!',
with_arg: 'Hello, {name}!',
with_named_format: 'It is {now, date, year-only}',
with_html: 'Hello, <b>{name}</b>!',
missing: undefined,
empty: '',
invalid: 'invalid {}',
missing_value: 'missing {arg_missing}',
missing_named_format: 'missing {now, date, format_missing}',
ast_simple: parse('hello world'),
ast_var: parse('hello there, {name}'),
},
formats: {
date: {
'year-only': {
year: 'numeric',
},
missing: undefined,
},
time: {
'hour-only': {
hour: '2-digit',
hour12: false,
},
missing: undefined,
translate(message) {
let ast = parse(message);
let translated = this.transform(ast);
return print(translated);
}
translate(message) {
const ast = parse(message);
const translated = this.transform(ast);
return print(translated);
}