Skip to content
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

Merged
merged 2 commits into from
Jul 3, 2020
Merged

Conversation

janlazo
Copy link
Contributor

@janlazo janlazo commented Jun 27, 2020

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.

@@ -179,3 +179,176 @@ describe('Translate tests', () => {
})

})

describe('Translate tests without Vue', () => {
Copy link
Contributor Author

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.

@kemar
Copy link
Contributor

kemar commented Jun 29, 2020

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?

@janlazo
Copy link
Contributor Author

janlazo commented Jun 29, 2020

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 Vue dependency in nodejs scripts.

@kemar
Copy link
Contributor

kemar commented Jun 29, 2020

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'
Copy link
Contributor

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.
@janlazo janlazo force-pushed the decouple-translate branch from 7faa2b4 to a17fa96 Compare June 30, 2020 02:39
@kemar
Copy link
Contributor

kemar commented Jul 1, 2020

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.

@janlazo
Copy link
Contributor Author

janlazo commented Jul 1, 2020

How I would use is what's done in the tests: swap out Vue.use(plugin) with translate.initTranslations. I need to export translate but there is no named export for it.

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

@kemar
Copy link
Contributor

kemar commented Jul 2, 2020

@janlazo How would you explain the benefits of your PR to other users of vue-gettext?

@janlazo
Copy link
Contributor Author

janlazo commented Jul 2, 2020

vue-gettext users can import translate directly instead of relying on Vue.prototype for nodejs files outside of Vue components but included in their build. It makes it more obvious where the $gettext is coming from. Aliasing is required because of easygettext but it's a one-liner adjustment.

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 Vue. My PR exposes that API so their nodejs scripts have 1 less runtime dependency to deal with.

@kemar kemar merged commit 8e8e286 into Polyconseil:master Jul 3, 2020
@kemar
Copy link
Contributor

kemar commented Jul 3, 2020

v2.1.9 available thanx @janlazo

@janlazo
Copy link
Contributor Author

janlazo commented Jul 6, 2020

Tested it today. I forgot to update translate.js to not use this within gettext() because gettext is an alias to an internal function. Easy to fix.

@janlazo janlazo deleted the decouple-translate branch July 6, 2020 22:28
janlazo added a commit to janlazo/vue-gettext that referenced this pull request Jul 6, 2020
'this' is not defined when not installing the plugin on Vue.

Fix Polyconseil#113 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants