https://github.com/eemeli/messages
Text message generation
https://github.com/eemeli/messages
Last synced: 4 months ago
JSON representation
Text message generation
- Host: GitHub
- URL: https://github.com/eemeli/messages
- Owner: eemeli
- License: mit
- Created: 2018-04-07T16:45:48.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-03T17:13:04.000Z (over 3 years ago)
- Last Synced: 2026-01-04T11:11:42.963Z (5 months ago)
- Language: JavaScript
- Size: 350 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Messages
A work in progress. Eventually, a complete framework for localizable messages for all user interfaces.
The commented examples below use [Fluent](https://projectfluent.org/) notation.
```js
import msg, { select } from 'messages'
// msg1 = foo
msg('foo') === 'foo'
// msg2 = bar
msg`bar` === 'bar'
// msg3 = string {$baz}
const baz = 'BAZ'
msg`string ${baz}` === 'string BAZ'
// selectMsg = { $arg ->
// [foo] bar
// *[other] qux
// }
const selectMsg = select({ foo: 'bar', other: 'qux' })
selectMsg('foo') === 'bar'
selectMsg('baz') === 'qux'
// pluralMsg = { $arg ->
// [42] truth
// [one] one
// *[other] other
// }
const pluralMsg = select({ one: 'one', 42: 'truth', other: 'other' })
pluralMsg(1) === 'one'
pluralMsg(2) === 'other'
pluralMsg(42) === 'truth'
// ordinalMsg = { NUMBER($arg, type: "ordinal") ->
// [1] first!
// [one] {$arg}st
// [two] {$arg}nd
// [few] {$arg}rd
// *[other] {$arg}th
// }
const ordinalMsg = select(
{
1: 'first!',
one: '#st',
two: '#nd',
few: '#rd',
other: '#th'
},
{ type: 'ordinal' }
)
ordinalMsg(2) === '2nd'
ordinalMsg(21) === '21st'
// -person = { $gender ->
// [male] He
// [female] She
// *[other] They
// }
// -results = { $count ->
// [0] no results
// [one] 1 result
// *[other] # results
// }
// resultSummary = {-person} found {-results}
const person = select({ male: 'He', female: 'She', other: 'They' })
const results = select({
0: 'no results',
one: '1 result',
other: '# results'
})
const resultSummary = msg`${person(gender)} found ${results(count)}.`
```