How to use the intl-messageformat-parser.TYPE.literal function in intl-messageformat-parser

To help you get started, we’ve selected a few intl-messageformat-parser examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github formatjs / react-intl / src / format.ts View on Github external
// `id` is a required field of a Message Descriptor.
  invariant(id, '[React Intl] An `id` must be provided to format a message.');

  const message = messages && messages[id];
  const hasValues = Object.keys(values).length > 0;

  // Avoid expensive message formatting for simple messages without values. In
  // development messages will always be formatted in case of missing values.
  if (!hasValues && process.env.NODE_ENV === 'production') {
    const val = message || defaultMessage || id;
    if (typeof val === 'string') {
      return escapeUnformattedMessage(val);
    }
    invariant(
      val.length === 1 && val[0].type === TYPE.literal,
      'Message has placeholders but no values was provided'
    );
    return (val[0] as LiteralElement).value;
  }

  let formattedMessageParts: Array = [];

  if (message) {
    try {
      let formatter = state.getMessageFormat(message, locale, formats, {
        formatters: state,
      });

      formattedMessageParts = formatter.formatHTMLMessage(values);
    } catch (e) {
      onError(