using multiple translation files in aurelia i18N

2019-06-22 20:41发布

问题:

I have a working app using aurelia-i18n. I would like to split translation.json file into multiple files like nav.json, message.json, etc but I am not sure how to do it.
This is how it looks right now.

locale
  |-en
     |- translation.json

But I want to change it to this way.

locale
  |-en
     |- nav.json
     |- message.json

Is it possible to do it? If so, how do I configure it and access values in each file?

回答1:

You can have multiple resource files and these are called namespaces in the i18next library (by default you only have one namespace which is called: translation) which is used by aurelia i18N.

You just need to list your namespaces when configuring the plugin with the namespaces and defaultNs properties inside the ns option:

.plugin('aurelia-i18n', (instance) => {
        // adapt options to your needs (see http://i18next.com/pages/doc_init.html)
        instance.setup({
          resGetPath : 'locale/__lng__/__ns__.json',
          lng : 'de',
          attributes : ['t','i18n'],
          ns: { 
             namespaces: ['nav', 'message'], 
             defaultNs: 'message'
          },
          getAsync : true,
          sendMissing : false,
          fallbackLng : 'en',
          debug : false
        });
      });

See also the documentation of i18next and this related github issue: Using namespaces