You can also format values you pass as an argument to typesafe-i18n.
See here on more information how to set up typesafe-i18n.
npm install typesafe-i18nYou can find an example how to configure and use formatters here.
You can run the example by opening the folder in your terminal and running:
npm run devYou can specify your own formatters, that take an argument as an input and returns another value.
For more information about the
LLLobject, read the usage section of the main repository.
const formatters = {
custom: (value) => (value * 4.2) - 7
}
LLL("For input '{0}' I get '{0|custom}' as a result", 100)
// => "For input '100' I get '413' as a result"If you need to apply multiple formatters to the same argument, you can chain them by using the pipe | operator:
const formatters = {
sqrt: (value) => Math.sqrt(value),
round: (value) => Math.round(value),
}
LLL('Result: {0|sqrt|round}', 5)
// => 'Result: 2'The formatters get applied from left to right. So in this example
5will be the input for thesqrtformatter, that will return2.23606797749979. This value then gets passed to theroundformatter that will output2.
You can also use a few builtin formatters:
A wrapper for Intl.DateTimeFormat.
import { date } from 'typesafe-i18n/formatters'
const formatters = {
weekday: date('en', { weekday: 'long' })
}
LLL('Today is {0|weekday}', new Date())
// => 'Today is friday'See here if you want to use this formatter in a Node.JS environment.
Same as the date-formatter
import { time } from 'typesafe-i18n/formatters'
const formatters = {
timeShort: time('en', { timeStyle: 'short' })
}
LLL('Next meeting: {0|timeShort}', meetingTime)
// => 'Next meeting: 8:00 AM'A wrapper for Intl.NumberFormat
import { number } from 'typesafe-i18n/formatters'
const formatters = {
currency: number('en', { style: 'currency', currency: 'EUR' })
}
LLL('Your balance is {0|currency}', 12345)
// => 'your balance is €12,345.00'See here if you want to use this formatter in a Node.JS environment.
A wrapper for String.prototype.replace
import { replace } from 'typesafe-i18n/formatters'
const formatters = {
noSpaces: replace(/\s/g, '-')
}
LLL('The link is: https://www.xyz.com/{0|noSpaces}', 'super cool product')
// => 'The link is: https://www.xyz.com/super-cool-product'Returns the variable without modifications
import { identity } from 'typesafe-i18n/formatters'
const formatters = {
myFormatter: identity // (value) => value
}
LLL('Hello {name|myFormatter}', { name: 'John' })
// => 'Hello John'Ignores the variable and returns an empty string.
import { ignore } from 'typesafe-i18n/formatters'
const formatters = {
myFormatter: ignore // () => ''
}
LLL('Hello {name|myFormatter}', { name: 'John' })
// => 'Hello 'A wrapper for String.prototype.toUpperCase
import { uppercase } from 'typesafe-i18n/formatters'
const formatters = {
upper: uppercase
}
LLL('I said: {0|upper}', 'hello')
// => 'I said: HELLO'A wrapper for String.prototype.toLowerCase
import { lowercase } from 'typesafe-i18n/formatters'
const formatters = {
lower: lowercase
}
LLL('He said: {0|lower}', 'SOMETHING')
// => 'He said: something'