i18next / i18next-scanner

Scan your code, extract translation keys/values, and merge them into i18n resource files.
http://i18next.github.io/i18next-scanner
MIT License
567 stars 128 forks source link

Is it possible to pluralise the collected strings automatically from <Trans> components ? #96

Open HichemBenChaaben opened 6 years ago

HichemBenChaaben commented 6 years ago

Version

Configuration

const fs = require('fs');
const chalk = require('chalk');

module.exports = {
  options: {
    debug: true,
    removeUnusedKeys: true,
    func: {
      list: ['i18next.t', 'i18n.t', 't'],
      extensions: ['.js']
    },
    trans: {
      component: 'Trans',
      i18nKey: 'i18nKey',
      defaultsKey: 'defaults',
      extensions: ['.js'],
      fallbackKey: (ns, value) => value
    },
    lngs: ['en'],
    ns: ['translation'],
    defaultLng: 'en',
    defaultNs: 'translation',
    fallbackLng: 'en',
    defaultValue: '__STRING_NOT_TRANSLATED__',
    resource: {
      loadPath: 'src/i18n/{{lng}}/{{ns}}.json',
      savePath: 'src/i18n/{{lng}}/{{ns}}.json',
      jsonIndent: 2,
      lineEnding: '\n'
    },
    nsSeparator: false, // namespace separator
    keySeparator: false, // key separator
    interpolation: {
      prefix: '{{',
      suffix: '}}'
    }
  },
  transform: function customTransform(file, enc, done) {
    const parser = this.parser;
    const content = fs.readFileSync(file.path, enc);
    let count = 0;

    parser.parseFuncFromString(content, { list: ['i18next._', 'i18next.__'] }, (key, options) => {
      parser.set(key, Object.assign({}, options, {
        nsSeparator: false,
        keySeparator: false
      }));
      ++count;
    });
    if (count > 0) {
      console.log(`i18next-scanner: count=${chalk.cyan(count)}, file=${chalk.yellow(JSON.stringify(file.relative))}`);
    }
    done();
  }
};
<Trans count={count}>
    you have {{ count }}
</Trans>

and be collected as the following

  "unreadMessages": "you have <1>{{count}}</1> unread message.",
  "unreadMessages_plural": "you have <1>{{count}}</1> unread messages."
daliusd commented 5 years ago

IMHO it is really hard task. You can pluralize individual word (e.g. using https://www.npmjs.com/package/pluralize) but pluralizing sentence is really hard task. That's because you need to parse English text somehow and your algorithm should be "smart" enough to identify which word must be pluralized. E.g. in your example "unread" or "messages" must be pluralized? Yes, it is easy for human being but how computer should know which word must be pluralized.

I think this issue will not be fixed in the scope of this project.