Assembless / react-littera

🌐 Modern react library for managing translations.
MIT License
19 stars 1 forks source link

useLittera can't be used in an object #8

Closed xomod closed 4 years ago

xomod commented 4 years ago

Is your feature request related to a problem? Please describe. I wanted to use useLittera in my object, but unfortunately it's impossible. useLittera can only be used in a component, which is very sad for me.

DRFR0ST commented 4 years ago

You can use translate method to get translated object returned but you have to pass a locale which is stored in Reacts context. You probably won't be able to read the context state outside of render functions/classes. As a walk around you could wrap your objects with a hook and then get it returned like in a regular scenario.

It will probably not solve your problem but you might do it like in the example below:

const useMyObjects = () => {
    const translated = useLittera(translations);

    return [
      {
         title: `Category: ${translated.cat01Title}`,
         isCool: true,
         followers: 69
      },
      {
         title: `Category: ${translated.cat02Title}`,
         isCool: true,
         followers: 420
      }
    ]
}

const YourComponent = () => {
     const myObjects = useMyObjects();

     return <h1>{myObjects[0].title}</h1>
}
xomod commented 4 years ago

Thanks my master, that's working!