jshmrtn/vue3-gettext

inject() can only be used inside setup() or functional components.

wkra opened this issue · 2 comments

wkra commented

Is there a way to use in composable file/method instead of a component?

If I try to do a composable file outside vue component:
const { $gettext } = useGettext(); in composable file/method

I've got an error:
inject() can only be used inside setup() or functional components.

hi @wkra! you can move the createGettext(...) into a separate file and export it, then import it in your main.ts and other places where you need gettext, something like this:

export const myGettext = createGettext(...);
import { myGettext } from "./gettext";
const { $gettext } = myGettext;

maybe i'll make this easier in the future, because this plugin doesn't have to rely on vue's provide/inject mechanism...

wkra commented

Thanks, that solve the problem. This is in the documentation https://jshmrtn.github.io/vue3-gettext/configuration.html#using-gettext-functions-outside-of-components but I didn't find it before - sorry for that. Thanks for help :)