How to use messageformat-parser - 7 common examples

To help you get started, we’ve selected a few 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 thingsboard / thingsboard / ui-ngx / src / app / core / translate / translate-default-compiler.ts View on Github external
private checkIsPlural(src: string): boolean {
    let tokens: any[];
    try {
      tokens = parse(src.replace(/\{\{/g, '{').replace(/\}\}/g, '}'),
        {cardinal: [], ordinal: []});
    } catch (e) {
      console.warn(`Failed to parse source: ${src}`);
      console.error(e);
    }
    const res = tokens.filter(
      (value) => typeof value !== 'string' && value.type === 'plural'
    );
    return res.length > 0;
  }
github lingui / js-lingui / packages / core / src / dev / compile.js View on Github external
export default function compile(message: string): Function | string {
  try {
    return processTokens(parse(message))
  } catch (e) {
    console.error(`Message cannot be parsed due to syntax errors: ${message}`)
    return message
  }
}
github lingui / js-lingui / packages / cli / src / api / compile.js View on Github external
export function compile(message: string) {
  const arg = t.identifier("a")

  let tokens
  try {
    tokens = parse(message)
  } catch (e) {
    throw new Error(
      `Can't parse message. Please check correct syntax: "${message}"`
    )
  }
  const ast = processTokens(tokens, arg)

  if (isString(ast)) return t.stringLiteral(ast)

  return t.functionExpression(
    null,
    [arg],
    t.blockStatement([t.returnStatement(ast)])
  )
}
github messageformat / messageformat / packages / messageformat / src / compiler.js View on Github external
const result = {};
      for (const key of Object.keys(src)) {
        const pl = plurals[key] || plural;
        result[key] = this.compile(src[key], pl, plurals);
      }
      return result;
    }

    this.plural = plural;
    const parserOptions = {
      cardinal: plural.cardinals,
      ordinal: plural.ordinals,
      strict: this.options.strictNumberSign
    };
    this.arguments = [];
    const r = parse(src, parserOptions).map(token => this.token(token));
    let reqArgs = '';
    if (this.options.requireAllArguments && this.arguments.length > 0) {
      this.setRuntimeFn('reqArgs');
      reqArgs = `reqArgs(${JSON.stringify(this.arguments)}, d); `;
    }
    return `function(d) { ${reqArgs}return ${this.concatenate(r, true)}; }`;
  }
github lingui / js-lingui / packages / lingui-i18n / src / utils.dev.js View on Github external
export const compileMessage = (message: string) => processTokens(parse(message))
github format-message / format-message / scripts / benchmark.src.js View on Github external
    'messageformat-parser': function () { return parse2(pattern) },
    'format-message-parse': function () { return parse(pattern) }
github TheThingsNetwork / lorawan-stack / .mage / xx.js View on Github external
module.exports = function (format) {
  return icu.parse(format).map(stringify).join('')
}

messageformat-parser

A PEG.js parser for ICU MessageFormat strings

MIT
Latest version published 5 years ago

Package Health Score

82 / 100
Full package analysis

Popular messageformat-parser functions