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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions 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'
'dayjs', 'fast-plural-rules'
],
plugins: [
babel({
Expand All @@ -21,7 +21,8 @@ module.exports = (config) => {
format: 'umd',
name: name || 'dayjs',
globals: {
dayjs: 'dayjs'
dayjs: 'dayjs',
'fast-plural-rules': 'fastPluralRules'
}
}
}
Expand Down
59 changes: 58 additions & 1 deletion docs/en/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ const localeObject = {
months: 'Enero_Febrero ... '.split('_'), // months Array
monthsShort: 'Jan_F'.split('_'), // OPTIONAL, short months Array, use first three letters if not provided
ordinal: n => `${n}º`, // ordinal Function (number) => return number + output
relativeTime: {
// see below
}
}
```

Old template of the part of a Day.js locale Object for the RelativeTime plugin. It works well for languages which do not decline nouns and which have only one form of plural. English, for example.
```javascript
relativeTime: { // relative time format strings, keep %s %d as the same
future: 'in %s', // e.g. in 2 hours, %s been replaced with 2hours
past: '%s ago',
Expand All @@ -98,9 +106,58 @@ const localeObject = {
y: 'a year',
yy: '%d years'
}
}
```

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
// Using 3 plural forms in Slavic languages
duration: {
// Static message, just one singular/plural form needed
s: 'několik sekund',
// Static message for a single minute without any number
m: 'minuta',
// Plural forms for 1, 2 to 4, and 5 and more minutes
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 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
import dayjs from 'dayjs'
Expand Down
10 changes: 10 additions & 0 deletions docs/en/Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,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/dayjs.min.js"></script>
<script arc="https://unpkg.com/dayjs/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
51 changes: 28 additions & 23 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,7 @@
"size-limit": "^0.18.0",
"typescript": "^2.8.3"
},
"dependencies": {}
"dependencies": {
"fast-plural-rules": "^0.0.1"
}
}
54 changes: 54 additions & 0 deletions src/locale/cs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import dayjs from 'dayjs'

const locale = {
name: 'cs',
weekdays: 'neděle_pondělí_úterý_středa_čtvrtek_pátek_sobota'.split('_'),
months: 'leden_únor_březen_duben_květen_červen_červenec_srpen_září_říjen_listopad_prosinec'.split('_'),
ordinal: n => `${n}.`,
relativeTime: {
// Using 3 plural forms for 1, 2-4, 5-
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']
}
}
}

dayjs.locale(locale, null, true)

export default locale
Loading