Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
@m6web/react-i18n
Advanced tools
This library brings internationalisation through a set of react components.
The translation function is in i18n.utils.js file.
It uses react context API to provide translation function to every components.
React version: ^16.4.0
First install the library
yarn add -E @m6web/react-i18n
Add the provider above a component to configure and provide translation function.
// Import the provider
import { I18nProvider } from '@m6we/react-i18n';
// Dictionnary for your app, you should have a different dictionary for each language
const translations = {
foo: {
bar: 'Foo bar',
legal: 'App from %(company)s'
}
};
// Global values used for interpolations in some translations like in the foo.legal key.
const i18nNames = {
company: 'm6'
};
// Put your app in the provider
const Root = () => (
<I18nProvider lang={translations} i18nNames={i18nNames} >
<App />
</I18nProvider>
);
This component needs React 16 at least because its render returns a string value.
import React from 'react';
import { Trans } from '@m6we/react-i18n';
// Interpolation values
const data = { element: 'foo' };
export default const MyComponent = ({ nbExample, t }) => {
return (
<div class="foo">
<h1>
<Trans i18nKey="foo.bar" />
</h1>
<p>
<Trans i18nKey="foo.exemple" number={nbExample} data={data} general/>
</p>
</div>
);
};
import React from 'react';
import { HtmlTrans } from '@m6we/react-i18n';
// Interpolation values
const data = { element: 'foo' };
export default const MyComponent = ({ nbExample, t }) => {
return (
<div class="foo">
<HtmlTrans i18nKey="foo.bar" element="h1" />
<HtmlTrans i18nKey="foo.exemple" number={nbExample} data={data} general element="p" />
</div>
);
};
span
)Note that number and data can be used together.
This HOC provides the translate function to the component as prop.
import React from 'react';
import { translate } from '@m6we/react-i18n';
// Interpolation values
const data = { element: 'foo' };
const MyComponent = ({ nbExample, t }) => {
return (
<div class="foo">
<h1>
{t('foo.bar')}
</h1>
<p>
{t('foo.exemple', data, nbExample, true)}
</p>
</div>
);
};
export default translate(MyComponent);
Note that number and data can be used together.
Build list function allows you to build a list in specific language.
import { buildListFunction } from '@m6we/react-i18n';
// Define separators with translations
const lang = {
_i18n: {
separator: ', ',
and: ' and ',
}
};
const list = buildListFunction(lang)(['foo', 'bar', 'foobar']);
// list => 'foo, bar and foobar'
The translate function provided in the component and the container handle plural for several languages.
The lang has to be set through _i18n.lang
key, and should be in lower case.
This is the configuration of plural form for keys:
language | zero | singular | general plural | first plural | second plural | third plural |
---|---|---|---|---|---|---|
FR | one | one | other | other | - | - |
EN | other | one | other | other | - | - |
HU | one | one | other | one | - | - |
HR | many | one | other | one | few | many |
The variable used in translation template string has to be %(number)d
, and is automatically provided by the translate function.
To use general form, you need to set 4th parameter of the translate function to true
FAQs
Provider and utils for translation in a react app
The npm package @m6web/react-i18n receives a total of 1 weekly downloads. As such, @m6web/react-i18n popularity was classified as not popular.
We found that @m6web/react-i18n demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.