@codedbypol/translate
v1.0.2
Published
Npm package to handle translations in client side of React. It consist on a context used to surround your app and a custom hook were the sent key is auto-translated
Downloads
1
Maintainers
Readme
@codedbypol/translations
Npm package to handle translations in client side of React. It consist on:
- A context used to surround your app.
- A custom hook were the sent key is auto-translated
Feel free to contribute to this project.
Installation
npm install @codedbypol/translations
Usage
- Create a file with your translations. For example,
translations.js
:
export default {
en: {
hello: "Hello",
world: "World",
},
es: {
hello: "Hola",
world: "Mundo",
},
};
- Wrap your app with the
TranslationContext
and pass the translations as a prop:
import React from "react";
import translations from "./translations";
const App = () => {
return (
<TranslationContext.Provider
value={{ key: "language", defaultLanguage: "en", translations }}
>
<YourApp />
</TranslationContext.Provider>
);
};
- Use the
useTranslate
hook to translate your keys:
import React from "react";
import { useTranslate } from "@codedbypol/translations";
const YourApp = () => {
const { t } = useTranslate();
return (
<div>
<h1>{t("hello")}</h1>
<h2>{t("world")}</h2>
</div>
);
};