Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow grammatically correct localisation of the RelativeTime plugin to fusional languages #304

Closed
wants to merge 13 commits into from

Conversation

prantlf
Copy link
Contributor

@prantlf prantlf commented Aug 16, 2018

  • Redefine the locale object structure to separate expressions for duration, future and past. Allow the proper declension of the noun coming after the numeral.
  • Automatically convert the old locale object structure to the new one during the first call to a method of the RelativeTime plugin.

Redefining the locale object structure from the old one:

  relativeTime: {
    future: 'in %s',
    past: '%s ago',
    s: 'a few seconds',
    ...
    yy: '%d years'
  }

to the new one solves the problem 1 from #302:

  relativeTime: {
    duration: {
      s: 'a few seconds',
      ...
      yy: '%d years'
    },
    future: {
      s: 'in a few seconds',
      ...
      yy: 'in %d years'
    },
    past: {
      s: 'a few seconds ago',
      ...
      yy: '%d years ago'
    }
  }

Adding second expressions with three-letter keys for numerals greater or equal to 5 solves the problem 2 from #302:

    duration: {
      s: 'a few seconds',
      m: 'a minute',
      mm: '%d minutes',
      mmm: '%d minutes',
      ...
    },

My Czech localisation is an example how to use the new locale structure to support grammatically correct formatting.

It is not grammatically correct. The current localization support in Day.js
is insufficient for fusional languages like Slavic ones.
* Redefine the locale object structure to separate expressions
  for duration, future and past. Allow the proper declension of
  the noun coming after the numeral.
* Automatically convert the old locale object structure to the
  new one during the first call to a method of the RelativeTime
  plugin.
@codecov-io
Copy link

codecov-io commented Aug 16, 2018

Codecov Report

Merging #304 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff           @@
##           master   #304    +/-   ##
======================================
  Coverage     100%   100%            
======================================
  Files          32     44    +12     
  Lines         386    488   +102     
  Branches       53     65    +12     
======================================
+ Hits          386    488   +102
Impacted Files Coverage Δ
src/locale/ru.js 100% <ø> (ø) ⬆️
src/plugin/relativeTime/index.js 100% <100%> (ø) ⬆️
src/locale/cs.js 100% <100%> (ø)
src/locale/es.js 100% <0%> (ø) ⬆️
src/locale/de.js 100% <0%> (ø) ⬆️
src/locale/fr.js 100% <0%> (ø) ⬆️
src/locale/it.js 100% <0%> (ø) ⬆️
src/locale/ja.js 100% <0%> (ø) ⬆️
src/index.js 100% <0%> (ø) ⬆️
... and 12 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b14bdd1...d587c77. Read the comment docs.

@prantlf prantlf changed the title Allow grammatically correct localisation to fusional languages Allow grammatically correct localisation of the RelativeTime plugin to fusional languages Aug 16, 2018
…d forms

* Add plural rules and forms from Mozilla localization concepts.
* Convert cs and ru locales to the new format.
@prantlf
Copy link
Contributor Author

prantlf commented Oct 14, 2018

I extended the localization format to cover all possible languages. The simple adding of a new string key was not enough. Even the common languages were not covered well with the three plural forms and the single rule for them, as @leovp pointed out.

Newly, the plural rule (a number or a function) has to be specified and the plural forms are specified as an array of strings, which the plural rule is an index to.

  relativeTime: {
    // Slavic (Slovak, Czech), 3 plural forms for 1, 2-4, 5-
    pluralRule: 8,
    duration: {
      s: 'několik sekund',
      m: 'minuta',
      mm: ['%d minuta', '%d minuty', '%d minut'],
      h: 'hodina',
      hh: ['%d hodina', '%d hodiny', '%d hodin'],
      d: 'den',
      dd: ['%d den', '%d dny', '%d dní'],
      M: 'měsíc',
      MM: ['%d měsíc', '%d měsíce', '%d měsícú'],
      y: 'rok',
      yy: ['%d rok', '%d roky', '%d let']
    },
    future: {
      s: 'za několik sekund',
      m: 'za minutu',
      mm: ['za %d minutu', 'za %d minuty', 'za %d minut'],
      h: 'za hodinu',
      hh: ['za %d hodinu', 'za %d hodiny', 'za %d hodin'],
      d: 'zítra',
      dd: ['za %d den', 'za %d dny', 'za %d dní'],
      M: 'za měsíc',
      MM: ['za %d měsíc', 'za %d měsíce', 'za %d měsícú'],
      y: 'za rok',
      yy: ['za %d rok', 'za %d roky', 'za %d let']
    },
    past: {
      s: 'před několika sekundami',
      m: 'před minutou',
      mm: ['před %d minutou', 'před %d minutami', 'před %d minutami'],
      h: 'před hodinou',
      hh: ['před %d hodinou', 'před %d hodinami', 'před %d hodinami'],
      d: 'včera',
      dd: ['před %d dnem', 'před %d dny', 'před %d dny'],
      M: 'před měsícem',
      MM: ['před %d měsícem', 'před %d měsíci', 'před %d měsíci'],
      y: 'vloni',
      yy: ['před %d rokem', 'před %d roky', 'před %d lety']
    }
  }

The plural rule can be either a number from src/plugin/relativeTime/pluralRules.js (an index in the array) or a function.

    pluralRule: 8
    pluralRule: n => n === 1 ? 0 : n >= 2 && n <= 4 ? 1 : 2

The plural forms are an array of strings, where the first one is for 1 item (singular, or 21, 31 and other numbers, depending on the language) and the others are supposed to match the index returned by the plural rule function. If there is only a single special case how to handle zero, this should be the last one (with the highest index). Singulars for the value 1 are special, because they usually have no number in the text.

I upgrade earlier locale format versions to retain the compatibility.

Number 1 is not formatted using the plural forms as usual. There is an additional text added for this case.
…by using the fast-plural-rules NPM module

* Remove the plurakl ule index from the localization object.
* Introduce a dependency on the fast-plural-rules NPM module.
@kmisachenka
Copy link

Hi! There is any progress for this issue?

@iamkun
Copy link
Owner

iamkun commented Oct 2, 2020

I think RelativeTime plugin has better support for fusional languages now.
I'll close this PR since it's been a while since it's been opened. Feel free to reopen if you have updates on this

@iamkun iamkun closed this Oct 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants