globalizejs / globalize

A JavaScript library for internationalization and localization that leverages the official Unicode CLDR JSON data
https://globalizejs.com
MIT License
4.8k stars 603 forks source link

Singular / plural and other inflections? #76

Closed lapidus closed 9 years ago

lapidus commented 13 years ago

Is Globalize.localize already able to handle inflections or how would one devise such an extension?

//Pseudo example defining 3 forms of test

// Map of messages used by .localize()
    messages: {
       test : {
          _0 : "Zero tests",
          _1 : "%1s test",
          _1+ : "%1s tests"
       }    
    }
rdworth commented 13 years ago

No, this is (so far) out of scope. I'll leave the issue open to reflect that we may want to consider it as a feature in the future.

lukejagodzinski commented 11 years ago

It would be nice to have params and plural rules. It's easy to implement and here are the rules: http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html

Plural rules could be included into info object passed as an argument of the addCultureInfo function.

Could look like this:

numberFormat: {
    pluralRules: function (n) {
        var int,
            mod10,
            mod100;

        if (n === 1) {
            return 'one';
        }

        int    = n === n | 0;
        mod10  = n % 10;
        mod100 = n % 100;

        if (int && ((mod10 >= 2 && mod10 <= 4) && (mod100 < 12 || mod100 > 14))) {
            return 'few';
        }

        if (int && ((mod10 === 0 || mod10 === 1) || (mod10 >= 5 && mod10 <= 9 || mod100 >= 12 && mod100 <= 14))) {
            return 'many';
        }

        return 'other';
    }
},

And dictionary (messages) could look like this:

{
    'There are {{count}} events on': {
        'one':   'Istnieje {{count}} wydarzenie w dniu',
        'few':   'Istnieją {{count}} wydarzenia w dniu',
        'many':  'Istnieje {{count}} wydarzeń w dniu',
        'other': 'Istnieje {{count}} wydarzenia w dniu'
    },
    'Hello {{name}}': 'Cześć {{name}}',
    'My name is {{name}}': 'Mam na imię {{name}}'
}

Where {{name}} is only example of how params can be presented (tribute towards Handlebars and Mustaches). Example invocation of localize function could look like this:

Globalize.localize('There are {{count}} events on', {count: 15}, 'pl-PL');

If there is more than one param in the string, the plural rule number can be passed as special attribute, i.e.

Globalize.localize('There are {{count}} events on {{date}}', {count: 15, date: new Date(), _plurality: 15}, 'pl-PL');

I can contribute to Globalize library if you want, maybe I could do it in my work if my supervisors agree.

SlexAxton commented 11 years ago

Hi Łukasz,

So my messageformat.js library does more or less what you're proposing: https://github.com/SlexAxton/messageformat.js

The globalize team is working on getting all of those pluralization keywords and functions in from the CLDR library, but I wouldn't suggest rewriting an ICU MessageFormat parser unless you have some problems with the one I wrote. You could integrate these pretty easily, but the license is very open (moving to the dojo foundation under an Apache 2 license soon as well), so you're even welcome to push a feature to globalize that uses messageformat.js as a dependency.

For your examples, something like Globalize.localize = new Messageformat('pl-PL'); would pretty much do the trick.

It's a hard problem, and I think the ICU MessageFormat standard is ideal. The pluralization function you're mentioning are part of that spec, though it looks like you're pulling data from NumberFormat which also does similar things but is for formatting numbers. I have a port of Google's NumberFormat implementation in the Jed Toolkit repo here: https://github.com/jedtoolkit/numberformat.js

lukejagodzinski commented 11 years ago

Thanks for reply.

The library you wrote looks nice so I would use. But it would be good to have it integrated to Globalize, not as a dependency, so can I integrate it? I'm don't know what Dojo CLA license means in terms of modifying it and integrating into other library, so could you tell me if I'm able to do that?

You said that globalize team is working on this library but I don't see any changes for last 2 years, excluding some minor upgrades. Is it still alive?

scottgonzalez commented 11 years ago

Yes, the project is still alive. The original code was provided by Microsoft and based on the .NET locale data. We're in the process of changing to use CLDR as the data source and getting ready to actively work on the project.

rxaviers commented 10 years ago

This will be fixed by ICU Message Format #265

rxaviers commented 9 years ago

All those forms of plurals have been implemented.