airbnb / polyglot.js

Give your JavaScript the ability to speak many languages.
http://airbnb.github.io/polyglot.js
BSD 2-Clause "Simplified" License
3.71k stars 208 forks source link

Move plural types to single npm package #105

Open likerRr opened 6 years ago

likerRr commented 6 years ago

What do you think of moving that part of code to a single package?

Reasons:

  1. Anyone can take it as a starting point for building whatever pluralization library
  2. It gives an ability to support locales and their pluralization logic separately from the core
  3. If you check current pull requests you can see that most of them is about locales and not about the core mechanics
ljharb commented 6 years ago

That's not a bad idea. The only thing that's used inside the rest of polyglot is pluralTypeIndex, but I'm not sure how generic that is (or how useful it would be to other libs), since it deals with the "smart_count" stuff.

likerRr commented 6 years ago

I think it's generic enough in what it does. I see it as a module, which exports pluralTypeIndex function as default. So anyone will be able to build very basic pluralization implementation, e.g:

import getPluralTypeIndex from 'plural-type-index';

const phrases = {
    cars: ['car', 'cars'], // plural forms
};

const pluralize = (locale, phrase, count) =>
    phrase[getPluralTypeIndex(locale, count)];

pluralize('en', phrases.cars, 1); // car
pluralize('en', phrases.cars, 2); // cars

So the purpose of moving code to separate lib is to make it does one thing - get pluralization index

WORMSS commented 4 years ago

One thing I would like to add to this if it goes into its own package, The ability to query how many sections needed/recommended for each locale.

I am going to bake it into my application directly for now, eg

{
  'arabic': 6
  'bosnian_serbian': 3,
  'chinese': 1,
  ....
}

you get the idea.