diff --git a/src/core/hooks.js b/src/core/hooks.js index bdd68184a..566cf2574 100644 --- a/src/core/hooks.js +++ b/src/core/hooks.js @@ -29,12 +29,6 @@ export function createExtendRoutesHook (moduleContainer, options) { } export function buildHook (moduleContainer, options) { - if (options.langDir) { - if (!options.locales.length || typeof options.locales[0] === 'string') { - console.error('[' + MODULE_NAME + '] When using "langDir" option, the "locales" option must be a list of objects') - } - } - if (options.strategy === STRATEGIES.NO_PREFIX && options.differentDomains) { // eslint-disable-next-line no-console console.warn('[' + MODULE_NAME + '] The `differentDomains` option and `no_prefix` strategy are not compatible. Change strategy or disable `differentDomains` option.') diff --git a/src/index.js b/src/index.js index e72d4a07b..7caf21d66 100644 --- a/src/index.js +++ b/src/index.js @@ -21,8 +21,18 @@ export default function (userOptions) { return } - // Resolve langDir - if (options.langDir) { + if (options.lazy) { + if (!options.langDir) { + throw new Error(`[${MODULE_NAME}] When using the "lazy" option you must also set the "langDir" option.`) + } + if (!options.locales.length || typeof options.locales[0] === 'string') { + throw new Error(`[${MODULE_NAME}] When using the "langDir" option the "locales" option must be a list of objects.`) + } + for (const locale of options.locales) { + if (!locale[LOCALE_FILE_KEY]) { + throw new Error(`[${MODULE_NAME}] All locale objects must have the "file" property set when using "lazy".\nFound none in:\n${JSON.stringify(locale, null, 2)}.`) + } + } options.langDir = this.nuxt.resolver.resolveAlias(options.langDir) } diff --git a/src/templates/options.js b/src/templates/options.js index c0f91341b..8ae89a38d 100644 --- a/src/templates/options.js +++ b/src/templates/options.js @@ -18,7 +18,7 @@ for (const [key, value] of Object.entries(options)) { } %> -<% if (options.langDir) { %> +<% if (options.lazy && options.langDir) { %> export const ASYNC_LOCALES = { <%= Array.from( new Set(options.locales.map(l => `'${l.file}': () => import('../${relativeToBuild(options.langDir, l.file)}' /* webpackChunkName: "lang-${l.file}" */)`)) diff --git a/src/templates/utils.js b/src/templates/utils.js index 084b31e2e..0e19695ce 100755 --- a/src/templates/utils.js +++ b/src/templates/utils.js @@ -1,7 +1,7 @@ import { LOCALE_CODE_KEY, LOCALE_FILE_KEY, - MODULE_NAME/* <% if (options.langDir) { %> */, + MODULE_NAME/* <% if (options.lazy && options.langDir) { %> */, ASYNC_LOCALES/* <% } %> */ } from './options' @@ -22,7 +22,7 @@ export async function loadLanguageAsync (context, locale) { if (localeObject) { const file = localeObject[LOCALE_FILE_KEY] if (file) { - /* <% if (options.langDir) { %> */ + /* <% if (options.lazy && options.langDir) { %> */ let messages if (process.client) { const { nuxtState } = context