Vera
Vera

Reputation: 51

How to run formatjs compile?

How to run formatjs compile command without replacement of already translated messages? Now I run it this way:

../../node_modules/.bin/formatjs compile lang/ru.json --out-file compiled-lang/ru.json

Upvotes: 0

Views: 1067

Answers (1)

xiongemi
xiongemi

Reputation: 209

You need to have a custom formatter with your own compile function: https://formatjs.io/docs/tooling/cli/#custom-formatters

This is what my formatter file looks like:

const argv = require('yargs').argv;
const originTranslations = require(argv['out-file']);

module.exports = {
  compile: function (msgs) {
    const results = {};
    for (const k in msgs) {
      const defaultMessage = msgs[k].defaultMessage;
      if (originTranslations[k]) {
        results[k] = originTranslations[k];
      } else if (defaultMessage) {
        results[k] = defaultMessage;
      } else {
        results[k] = 'MISSING TRANSLATION';
      }
    }
    return results;
  },
};

You need to pass in this formatter with --format option

yarn formatjs compile lang/ru.json --out-file compiled-lang/ru.json --format <your formatter file>

Upvotes: 0

Related Questions