-
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
Decouple Vue from translate.js #113
Conversation
@@ -179,3 +179,176 @@ describe('Translate tests', () => { | |||
}) | |||
|
|||
}) | |||
|
|||
describe('Translate tests without Vue', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the same as the describe()
above but with Vue.config
replaced with a local config
object.
Hi and thank you for your PR. Vue-gettext tries to do just one thing : translate a Vue.js application with gettext. Why don't you use gettext.js to translate a node script? |
I am using https://github.com/Polyconseil/easygettext to extract/compile all Javascript translations. vue-gettext works with JSON output of easygettext. https://github.com/guillaumepotier/gettext.js suggests https://github.com/Wisembly/xgettext-php, not easygettext, for parsing translations. There is no guarantee that gettext.js will abide by the JSON output format of easygettext. If they don't match 1-to-1, then I have to create my own gettext API to use easygettext JSON which ruins the point of using a JS dependency for gettext API. Most of my project translations are for Vue.js application but there are a few nodejs scripts that need translations. I want to keep my gettext dependencies to this project and easygettext if possible. Using vue-gettext in nodejs scripts without this PR works but I want to avoid adding a |
OK, I understood. I'll review your PR asap. |
src/translate.js
Outdated
@@ -157,4 +163,20 @@ export default { | |||
return this.getTranslation(msgid, n, context, plural) | |||
}, | |||
|
|||
/* | |||
* Initialize local state for translations and configuration. | |||
* Required to decouple global 'Vue' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to be explicit somewhere in the comments and explain why you're doing this: to use vue-gettext without Vue.
Please, can you update the README too. Add documentation and at least one example describing how to use the plugin for a Node script.
This makes it possible to use vue-gettext without Vue. Use case is to reuse vue-gettext for nodejs scripts that need only the gettext API for translations.
7faa2b4
to
a17fa96
Compare
Can you also please add a little doc in the README about how to use the plugin for a node script. Or tell me how you use it and I'll do it. |
How I would use is what's done in the tests: swap out import {translate} from 'vue-gettext';
import translations from './translations.json';
const config = {
language: '',
getTextPluginSilent: false,
getTextPluginMuteLanguages: [],
silent: false,
};
// easygettext aliases
const {
gettext: $gettext,
ngettext: $ngettext,
} = translate;
translate.initTranslations(translations, config);
let n = 3;
console.log($gettext('Hello'));
console.log($ngettext('There is one sheep', 'There are many sheeps', n)); |
@janlazo How would you explain the benefits of your PR to other users of vue-gettext? |
vue-gettext users can import import Vue from 'vue';
let str = Vue.prototype.$gettext('Hello'); import {translate} from 'vue-gettext';
const {gettext: $gettext} = translate;
let str = $gettext('Hello'); Other benefit is for my use case: all translations are handled by easygettext and vue-gettext aside from a few nodejs scripts for non-Vue builds (legacy site). easygettext supports plain Javascript files so users need only a gettext API to get the translations from their translations.json. vue-gettext has that API but it cannot be exported without |
v2.1.9 available thanx @janlazo |
Tested it today. I forgot to update |
'this' is not defined when not installing the plugin on Vue. Fix Polyconseil#113 (comment)
This makes it possible to use vue-gettext without Vue.
Use case is to reuse vue-gettext for nodejs scripts
that need only the gettext API for translations.