Namespaces are a feature in i18next internationalization framework which allows you to separate translations that get loaded into multiple files.
While in a smaller project it might be reasonable to just put everything in one file you might get at a point where you want to break translations into multiple files. Reasons might be:
We recommend you to create multiple smaller namespaces.
Often you wish to separate some segments out because they belong together. We do this in most of our projects, eg.:
More often you don't want to load all the translations upfront or at least reduce the amount loaded. This reason often goes hand in hand with the one translation file gets to large and you start loosing the overview scrolling through hundred of text fragments.
i18next.init({
ns: ['common', 'moduleA', 'moduleB'],
defaultNS: 'moduleA'
}, (err, t) => {
i18next.t('myKey'); // key in moduleA namespace (defined default)
i18next.t('myKey', { ns: 'common' }); // key in common namespace
i18next.t('common:myKey'); // key in common namespace
});
// load additional namespaces after initialization
i18next.loadNamespaces('myNamespace', (err, t) => { /* ... */ });
There are advanced options like:
more details: locize / i18next
locizify.init({
namespace: 'myNamespace'
ns: ['common', 'myNamespace'] // -> add additional namespaces to load
});
You can access a different namespace by setting the attribute i18next-options:
<div i18next-options='{"ns": "common"}'>
<p>different namespace common is used</p>
<p>all the way down</p>
</div>
Optionally you can set locizify to create a namespace per location/route.
locizify.init({
namespaceFromPath: true
});
more details: locizify
Last updated 1 year ago