Skip to content
This repository has been archived by the owner on May 19, 2022. It is now read-only.

Commit

Permalink
feat: component namespace
Browse files Browse the repository at this point in the history
One can overwrite the namespace name that is generated for the component:

- function `getComponentNamespace(vm)`

Should return: `{ namespace: ‘new-namespace’, loadNamespace: true}`
`namespace`: is the new namespace to use
`loadNamespace`: tells if the namespace should be loaded. Be aware that if the global options `loadComponentNamespace === false` then the namespace will not be loaded even if `loadNamespace === true`.
  • Loading branch information
claudiocro committed Oct 24, 2017
1 parent d66cd72 commit 399374f
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ export function install(_Vue) {
return key;
};

const getComponentNamespace = (vm) => {
const namespace = vm.$options.name || vm.$options._componentTag;
if (namespace) {
return {
namespace,
loadNamespace: true,
};
}

return {
namespace: `${Math.random()}`,
};
};

Vue.mixin({
computed: {
$t() {
Expand Down Expand Up @@ -55,35 +69,40 @@ export function install(_Vue) {
});
}

if (options.i18nOptions) {
const { lng = null, keyPrefix = null, messages } = this.$options.i18nOptions;
let { namespaces } = this.$options.i18nOptions;
namespaces = namespaces || this.$i18n.i18next.options.defaultNS;

if (typeof namespaces === 'string') namespaces = [namespaces];
namespacesToLoad = namespaces.concat(namespacesToLoad);
const namespacesToLoad = namespaces.concat([namespace]);

if (messages) {
inlineTranslations = deepmerge(inlineTranslations, messages);
}

this._i18nOptions = { lng, namespaces: namespacesToLoad, keyPrefix };
this.$i18n.i18next.loadNamespaces(namespaces);
} else if (options.parent && options.parent._i18nOptions) {
this._i18nOptions = options.parent._i18nOptions;
} else if (options.__i18n) {
this._i18nOptions = { namespaces: namespacesToLoad };
this._i18nOptions = { namespaces: [namespace] };
}

if (loadNamespace && this.$i18n.options.loadComponentNamespace) {
this.$i18n.i18next.loadNamespaces([namespace]);
}

const languages = Object.keys(inlineTranslations);
languages.forEach((lang) => {
this.$i18n.i18next.addResourceBundle(
lang,
namespace,
{ ...inlineTranslations[lang] },
true,
false);
lang,
namespace,
{ ...inlineTranslations[lang] },
true,
false,
);
});

this.$i18n.i18next.loadNamespaces(Array.from(new Set(namespacesToLoad)));
}
},
});
Expand Down

0 comments on commit 399374f

Please sign in to comment.