i18next-scanner icon indicating copy to clipboard operation
i18next-scanner copied to clipboard

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

Open HichemBenChaaben opened this issue 7 years ago • 1 comments

Version

  • i18next: 11.5.0
  • i18next-scanner: 2.6.5

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."

HichemBenChaaben avatar Aug 07 '18 13:08 HichemBenChaaben

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.

daliusd avatar Nov 15 '18 11:11 daliusd