I18n-module: langDir does not support absolute path

Created on 10 Jul 2020  路  7Comments  路  Source: nuxt-community/i18n-module

Version

nuxt-i18n: 6.13.1
nuxt: 2.13.3

Nuxt configuration

mode:

  • [x] universal
  • [] spa

Nuxt-i18n configuration

const { resolve } = require('path');
...
        ['nuxt-i18n', {
            lazy: true
            langDir: resolve(__dirname, 'lang'),
        }]

-->

Steps to reproduce

I am trying to use lazy load on windows, but setting langDir to some diroctory with full path it will generate options.js:

...
export const langDir = 'C:\web\work\zeoshop\client\lang'
...

What is Expected?

backslash should be prefixed:

...
export const langDir = 'C:\\web\\work\\zeoshop\\client\\lang'
...

What is actually happening?

ERROR  in ./.nuxt/nuxt-i18n/options.js

Syntax Error: Unterminated string constant (9:23)

   7 | export const strategy = 'prefix_except_default'
   8 | export const lazy = true
>  9 | export const langDir = 'C:\web\work\zeoshop\client\lang\'

edit

or probably i do something bad, because it is forcing '~/<%= options.langDir %>'.. what is ~ directory? why is it forced?

edit 2

we have translable translation in admin area, so probably locale's file should be http://... ? to load newest translations every time by http/https protocol? but this is change request not bug (like this issue)

feature-request pinned 馃搶

Most helpful comment

we have translations editable in admin area.. so that JSONs are changeable in runtime, so we need to load actual json on every load (have some small cache time on server)
so I need to get around building translations messages with webpack

All 7 comments

langDir should be relative to srcDir.

I suppose this can be improved to work with absolute paths too.

yeah.. but we removed ~ alias to srcDir (in build.extend), we only use @.. because ~ is very buggy in less and importing node_modules styles (less have ~ alias to node_modules, and we want to use it)..
so yeah langDir should be JSON.stringify (for prefixes backslash)
and maybe not forcing to ~/... but use path.resolve with srcDir?

@padinko don't you think you are over-complicating it? Please see this example for how you can get it working without confusion on how to use paths, also works on all machines, keep all files within your project and not on the C drive in some random location.

i18n: {
    detectBrowserLanguage: {
      useCookie: true,
      cookieKey: 'i18n_redirected'
    },
    locales: [
      {
        code: 'en-GB',
        file: 'en-GB.json',
        moment: 'en'
      },
      {
        code: 'en-CA',
        file: 'en-CA.json',
        moment: 'en'
      },
      {
        code: 'fr-CA',
        file: 'fr-CA.json',
        moment: 'fr'
      },
      {
        code: 'en-SA',
        file: 'en-SA.json',
        moment: 'en'
      },
      {
        code: 'en-IE',
        file: 'en-IE.json',
        moment: 'en'
      },
      {
        code: 'hi-IN',
        file: 'hi-IN.json',
        moment: 'in'
      },
      {
        code: 'en-IN',
        file: 'en-IN.json',
        moment: 'en'
      }
    ],
    defaultLocale: 'en-GB',
    lazy: true,
    langDir: 'locales/',
    vueI18n: {
      fallbackLocale: 'en-GB',
      messages: {
        'en-GB': require('./locales/en-GB'),
        'fr-CA': require('./locales/fr-CA'),
        'en-CA': require('./locales/en-CA'),
        'en-IE': require('./locales/en-IE'),
        'en-SA': require('./locales/en-SA'),
        'hi-IN': require('./locales/hi-IN'),
        'en-IN': require('./locales/en-IN')
      }
    }
  },

we have translations editable in admin area.. so that JSONs are changeable in runtime, so we need to load actual json on every load (have some small cache time on server)
so I need to get around building translations messages with webpack

@padinko i see, that makes sense for the usecase then!

we have translable translation in admin area, so probably locale's file should be http://... ? to load newest translations every time by http/https protocol? but this is change request not bug (like this issue)

No, it can't and won't support http://.... Instead, you can assign a javascript file to the file property, that exports a function and you can have any custom logic you want there, including loading translations from a remote endpoint. See https://i18n.nuxtjs.org/lazy-load-translations.html for an example.

I'm treating this bug as being about supporting absolute paths in langDir and nothing more.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adetbekov picture adetbekov  路  21Comments

pi0 picture pi0  路  18Comments

Josepdal picture Josepdal  路  17Comments

johnboylesingfield picture johnboylesingfield  路  17Comments

javialon26 picture javialon26  路  20Comments