Gathering detailed insights and metrics for lioness
Gathering detailed insights and metrics for lioness
Gathering detailed insights and metrics for lioness
Gathering detailed insights and metrics for lioness
npm install lioness
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
29 Stars
181 Commits
4 Forks
3 Watching
22 Branches
5 Contributors
Updated on 26 Jan 2023
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-47.5%
31
Compared to previous day
Last week
-18.6%
249
Compared to previous week
Last month
6.9%
1,146
Compared to previous month
Last year
10.2%
17,888
Compared to previous year
2
25
Lioness is a React library for efficiently implementing Gettext localization in your app with little effort.
It utilises node-gettext
as translations tool, but this ought to be modularized in the future.
1<T 2 message="You have one thingy, {{ itemLink:check it out }}" 3 messagePlural="You have {{ count }} thingies, {{ listLink:check them out }}" 4 count={items.length} 5 itemLink={<a href={`/thingies/${items[0].id}`} />} 6 listLink={<a href="/thingies" />} 7/> 8// items.length === 1 => Du har en grej, <a href="/thingies/281">kolla in den här<a/>. 9// items.length === 7 => Du har 7 grejer, <a href="/thingies">kolla in dem här<a/>.
{{ variable }}
style syntax{{ link:Link text here }}
style syntax1npm install --save lioness 2 3# ...or the shorter... 4npm i -S lioness
This is an example app showing how to translate some text:
1import React from 'react' 2import ReactDOM from 'react-dom' 3import { LionessProvider, T } from 'lioness' 4 5// messages.json is a JSON file with all translations concatenated into one. 6// The format must conform to what node-gettext expects. 7// 8// See https://github.com/alexanderwallin/node-gettext#Gettext+addTranslations 9import messages from './translations/messages.json' 10 11function App({ name, numPotatoes }) { 12 return ( 13 <LionessProvider 14 messages={messages} 15 locale="sv-SE" 16 debug={/* true | false | null */} 17 > 18 <div className="App"> 19 <h1><T>Potato inventory</T></h1> 20 {/* => <h1><span>Potatisinventarie</span></h1> */} 21 22 <T 23 message="Dear {{ name }}, there is one potato left" 24 messagePlural="Dear {{ name }}, there are {{ count }} potatoes left" 25 count={numPotatoes} 26 name={name} 27 /> 28 {/* => <span>Kära Ragnhild, det finns 2 potatisar kvar</span> */} 29 30 <T 31 message="By more potatoes {{ link:here }}!" 32 link={<a href="http://potatoes.com/buy" />} 33 /> 34 {/* => <span>Köp mer potatis <a href="http://potatoes.com/buy">här</a>!</span> */} 35 </div> 36 </LionessProvider> 37 ) 38} 39 40ReactDOM.render( 41 <App name="Ragnhild" numPotatoes={Math.round(Math.random() * 3))} />, 42 document.querySelector('.app-root') 43)
<T />
<T />
exposes a set of props that make it easy to translate and interpolate your content. Being a React component, it works perfect for when you are composing your UI, like with the example above.
withTranslators(Component)
Sometimes, you will need to just translate and interpolate pure strings, without rendering components. To do this you can hook up your components with translator functions using the withTranslators(Component)
composer function.
withTranslators(Component)
will provide any component you feed it with a set of translator functions as props. Those props are: t
, tn
, tp
, tnp
, tc
, tcn
, tcp
and tcnp
.
1import { withTranslators } from 'lioness' 2 3function PotatoNotification({ notificationCode, t }) { 4 let message = '' 5 6 if (notificationCode === 'POTATOES_RECEIVED') { 7 message = t(`You have received potatoes`) 8 } else if (notificationCode === 'POTATOES_STOLEN') { 9 message = t(`Someone stole all your potatoes :(`) 10 } 11 12 return <span>{message}</span> 13} 14 15export default withTranslators(PotatoNotification)
babel-plugin-react-gettext-parser
1// .babelrc 2{ 3 ... 4 "plugins": [ 5 ["react-gettext-parser", { 6 "output": "gettext.pot", 7 "funcArgumentsMap": { 8 "tc": ["msgid", null], 9 "tcn": ["msgid", "msgid_plural", null, null], 10 "tcp": ["msgctxt", "msgid", null], 11 "tcnp": ["msgctxt", "msgid", "msgid_plural", null, null], 12 13 "t": ["msgid"], 14 "tn": ["msgid", "msgid_plural", null], 15 "tp": ["msgctxt", "msgid"], 16 "tnp": ["msgctxt", "msgid", "msgid_plural", null] 17 }, 18 "componentPropsMap": { 19 "T": { 20 "message": "msgid", 21 "messagePlural": "msgid_plural", 22 "context": "msgctxt", 23 "comment": "comment" 24 } 25 } 26 }] 27 ] 28 ... 29}
Lioness makes it possible to change locale and have all the application's translations instantly update to those of the new locale. <LionessProvider>
will trigger a re-render of all <T>
components and components wrapped in withTranslators()
whenever its locale
or messages
props change.
Note: For performance reasons, and in favour of immutability, this check is done using shallow equality, which means you need to pass an entirely new object reference as messages
for it to trigger the re-render. If this is an issue for you, simply make sure you create a new object when you get new messages, for instace by using something like messages = Object.assign({}, messages)
.
The following table indicates how gettext strings map to parameters in withTranslations
and props for <T />
Gettext | withTranslations | <T /> |
---|---|---|
msgctxt | context | context |
msgid | message | one | message |
msgid_plural | other | messagePlural |
withTranslations(Component)
Provides Component
with the lioness
context variables as props. These are locale
, t
, tn
, tp
, tnp
, tc
, tcn
, tcp
and tcnp
.
As a little helper, here's what the letters stand for:
Letter | Meaning | Parameters |
---|---|---|
t | translate a message | message |
c | ...with injected React components | - |
n | ...with pluralisation | one , other , count |
p | ...in a certain gettext context | context |
locale
The currently set locale passed to <LionessProvider />
.
t(message, scope = {})
Translates and interpolates message.
tn(one, other, count, scope = {})
Translates and interpolates a pluralised message.
tp(context, message, scope = {})
Translates and interpolates a message in a given context.
tnp(context, one, other, count, scope = {})
Translates and interpolates a pluralised message in a given context.
tc(message, scope = {})
Translates and interpolates a message.
tcn(one, other, count, scope = {})
Translates and interpolates a pluralised message.
tcp(context, message, scope = {})
Translates and interpolates a message in a given context.
tcnp(context, one, other, count, scope = {})
Translates and interpolates a plural message in a given context.
<LionessProvider />
A higher-order component that provides the translation functions and state to <T />
through context.
Props:
messages
– An object containing translations for all languages. It should have the format created by gettext-parserlocale
– The currently selected locale (which should correspond to a key in messages
)gettextInstance
- A custom node-gettext instance. If you provide the messages
and/or local
props they will be passed on to this instance.transformInput
– A function (input: String) => String
that you can use to transform a string before <T />
sends it to the translation function. One use case is normalising strings when something like prettier
puts child content in <T />
on new lines, with lots of indentation. The default is a function that simply returns the input as is.All PRs that passes the tests are very much appreciated! 🎂
No vulnerabilities found.
No security vulnerabilities found.