Coder Social home page Coder Social logo

Plural rules definition about gettext HOT 13 CLOSED

php-gettext avatar php-gettext commented on May 18, 2024
Plural rules definition

from gettext.

Comments (13)

oscarotero avatar oscarotero commented on May 18, 2024

That's great. No problem, I can wait.

from gettext.

mlocati avatar mlocati commented on May 18, 2024

Done. See https://github.com/mlocati/cldr-to-gettext-plural-rules/

from gettext.

oscarotero avatar oscarotero commented on May 18, 2024

Nice!
I think this data should be saved in an external php file, so it can be re-generated in any time without modify the Locales class. What do you think about implementing something like this?:

php convert.php phparray locales-data.php

So, in Locales class, we can create a method to retrieve this information:

public static function getData()
{
    if (!isset(self::$data)) {
        self::$data = include __DIR__.'/locales-data.php';
    }

    return self::$data;
}

from gettext.

mlocati avatar mlocati commented on May 18, 2024

Nice idea. Done 😉

from gettext.

oscarotero avatar oscarotero commented on May 18, 2024

Ok, I've changed the Locales class to work with the new source. There are some changes, for example in the russian language that now has 4 plurals instead 3, I hope this is right (I have no idea).
I don't know whether the territories data is useful, but I've keept it anyway.

from gettext.

mlocati avatar mlocati commented on May 18, 2024

The russian language that now has 4 plurals instead 3, I hope this is right (I have no idea)

Previously we had 3 plurals with this formula:
(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)

But the CLDR data says this:

"ru": {
    "pluralRule-count-one": "v = 0 and i % 10 = 1 and i % 100 != 11 @integer 1, 21, 31, 41, 51, 61, 71, 81, 101, 1001, …",
    "pluralRule-count-few": "v = 0 and i % 10 = 2..4 and i % 100 != 12..14 @integer 2~4, 22~24, 32~34, 42~44, 52~54, 62, 102, 1002, …",
    "pluralRule-count-many": "v = 0 and i % 10 = 0 or v = 0 and i % 10 = 5..9 or v = 0 and i % 100 = 11..14 @integer 0, 5~19, 100, 1000, 10000, 100000, 1000000, …",
    "pluralRule-count-other": "   @decimal 0.0~1.5, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, …"
},

As you can see, there should be 4 plurals.
And the new formula is

(n % 10 == 1 && n % 100 != 11) ? 0
:
((n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 12 || n % 100 > 14)) ? 1
:
((n % 10 == 0 || n % 10 >= 5 && n % 10 <= 9 || n % 100 >= 11 && n % 100 <= 14) ?
2
:
3
))

Which seems congruent with the CLDR data (for gettext we have that n == i and v == 0)

BUT the fourth case never occurs for integers... So, yes: it seems there's still some optimizations to be done...

from gettext.

mlocati avatar mlocati commented on May 18, 2024

There are 4 cases that I have to investigate.

You have eagle eyes ;) Thanks!

from gettext.

mlocati avatar mlocati commented on May 18, 2024

@oscarotero Ok, I added a procedure that removes the superfluous cases; the languages cleaned have been the 4 problematic ones: be- Belarusian,pl- Polish,ru- Russian anduk` - Ukrainian.

from gettext.

mlocati avatar mlocati commented on May 18, 2024

Just a side note: Daiki Ueno (head developer of the gettext tools) seems interested in my cldr-to-gettext-plural-rules 😉

from gettext.

oscarotero avatar oscarotero commented on May 18, 2024

That's fantastic!!
I've pushed some changes with the new data exported by your tool and everything works fine.

You have eagle eyes ;) Thanks!

The phpunit test has eagle eyes 👓

from gettext.

mlocati avatar mlocati commented on May 18, 2024

@oscarotero Just a notice: https://packagist.org/packages/gettext/languages

from gettext.

oscarotero avatar oscarotero commented on May 18, 2024

Ok.
Do you think it's better to include this package as a dependency and generate the data using the composer post-install event?

from gettext.

mlocati avatar mlocati commented on May 18, 2024

If you include my package, you can also use the Language class without using pre-generated php code...

from gettext.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.