Skip to content

Commit 7faa2b4

Browse files
committed
Decouple Vue from translate.js
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.
1 parent 883b4ab commit 7faa2b4

File tree

5 files changed

+248
-26
lines changed

5 files changed

+248
-26
lines changed

dist/vue-gettext.js

+46-21
Original file line numberDiff line numberDiff line change
@@ -184,25 +184,14 @@
184184

185185
};
186186

187-
// Ensure to always use the same Vue instance throughout the plugin.
188-
//
189-
// This was previously done in `index.js` using both named and default exports.
190-
// However, this currently must be kept in a separate file because we are using
191-
// Rollup to build the dist files and it has a drawback when using named and
192-
// default exports together, see:
193-
// https://github.com/rollup/rollup/blob/fca14d/src/utils/getExportMode.js#L27
194-
// https://github.com/rollup/rollup/wiki/JavaScript-API#exports
195-
//
196-
// If we had kept named and default exports in `index.js`, a user would have to
197-
// do something like this to access the default export: GetTextPlugin['default']
198-
199-
var _Vue;
200-
201-
function shareVueInstance (Vue) {
202-
_Vue = Vue;
203-
}
204-
205187
var SPACING_RE = /\s{2,}/g;
188+
var _config = {
189+
language: '',
190+
getTextPluginSilent: false,
191+
getTextPluginMuteLanguages: [],
192+
silent: false,
193+
};
194+
var _translations = {};
206195

207196
var translate = {
208197

@@ -221,14 +210,14 @@
221210
if ( n === void 0 ) n = 1;
222211
if ( context === void 0 ) context = null;
223212
if ( defaultPlural === void 0 ) defaultPlural = null;
224-
if ( language === void 0 ) language = _Vue.config.language;
213+
if ( language === void 0 ) language = _config.language;
225214

226215

227216
if (!msgid) {
228217
return '' // Allow empty strings.
229218
}
230219

231-
var silent = _Vue.config.getTextPluginSilent || (_Vue.config.getTextPluginMuteLanguages.indexOf(language) !== -1);
220+
var silent = _config.getTextPluginSilent || (_config.getTextPluginMuteLanguages.indexOf(language) !== -1);
232221

233222
// Default untranslated string, singular or plural.
234223
var untranslated = defaultPlural && plurals.getTranslationIndex(language, n) > 0 ? defaultPlural : msgid;
@@ -239,7 +228,7 @@
239228
// See the `Language` section in https://www.gnu.org/software/gettext/manual/html_node/Header-Entry.html
240229
// So try `ll_CC` first, or the `ll` abbreviation which can be three-letter sometimes:
241230
// https://www.gnu.org/software/gettext/manual/html_node/Language-Codes.html#Language-Codes
242-
var translations = _Vue.$translations[language] || _Vue.$translations[language.split('_')[0]];
231+
var translations = _translations[language] || _translations[language.split('_')[0]];
243232

244233
if (!translations) {
245234
if (!silent) {
@@ -363,6 +352,22 @@
363352
return this.getTranslation(msgid, n, context, plural)
364353
},
365354

355+
/*
356+
* Initialize local state for translations and configuration.
357+
* Required to decouple global 'Vue'
358+
*
359+
* @param {Object} translations - translations.json
360+
* @param {Object} config - Vue.config
361+
*
362+
*/
363+
initTranslations: function (translations, config) {
364+
if (translations && typeof translations === 'object') {
365+
_translations = translations;
366+
}
367+
if (config && typeof config === 'object') {
368+
_config = config;
369+
}
370+
},
366371
};
367372

368373
// UUID v4 generator (RFC4122 compliant).
@@ -387,6 +392,24 @@
387392

388393
}
389394

395+
// Ensure to always use the same Vue instance throughout the plugin.
396+
//
397+
// This was previously done in `index.js` using both named and default exports.
398+
// However, this currently must be kept in a separate file because we are using
399+
// Rollup to build the dist files and it has a drawback when using named and
400+
// default exports together, see:
401+
// https://github.com/rollup/rollup/blob/fca14d/src/utils/getExportMode.js#L27
402+
// https://github.com/rollup/rollup/wiki/JavaScript-API#exports
403+
//
404+
// If we had kept named and default exports in `index.js`, a user would have to
405+
// do something like this to access the default export: GetTextPlugin['default']
406+
407+
var _Vue;
408+
409+
function shareVueInstance (Vue) {
410+
_Vue = Vue;
411+
}
412+
390413
/**
391414
* Translate content according to the current language.
392415
*/
@@ -841,6 +864,8 @@
841864

842865
Config(Vue, languageVm, options.silent, options.autoAddKeyAttributes, options.muteLanguages);
843866

867+
translate.initTranslations(options.translations, Vue.config);
868+
844869
// Makes <translate> available as a global component.
845870
Vue.component('translate', Component);
846871

0 commit comments

Comments
 (0)