inno-trans
📜 simple localization library (inspired by laravel translation)
Example
const t = t // => hello world!
Install
yarn add inno-trans
const InnoTrans = ;
browser
Features
Interpolation
Interpolate using tag bracket ({}
).
const t =
t // => welcome, john!
Change interpolation tag bracket.
const t =
tt // => welcome, john!
Pluralization
Choose a message that matches the quantity.
const t =
t // => one applet // => many apples
Complex pluralization
const t =
t // => nonet // => somet // => many
Fallback
Fallback a another locale when there is no message.
const t =
t // => index.hellot // => index.welcome t t // => 안녕~t // => 歓迎よ~
Filter
Converts interpolation values.
const t =
tt t // => welcome JOHN!t // => hello john!
Common filter
const t =
tt
Formatter
Converts interpolated messages.
const t =
t t// str: welcome, john!, values: {"name":"john"}, locale: en// => <p>welcome, john!</p>
Event
const t = t // => welcome, john! t t
events
changeLocale
changeFallbacks
changeTag
addMessages
removeMessages
addFilter
removeFilter
addFormatter
removeFormatter
*
- Global event.
API
InnoTrans(options)
Create InnoTrans instance.
const t =
options
locale
- Specifies current locale. If not, it is automatically inferred.fallbacks
- Specifies another locales to fallback.messages
- Message resources.tag
- Prefix and suffix for interpolation. Default{
,}
.filters
- Function for converting interpolation values.formatters
- Function for converting interpolated message.plugins
- Plugin to use.
t.trans(key [, values [, options]])
Returns a message that matches the key.
options
locale
- Specifies the locale. The current locale is ignored.defaults
- String to return instead of the key when a message does not exist.
const t = t // => welcomet // => Welcome~!t // => hello!t // => 안녕
Short method
t
t.transChoice(key, number [, values [, options]])
Returns a message that matches the key and the quantity number.
t t // => I bought onet // => I bought many things!
Short mehotd
t;
t.locale([locale])
t // Change locale to "ko".t // => 'ko'
t.fallbacks([fallbacks])
tt // => ['ko', 'ja']
t.addMessages(locale, messages)
t
t.removeMessages(locale[, key])
If no key
argument, remove all messages in the locale.
tt
t.getAddedLocales()
Returns the added message locales.
t // => ['en', 'ko', 'ja']tt // => ['ko', 'ja']
t.hasMessage(locale [, key])
t // => false t t // => truet // => truet // => false
t.tag(tag)
Set prefix and suffix for interpolation.
t
t.addFilter(name, filter)
Add a filter function.
t.removeFilter(name[, filter])
Remove a filter function.
t.addFormatter(formatter)
Add a formatter function.
t.removeFormatter(formatter)
Remove a formatter function.
t.use(plugin)
Add a plugin.
License
MIT