Skip to content

react-i18next extension to enable easy loc string loading (as object) using decorators

License

Notifications You must be signed in to change notification settings

sidecus/react-i18next-extension

Repository files navigation

react-i18next-extension

react-i18next extension to enable loc string loading as objects using decorators

NPM CI

Install

npm install react-i18next-extension
#or
yarn add react-i18next-extension

Usage

Enabling decorators

You'll need to enable decorators (experimental) for your project. See detailed instructions from serializr.

Define loc string object in your loc strings, e.g. translation.json:

{
  "myComponent": {
    "string1": "locstring1",
    "string2": "locstring2 {{someinterpolationvalue}}"
  }
}

Use locSectionName decorator and useLocStrings hook in your component

@locSectionName('myComponent')
class MyComponentStrings {
  string1 = '';
  string2 = '';
}
const MyComponent = () => {
    const strings = useLocStrings(MyComponentStrings);
    // or below if you are using interpolation - second param is
    // the same as that of t function
    // const strings = useLocStrings(
    //    MyComponentStrings,
    //    { someinterpolationvalue: 'bla' }
    // );
    return <div>{strings.string1}</div>;
}

Happy coding. Peace.

MIT © sidecus

About

react-i18next extension to enable easy loc string loading (as object) using decorators

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published