1.0.2 • Published 4 years ago
estafette-intl v1.0.2
Installation
With yarn
:
yarn add estafette-intl
With npm
:
npm install estafette-intl
Getting started
Steps:
- Create internalization
- Create messages
- Use internalization using useIntl
Create internalization
import { CreateIntl } from 'estafette-intl';
ReactDOM.render(
<CreateIntl defaultLocale="en" messages={messages}>
<App />
</CreateIntl>,
document.getElementById('root'),
);
Create messages
import { createMessages } from 'estafette-intl';
const messages = createMessages([
{
gretting: {
en: 'Hello, {name}!',
},
},
]);
Use internalization using useIntl
import { useIntl } from 'estafette-intl';
const App = () => {
const { t } = useIntl();
return <h1>{t('gretting', { name: 'Vladimir Josan' })}</h1>;
};
API
CreateIntl
Internalization creator
Props
defaultLocale
(String) - it can be simple string, that you will be able to change fromuseIntl
usingsetLocale
messages
(Object) -Messages
fromcreateMessage
createMessage
function helper, gets array of messages, parses all of them into an object and checks for dublicates
Arguments
messages
(Array) -Messages
Messages
typescript interface
Interface
interface Messages {
[locale: string]: {
[lang: string]: string;
};
}
useIntl
hook helper for using internalization
Returns
t
(Function) - a function that requireslocale
(String) argument and returns bylocale
keyword necessary textlocale
(String) - current localesetLocale
(Function) - a function that requiresnewLocale
(String) argument and change current locale to the new locale.