|
184 | 184 |
|
185 | 185 | };
|
186 | 186 |
|
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 |
| - |
205 | 187 | var SPACING_RE = /\s{2,}/g;
|
| 188 | + var _config = { |
| 189 | + language: '', |
| 190 | + getTextPluginSilent: false, |
| 191 | + getTextPluginMuteLanguages: [], |
| 192 | + silent: false, |
| 193 | + }; |
| 194 | + var _translations = {}; |
206 | 195 |
|
207 | 196 | var translate = {
|
208 | 197 |
|
|
221 | 210 | if ( n === void 0 ) n = 1;
|
222 | 211 | if ( context === void 0 ) context = null;
|
223 | 212 | if ( defaultPlural === void 0 ) defaultPlural = null;
|
224 |
| - if ( language === void 0 ) language = _Vue.config.language; |
| 213 | + if ( language === void 0 ) language = _config.language; |
225 | 214 |
|
226 | 215 |
|
227 | 216 | if (!msgid) {
|
228 | 217 | return '' // Allow empty strings.
|
229 | 218 | }
|
230 | 219 |
|
231 |
| - var silent = _Vue.config.getTextPluginSilent || (_Vue.config.getTextPluginMuteLanguages.indexOf(language) !== -1); |
| 220 | + var silent = _config.getTextPluginSilent || (_config.getTextPluginMuteLanguages.indexOf(language) !== -1); |
232 | 221 |
|
233 | 222 | // Default untranslated string, singular or plural.
|
234 | 223 | var untranslated = defaultPlural && plurals.getTranslationIndex(language, n) > 0 ? defaultPlural : msgid;
|
|
239 | 228 | // See the `Language` section in https://www.gnu.org/software/gettext/manual/html_node/Header-Entry.html
|
240 | 229 | // So try `ll_CC` first, or the `ll` abbreviation which can be three-letter sometimes:
|
241 | 230 | // 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]]; |
243 | 232 |
|
244 | 233 | if (!translations) {
|
245 | 234 | if (!silent) {
|
|
363 | 352 | return this.getTranslation(msgid, n, context, plural)
|
364 | 353 | },
|
365 | 354 |
|
| 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 | + }, |
366 | 371 | };
|
367 | 372 |
|
368 | 373 | // UUID v4 generator (RFC4122 compliant).
|
|
387 | 392 |
|
388 | 393 | }
|
389 | 394 |
|
| 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 | + |
390 | 413 | /**
|
391 | 414 | * Translate content according to the current language.
|
392 | 415 | */
|
|
841 | 864 |
|
842 | 865 | Config(Vue, languageVm, options.silent, options.autoAddKeyAttributes, options.muteLanguages);
|
843 | 866 |
|
| 867 | + translate.initTranslations(options.translations, Vue.config); |
| 868 | + |
844 | 869 | // Makes <translate> available as a global component.
|
845 | 870 | Vue.component('translate', Component);
|
846 | 871 |
|
|
0 commit comments