1

I have the following files:

// nuxt.config.js
import { locales } from './services/i18n'
...
    i18n: {
        lazy: true,
        langDir: '~/locales/',
        defaultLocale: 'en',
        detectBrowserLanguage: false,
        differentDomains: true,
        locales,
        vueI18n: {
            fallbackLocale: 'en'
        }
    },
    publicRuntimeConfig: {
        ...
        subDomain: process.env.SUB_DOMAIN,
    },
...

// services/i18n/index.js
export const locales = [
    {
        code: 'ar',
        iso: 'ar',
        file: 'ar.json',
        dir: 'rtl',
        domain: `${process.env.SUB_DOMAIN}.example.ae`,
        name: 'العَرَبِيَّة',
        enName: 'Arabic',
        defaultLanguage: true,
        languages: ['ar']
    },
    {
        code: 'bg',
        iso: 'bg',
        file: 'bg.json',
        dir: 'ltr',
        domain: `${process.env.SUB_DOMAIN}.example.bg`,
        name: 'Български',
        enName: 'Bulgarian',
        defaultLanguage: true,
        languages: ['bg']
    },
    ...
]

The problem is that process.env.SUB_DOMAIN seems to be undefined in /services/i18n/index.js, although it is set because the same variable is not undefined in nuxt.config.js. I know that nuxt is exposing the values of publicRuntimeConfig as $config, however, $config is not accessible in /services/i18n/index.js. It might work if I would move locales to nuxt.config.js, but I don't want to do that because it would worsen the readability of the config file.

So my question is what the best approach would be to get subdomain inside /services/i18n/index.js.

1 Answer 1

1

EDIT: A great answer was given on Nuxtjs' discussions by Alexander Lichter: https://github.com/nuxt/nuxt.js/discussions/9289#discussioncomment-729801

// nuxt.config.js
import { locales } from './services/i18n'
...
    i18n: {
        lazy: true,
        langDir: '~/locales/',
        defaultLocale: 'en',
        detectBrowserLanguage: false,
        differentDomains: true,
        locales: locales(process.env.SUB_DOMAIN),
        vueI18n: {
            fallbackLocale: 'en'
        }
    },
    publicRuntimeConfig: {
        ...
        subDomain: process.env.SUB_DOMAIN,
    },
...
// services/i18n/index.js
export const locales = domain => [
    {
        code: 'ar',
        iso: 'ar',
        file: 'ar.json',
        dir: 'rtl',
        domain: `${domain}.example.ae`,
        name: 'العَرَبِيَّة',
        enName: 'Arabic',
        defaultLanguage: true,
        languages: ['ar']
    },
    {
        code: 'bg',
        iso: 'bg',
        file: 'bg.json',
        dir: 'ltr',
        domain: `${domain}.example.bg`,
        name: 'Български',
        enName: 'Bulgarian',
        defaultLanguage: true,
        languages: ['bg']
    },
    ...
]
Sign up to request clarification or add additional context in comments.

4 Comments

Maybe try to ask this question on the new related github discussions here: github.com/nuxt/nuxt.js/discussions
How would you suggest doing this with the file you just want to share env with for example, I have a constant.js file export const BASE_URL = process.env.BASE_URL my process.env is undefined but in nuxt.config they are all there and I can use them across plugins, middlewares, and so on
@rrrm93 a full brand new question with some details would be welcome here!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.