-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to run gettext function in custom js module? #76
Comments
Have you tried |
This is interesting but I have a similar need for vuex and for a pure js module (messages used by vuelidate-error-extractor). Do you have a solution for these situations? |
We're doing this:
And in a import gettext from '@helper/gettext'
export default {
mainNav: {
animals: {
itemText: gettext('AnimalsText'),
routeName: 'Animals',
},
... Does this help? |
I tried it, I created gettext.js and then I imported it in the store and I get the error: |
Did you add the plugin to your Vue instance? The idea assumes that you integrated vue-gettext into your Vue-project already. If yes we need to check more detailed. Maybe you can create a test-repo and share it. |
Yes, I integrated vue-gettext with following conf:
And is working perfectly. Honestly -due to my poor knowledge of js, my background is Python...- I'm, uncertain of what happens when you write Vue.use(...): any instance of Vue will get that plugin from Vue.prototype? that means that if it works I can get also $language in the same way? Could you even use Vue.prototype.$gettext directly? |
I prepared this codesandbox that shows the error I'm getting. Any hint is appreciated. |
@sandroden as its name implies, this plugin is thightly coupled to a Vue.js application. All components of an app are sharing the same Vue instance (in your codesandbox it's the one in When you're importing Vue in your As suggested by others, you can find some ways to share the Vue instance and somewhat use the plugin in custom js modules, but you will loose the reactivity that is provided by the component and the directive. All use cases supported by the plugin are covered in the documentation. |
My codesandbox sample was in fact the attempt to implement @escapedcat 's suggestion. Reactivity may not be an issue in mutations and actions at least. I just realized that store has an instance of vue as |
There is another easy working way to do this: you can push this.$gettext object into a vuex store as a state variable and then use it there. |
Any solution to this? how could we use gettext in a completely separate js file? |
I recommend to use i18n for Vue projects. It also has Nuxt Js module |
Well, I'm actually switching from i18n. gettext is the standard way of handling translations in linux and has automatic extraction of string. I don't really understand why the vue community opted for a different approach. Besides lacking the automatic extraction of string, i18n is more verbose, since we have to write message.key everywhere and it makes more difficult to non-programmers translators to translate the files. AFAIK, there is no software like poedit that allows a non technical person to translate the strings. |
This might be helpful to people here: #51 (comment) |
There's another way of doing this by using a "lazy-string" technique
In any files, you can just use it
And that should work with your components
This should also work for SSR too, if you use Nuxt, please make sure the language is set properly before the page rendered, there're hooks that run after plugin initialization and before rendering, you should set your language in those hooks from your cookies, user's settings or whatever, good luck |
Hello.
Please advise, how to run translation functions like (gettext, ngettext and etc. ) in custom js file? For example Vue-Gettext is installed and initiated in main.js file, everything is working fine, but if I want to use gettext functions in router.js file, functions are not working, I have tried:
Vue.$gettext('Some text')
this.$gettext('Some text')
Vue.prototype.$gettext('Some text')
Nothing works.
In Router file I am setting titles for each page, and I wish, that they will be available for translating.
Please help.
Thank you.
The text was updated successfully, but these errors were encountered: