I18n-module: cant read messages from .js files

Created on 25 Mar 2018  路  9Comments  路  Source: nuxt-community/i18n-module

i make en-Us.js file in lang folder and put this code
export default { "welcome" : "sdfs", account_description: 'Account description', }
but not loaded.
i found problem there
`/**

  • Asynchronously load messages from translation files
  • @param {VueI18n} i18n vue-i18n instance
  • @param {String} lang Language code to load
  • @return {Promise}
    /
    export function loadLanguageAsync (i18n, lang) {
    if (!i18n.loadedLanguages) {
    i18n.loadedLanguages = []
    }
    if (!i18n.loadedLanguages.includes(lang)) {
    const langOptions = i18n.locales.find(l => l.code === lang)
    if (langOptions) {
    const { langFile } = langOptions;
    return import(/
    webpackChunkName: "lang-[request]" */ '~/<%= options.langDir %>'+langFile)
    .then(msgs => {
    i18n.setLocaleMessage(lang, msgs)
    i18n.loadedLanguages.push(lang)
    return
    })
    }
    }
    return Promise.resolve()
    }`

in line
i18n.setLocaleMessage(lang, msgs)

to

i18n.setLocaleMessage(lang, msgs.default)

its true?

This question is available on Nuxt.js community (#c34)

Most helpful comment

@amazzoccone Location files can be js files as well! They need the correct export though (Example)

All 9 comments

Hi @roshangara !
At the moment, I'd recommend you use the "classic" export method by replacing export default { with module.exports = {. It should do the trick :)

I've tried module.exports in my lang file which integrates locale from element-ui but got an error like below

client.js:524 [nuxt] Error while initializing app TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
    at Object.<anonymous> (zh-CN.js:11)
    at Object../lang/zh-CN.js (zh-CN.js:21)
    at __webpack_require__ (bootstrap a35ddee16c4bfdf308d8:712)
    at fn (bootstrap a35ddee16c4bfdf308d8:117)
    at main.scss?4a09:21
    at <anonymous>

here's my lang file,

var elementLocale = require('element-ui/lib/locale/lang/zh-TW').default;

module.exports = Object.assign({}, elementLocale, {
  appName: 'appName',
  placeholder: {
    input: 'Please input {0}',
  },
});

change it to es2015 syntax solves the issue but result in an expected default key in my i18n messages object.

import elementLocale from 'element-ui/lib/locale/lang/zh-TW';

export default {
  ...elementLocale,
  appName: 'appName',
  placeholder: {
    input: 'Please input {0}',
  },
};

How could I get around this problem?

I ended up calling app.i18n.mergeLocaleMessage(locale, elementLocale) in onLanguageSwitched(So did vee-validate's dictionary localization). Since setLocaleMessage would be called after beforeLanguageSwitch if loadLanguagesAsync is turned on.

Hi @blooddrunk . I think it's a bug in the documentation of this package. Lang files should be .json.
In nuxt.config.js for example:

modules: [
      ['nuxt-i18n', {
        locales: [
          {
            code: 'en',
            iso: 'en-US',
            name: 'English',
            langFile: 'en-US.json'
          },
          {
            code: 'es',
            iso: 'es-ES',
            name: 'Espa帽ol',
            langFile: 'es-ES.json'
          }
        ],
        loadLanguagesAsync: true,
        langDir: 'lang/'
      }]
  ],

@amazzoccone Location files can be js files as well! They need the correct export though (Example)

I had a similar problem and thought I would share my gist: https://gist.github.com/toadkicker/99551a94d0922e43a18aea7c6169a62e

Needed to read in YAML files from the Rails backend, and then make them available to the Nuxt application..

I need to use nuxt and i18n to add different language support to my app, the problem is that I can have the json files with the lang locally, I need to do an api call to the back end and retrieve the json file, set it to the localStorage and them consume the lang from there. I have been struggling with this the whole day. And this keep throwing an error when trying to change the route to a different language: app.i18n.beforeLanguageSwitch is not a function

This should be fixed in current version as it takes into account possible export.default property:
https://github.com/nuxt-community/nuxt-i18n/blob/a6172ec0b2387c24424d50189fee015e20232a95/src/templates/utils.js#L23-L25

Closing for now

Was this page helpful?
0 / 5 - 0 ratings

Related issues

javialon26 picture javialon26  路  20Comments

ebeloded picture ebeloded  路  15Comments

lucassith picture lucassith  路  28Comments

varna picture varna  路  14Comments

pi0 picture pi0  路  18Comments