@ulule/localize
v1.2.2
Published
Translations management
Downloads
7
Maintainers
Keywords
Readme
localize
Localize is a tiny library to manage translations in your javascript application. It is based on React and uses Jed(https://github.com/messageformat/Jed) and the Gettext message format.
To use Localize you need first to initialize the library with your gettext strings (as json).
import { setTranslations } from 'localize'
setTranslations(your_translations)
Localize is composed of 4 functions:
- t()
- tn()
- tc()
- tnc()
t()
t() allows you to include a single translation
import { t } from 'localize'
t('Anonymous')
t('Your are on %(username)s’s profile', { username: 'Ulule' })
tp()
tp() allows you to include a translation with context, usually for translate same translation. It will use context as key.
import { tp } from 'localize'
tp('remind-me', 'Listed')
tp('coming-soon', 'Listed') // could be translate differently
tp('%(username)s’s profile', { username: 'Ulule' })
tn()
t() allows you to include a single translation and the plural version
import { tn } from 'localize'
tn('%d supporter', '%d supporters', 1)
tn('%(num)d supporter', '%(num)d supporters', 1, {num: 1})
tc()
tc() allows you to include some html tag in a single translation
import { tc } from 'localize'
tc('Include a [br] br element', {br: <br />})
tc('You already have an account? [link: Sign in!]', {link: <a href="#" />})
tnc()
tnc() allows you to include some html tag in a single translation and the plural version
import { tnc } from 'localize'
tnc(
'Already %(count)d [strong: comment]. Something more?',
'Already %(count)d [strong: comments]. Something more?',
20,
{
count: 20,
strong: <strong />,
}
)