Skip to content

Commit

Permalink
Merge branch 'fusional-languages' into combined
Browse files Browse the repository at this point in the history
  • Loading branch information
prantlf committed Oct 28, 2018
2 parents cf2fa54 + 6253bcd commit c3a6822
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 249 deletions.
3 changes: 2 additions & 1 deletion build/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = (config) => {
input: {
input,
external: [
'dayjs', 'timezone-support'
'dayjs', 'fast-plural-rules', 'timezone-support'
],
plugins: [
babel({
Expand All @@ -22,6 +22,7 @@ module.exports = (config) => {
name: name || 'dayjs',
globals: {
dayjs: 'dayjs',
'fast-plural-rules': 'fastPluralRules',
'timezone-support': 'timezone-support'
},
sourcemap: true
Expand Down
7 changes: 3 additions & 4 deletions docs/en/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,10 @@ Old template of the part of a Day.js locale Object for the RelativeTime plugin.
}
```

New template of the part of a Day.js locale Object for the RelativeTime plugin. It works well for fusional languages which decline nouns and which have two forms of plural. Slavic languages like Czech language, for example. The `duration` expressions will be used if the `withoutSuffix` parameter is set to `true` in the method calls.
New template of the part of a Day.js locale Object for the RelativeTime plugin. It works well for fusional languages which decline nouns and which have multiple plural forms. Slavic languages like Czech language, for example. The `duration` expressions will be used if the `withoutSuffix` parameter is set to `true` in the method calls.
```javascript
relativeTime: { // relative time format strings, keep %d as the same
// Plural rule for 3 plural forms in Slavic languages
pluralRule: 8,
// Using 3 plural forms in Slavic languages
duration: {
// Static message, just one singular/plural form needed
s: 'několik sekund',
Expand Down Expand Up @@ -165,7 +164,7 @@ New template of the part of a Day.js locale Object for the RelativeTime plugin.
}
}
```
The plural rule is an index of a function in the array in `src/plugins/relativeTime/pluralRules.js`, or an actual function. The function gets a number and return an index of the plural form to use. The keys with two-letter time units point to arrays with plural forms. The keys with single-letter time units point to a string shown for a single value, usually without any number.
The keys with single-letter time units point to a string shown for a single value, usually without any number. The keys with two-letter time units point to arrays with plural forms. Before you work on a new localization, make yourself familiar with [plural rules and plural forms for the target language](https://github.com/prantlf/fast-plural-rules/blob/master/docs/languages.md#supported-languages). You will find more information about [plural rules](https://github.com/prantlf/fast-plural-rules/blob/master/docs/design.md#plural-rules) and [plural forms](https://github.com/prantlf/fast-plural-rules/blob/master/docs/design.md#plural-forms) in the [design](https://github.com/prantlf/fast-plural-rules/blob/master/docs/design.md#design-concepts) of the library [fast-plural-rules](https://github.com/prantlf/fast-plural-rules#fast-plural-rules) used for the grammatically correct localization of expressions with cardinal numbers.

Template of a Day.js locale file.
```javascript
Expand Down
10 changes: 10 additions & 0 deletions docs/en/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ Returns the `string` of relative time to X.
| 11 months to 17months | y | a year ago |
| 18 months+ | yy | 2 years ago ... 20 years ago |

#### Installation

This plugin has a dependency on the [`fast-plural-rules`](https://www.npmjs.com/package/fast-plural-rules) NPM module. If you are going to use it on a web page directly, add its script to your section of `<script>`s too, along with the `Day.js`, for example:

```html
<script arc="https://unpkg.com/dayjs-ext/dayjs.min.js"></script>
<script arc="https://unpkg.com/dayjs-ext/plugin/relativeTime.js"></script>
<script arc="https://unpkg.com/fast-plural-rules/dist/index.umd.js"></script>
```

### IsLeapYear
- IsLeapYear adds `.isLeapYear` API to returns a `boolean` indicating whether the `Dayjs`'s year is a leap year or not.

Expand Down
9 changes: 7 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
"url": "https://github.com/prantlf/dayjs/issues"
},
"dependencies": {
"fast-plural-rules": "^0.0.1",
"timezone-support": "^1.5.5"
},
"devDependencies": {
Expand Down
3 changes: 1 addition & 2 deletions src/locale/cs.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ const locale = {
LLLL: 'dddd D. MMMM YYYY H:mm'
},
relativeTime: {
// 3 plural forms for 1, 2-4, 5-
pluralRule: 8,
// Using 3 plural forms for 1, 2-4, 5-
duration: {
s: 'několik sekund',
m: 'minuta',
Expand Down
3 changes: 1 addition & 2 deletions src/locale/ru.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ const locale = {
LLLL: 'dddd, D MMMM YYYY г., H:mm'
},
relativeTime: {
// 3 plural forms for 1 and x1, 2-4 and x2-4, 5-
pluralRule: 7,
// Using 3 plural forms for 1 and x1, 2-4 and x2-4, 5-
duration: {
s: 'несколько секунд',
m: 'минута',
Expand Down
3 changes: 1 addition & 2 deletions src/locale/sk.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ const locale = {
LLLL: 'dddd D. MMMM YYYY H:mm'
},
relativeTime: {
// 3 plural forms for 1, 2-4, 5-
pluralRule: 8,
// Using 3 plural forms for 1, 2-4, 5-
duration: {
s: 'niekoľko sekúnd',
m: 'minúta',
Expand Down
3 changes: 1 addition & 2 deletions src/locale/ua.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ const locale = {
LLLL: 'dddd, D MMMM YYYY р., H:mm'
},
relativeTime: {
// 3 plural forms for 1 and x1, 2-4 and x2-4, 5-
pluralRule: 7,
// Using 3 plural forms for 1 and x1, 2-4 and x2-4, 5-
duration: {
s: 'кілька секунд',
m: 'хвилина',
Expand Down
46 changes: 24 additions & 22 deletions src/plugin/relativeTime/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import * as C from '../../constant'
import pluralRules from './pluralRules'
import { getPluralFormForCardinalByLocale } from 'fast-plural-rules'

// Special plural rules for upgraded locales, which are not complete
// Returns 0 for plural for languages with a single plural
const simplePluralRule = () => 0
// Returns 0 for plural for 2 <= value <= 4 and 1 for plural
// for value >= 5, which is sufficient for some languages like Czech
/* istanbul ignore next line */
const improvedPluralRule = n => n >= 2 && n <= 4 ? 0 : 1 // eslint-disable-line no-confusing-arrow
import * as C from '../../constant'

export default (o, c, d) => {
const proto = c.prototype
Expand Down Expand Up @@ -42,7 +35,23 @@ export default (o, c, d) => {
// Save the special singular without any number with the single-letter key and the
// single plural to be used with any number greater then 1 with the two-letter key
const text = loc[key]
result[key] = kl === 1 ? text : [text]
if (kl === 1) {
result[key] = text
// Insert singular for objects with plurals declared before singulars
const key2 = key + key
let plurals = result[key2]
if (!plurals) {
plurals = result[key2] = [] // eslint-disable-line no-multi-assign
}
plurals.unshift(text)
} else {
// Append plural for objects with plurals declared after singulars
let plurals = result[key]
if (!plurals) {
plurals = result[key] = [] // eslint-disable-line no-multi-assign
}
plurals.push(text)
}
}
// Remove the original locale entry; the original locale object needs
// to be retained to prevent upgrading on every formatting call
Expand All @@ -68,8 +77,6 @@ export default (o, c, d) => {
loc.duration = durations
loc.future = futures
loc.past = pasts
// Recognize only one plural like in English
loc.pluralRule = simplePluralRule
}
// Upgrade the improved, but not the final version of the localization,
// which supports two plurals by keys with two and three letters
Expand Down Expand Up @@ -102,14 +109,12 @@ export default (o, c, d) => {
loc.duration = convertPlurals(loc.duration)
loc.future = convertPlurals(loc.future)
loc.past = convertPlurals(loc.past)
// Recognize two plurals like in the Czech language
loc.pluralRule = improvedPluralRule
}
// Upgrades old locale format to provide compatibility with older
// localizations; the grammar may not be correct for fusional languages
// {
// duration: { s: '...', m: '...', mm: ['...', '...', ...] },
// future: { ... }, past: { ... }, pluralRule: N
// future: { ... }, past: { ... }
// }
function upgradeLocale(loc) {
// Do not upgrade already upgraded locales
Expand All @@ -120,7 +125,8 @@ export default (o, c, d) => {
}
}
const fromTo = (input, withoutSuffix, instance, isFrom) => {
const locs = instance.$locale().relativeTime
const locale = instance.$locale()
const locs = locale.relativeTime
const T = [
{ l: 's', r: 44, d: C.S },
{ l: 'm', r: 89 },
Expand Down Expand Up @@ -167,13 +173,9 @@ export default (o, c, d) => {
out = loc[key]
} else {
// Choose the plural form using the index decided by the plural rule
let { pluralRule } = locs
if (typeof pluralRule === 'number') {
pluralRule = pluralRules[pluralRule]
}
const pluralForms = loc[key]
const pluralFormIndex = pluralRule(abs)
out = pluralForms[pluralFormIndex].replace('%d', abs)
const pluralForm = getPluralFormForCardinalByLocale(locale.name, abs)
out = pluralForms[pluralForm].replace('%d', abs)
}
break
}
Expand Down
Loading

0 comments on commit c3a6822

Please sign in to comment.