1.0.0 β€’ Published 3 years ago

templural v1.0.0

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
3 years ago

templural 🍀

Template function for plural-sensitive formatting.

CI Version License: Apache-2.0

README en français

Usage

Install

yarn add templural

or

npm install templural

Import

import { templural } from 'templural'

or

const { templural } = require('templural')

Choose a locale

templural uses Intl.PluralRules to know which plural rule to apply for a given number, and plural rules vary depending on the language.

This means you have to set the locale used by templural in order to format correct sentences.

Either set the default locale:

templural.setLocales('fr_BE') // French (Belgium)

or create a new template function for a specific locale:

import { forLocales } from 'templural'

const templuralDeCH = forLocales('de_CH') // German (Switzerland)

For more information about the values accepted by templural.setLocales() and forLocales() see locales argument.

The following examples are specific to the English language, see Internationalization for information about other languages.

Match a word to a preceding number

templural`Yoann and Valentin had ${numberOfIdeas} interesting idea{s}`

// numberOfIdeas = 1 βž” "Yoann and Valentin had 1 interesting idea"
// numberOfIdeas = 2 βž” "Yoann and Valentin had 2 interesting ideas"
// numberOfIdeas = 42 βž” "Yoann and Valentin had 42 interesting ideas"
// numberOfIdeas = 0 βž” "Yoann and Valentin had 0 interesting ideas"

Insert any other values in the text

templural`${userName} has ${nbPoints} point{s}`

// userName = "Joe", nbPoints = 1 βž” "Joe has 1 point"
// userName = "Mario", nbPoints = 1000 βž” "Mario has 1000 points"

Match several words to the same preceding number

templural`I just had ${nbPints} bear{s} darling{s}, I swear{s}`

// nbPints = 1 βž” "I just had 1 bear darling, I swear"
// nbPints = 2 βž” "I just had 2 bears darlings, I swears"
// nbPints = 6 βž” "I just had 6 bears darlings, I swears"

Match several words each to a different preceding number

templural`I bought ${nbCarrots} carrot{s} and ${nbPotatoes} potato{es}`

// nbCarrots = 1, nbPotatoes = 1 βž” "I bought 1 carrot and 1 potato"
// nbCarrots = 1, nbPotatoes = 3 βž” "I bought 1 carrot and 3 potatoes"
// nbCarrots = 2, nbPotatoes = 1 βž” "I bought 2 carrots and 1 potato"
// nbCarrots = 2, nbPotatoes = 3 βž” "I bought 2 carrots and 3 potatoes"

Words with a different form in the singular and plural

templural`${nbConnected} {person;people} {is;are} connected`

// nbConnected = 1 βž” "1 person is connected"
// nbConnected = 2 βž” "2 people are connected"
// nbConnected = 666 βž” "666 people are connected"

Mix all the previous examples

templural`${nbDogs} dog{s} bark{s;} and ${nbCats} cat{s} meow{s;}`

// nbDogs = 1, nbCats = 1 βž” "1 dog barks and 1 cat meows"
// nbDogs = 2, nbCats = 1 βž” "2 dogs bark and 1 cat meows"
// nbDogs = 1, nbCats = 2 βž” "1 dog barks and 2 cats meow"
// nbDogs = 2, nbCats = 2 βž” "2 dogs bark and 2 cats meow"

Match a word to a non preceding number

templural`There {$1;is;are} ${nbWhales} flying whale{s}`

// nbWhales = 1 βž” "There is 1 flying whale"
// nbWhales = 2 βž” "There are 2 flying whales"

$1 references the first interpolated expression.

Use $2, $3 or $n to reference the second, the third or the nth interpolated expression.

πŸ—£οΈ Internationalization

templural is built on top of Intl.PluralRules and may be used to format sentences in any language.

Plural rules

Each language has different plural rules.

For example English has two plural categories, "one" for singular, and "other" for plural:

templural.setLocales('en')

templural`${n} is in the {one;other} category`

// n = 1 βž” "1 is in the one category"
// n = 2 βž” "2 is in the other category"
// n = 1000000 βž” "1000000 is in the other category"
// n = 0 βž” "0 is in the other category"

French is a little different from English, it has a third category "many" for some large numbers, and 0 is singular:

templural.setLocales('fr')

templural`${n} is in the {one;other;many} category`

// n = 1 βž” "1 is in the one category"
// n = 2 βž” "2 is in the other category"
// n = 1000000 βž” "1000000 is in the many category"
// n = 0 βž” "0 is in the one category"

See Language Plural Rules for information about plural rules in any language.

FIXME category order

FIXME category priority

FIXME category fallbacks

❓ FAQ

Any new features planned?

Not for the moment.

templural is simple and dumb, and it will probably stay like this.

What about negative or floating numbers?

🚧 FIXME

Any other questions?

Use the Discussions tab.

Authors & Contributors

Special thanks go to Valentin Cocaud and Yoann Prot for their original idea behind templural.

πŸ‘€ Nicolas Lepage

🀝 Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page.

Show your support

Give a ⭐️ and/or sponsor if this project helped you!

πŸ“ License

Copyright Β© 2021 Nicolas Lepage. This project is Apache-2.0 licensed.