jshmrtn / vue3-gettext

Translate Vue 3 applications with gettext.
https://jshmrtn.github.io/vue3-gettext/
MIT License
66 stars 23 forks source link

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

Closed wkra closed 1 year ago

wkra commented 1 year ago

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.

lzurbriggen commented 1 year ago

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 1 year ago

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 :)