I have a module that sets the language by requiring the right index.{i18n}.js
The code below works fine for pathes (urls) with /fr/, /en/, /es/...
module.exports = function (locals) {
var route = global.RoutesPages.routes.find(r => r.url == locals.path);
const language = locals.path.split('/')[1];
return Promise.resolve(
locals.template(
Object.assign(
{},
{
title: route.title,
type: route.type
},
require(`../../i18n-build/index.${language}.js`)
)
)
)
};
So it requires for example.com/fr/cool
the file i18n-build/index.fr.js
, and it requires for example.com/en/cool
the file i18n-build/index.en.js
The issue is I have a case where the url is example.com/global and so the require tells me it can't find the right file because it tries to require for example.com/global/foo
the file i18n-build/index.global.js
which is to be expected because I don't this file and don't want to. Indeed for users accessing example.com/global, I want to use the i18n-build/index.en.js
with english as a "global" user default
I tried to use a condition inside the require but then the build fails:
module.exports = function (locals) {
var route = global.RoutesPages.routes.find(r => r.url == locals.path);
const language = locals.path.split('/')[1];
function isCountry() {
//detect if language in uri is "global",
//i.e not a specific country
if (language !== "global" ) {
return true;
}
}
return Promise.resolve(
locals.template(
Object.assign(
{},
{
title: route.title,
type: route.type
},
require( isCountry ? `../i18n-build/index.${language}.js` : `../i18n-build/index.en.js` )
)
)
)
};
It seems to break what was working before when webpack is building the files for one url with /fr/, I get the error:
Cannot find module '../i18n-build/index.fr.js'
And the whole build fails.
I think how I write it is incorrect but i don't know how to apply a condition which basically goes like this:
If (isCountry), require ../i18n-build/index.${language}.js
, else require ../i18n-build/index.en.js
EDIT
After some comments asking me to test various requires here are the results on the build :
require(
../../i18n-build/index.${language}.js )
=> build works
require(
../../i18n-build/index.fr.js )
=> build works
require( isCountry ?
../i18n-build/index.${language}.js :
../i18n-build/index.en.js )
=> build fails
require( isCountry ?
../i18n-build/index.fr.js :
../i18n-build/index.en.js )
=> build fails
require( isCountry ?
../../i18n-build/index.fr.js :
../../i18n-build/index.en.js )
=> build works!!
require( isCountry ?
../../i18n-build/index.${language}.js :
../../i18n-build/index.en.js )
=> build fails
The last one really suggest the issue is about how to properly create a condition on a require because both files do exist, i am not even using a variable ($language) inside them
Kindly change your language assignment line by this:
const language = locals.path.split('/')[1] == 'global' ? 'en' : locals.path.split('/')[1];
And then use the 1st code require .
require(`../../i18n-build/index.${language}.js`)
因此错误一定是拼写错误,请尝试在条件中添加../
more:
require( isCountry ?`../../i18n-build/index.${language}.js`:`../../i18n-build/index.en.js`)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.