根据 req.headers['accept-language'] 导入动态文件夹
Import dynamic folder depending on req.headers['accept-language']
我正在尝试实现一项新功能 - 使用 Nodejs 发送多语言邮件。
我有这样的目录结构:
mail-templates
__index.js
__jp
____index.js
____mail1.js
____mail2.js
__en
____index.js
____mail1.js
____mail2.js
在en
和jp
的index
中,我将导入和导出当前文件夹中的所有文件
在mail-teamplates
的index
中,我想像这样根据req.headers['accept-language']动态导入文件夹:
import * as Mail from `./${variable}` // variable are en or jp depending on accept-language
我的问题:我该怎么做?如何在此处获取接受语言以动态导入文件夹?
您需要在请求处理函数中要求模块。
如果使用快速服务器,您可以尝试这样的操作。
app.get("/", async(req, res) => {
const language = req.headers["language"] || "en";
const module = `./${language}.js`;
const greet = require(module);
res.json(greet());
}
)
REPL link。
https://repl.it/repls/UsedSelfishVisitor
您可以 运行 下面的代码片段来检查基于 language
header
的响应
//Fetching data using laguage: es
fetch("https://UsedSelfishVisitor--five-nine.repl.co", {
method:"GET",
headers: {
language: "es"
}
}).then(res => res.json()).then(data => console.log(data));
//Fetching data using language: en
fetch("https://UsedSelfishVisitor--five-nine.repl.co", {
method:"GET",
headers: {
language: "en"
}
}).then(res => res.json()).then(data => console.log(data));
不建议在 http 回调中执行此操作。
针对您的问题的最佳解决方案是导入所有可用语言,并为每个请求使用首选语言。
示例:
在你的mail-templates/index.js
中:
import * as en from './en';
import * as es from './es';
const defaultLanguage = 'en';
const availableLanguages = { en, es };
function getMailByLanguage(language) {
return availableLanguages[language] || availableLanguages[defaultLanguage];
}
module.exports = getMailByLanguage;
当你想使用它时,只需这样做:
import * as MailTemplates from './mail-templates';
app.get("/", (req, res) => {
const language = req.headers["language"];
const Mail = MailTemplates.getMailByLanguage(language);
// Do your stuff's here
...
});
我正在尝试实现一项新功能 - 使用 Nodejs 发送多语言邮件。
我有这样的目录结构:
mail-templates
__index.js
__jp
____index.js
____mail1.js
____mail2.js
__en
____index.js
____mail1.js
____mail2.js
在en
和jp
的index
中,我将导入和导出当前文件夹中的所有文件
在mail-teamplates
的index
中,我想像这样根据req.headers['accept-language']动态导入文件夹:
import * as Mail from `./${variable}` // variable are en or jp depending on accept-language
我的问题:我该怎么做?如何在此处获取接受语言以动态导入文件夹?
您需要在请求处理函数中要求模块。
如果使用快速服务器,您可以尝试这样的操作。
app.get("/", async(req, res) => {
const language = req.headers["language"] || "en";
const module = `./${language}.js`;
const greet = require(module);
res.json(greet());
}
)
REPL link。 https://repl.it/repls/UsedSelfishVisitor
您可以 运行 下面的代码片段来检查基于 language
header
//Fetching data using laguage: es
fetch("https://UsedSelfishVisitor--five-nine.repl.co", {
method:"GET",
headers: {
language: "es"
}
}).then(res => res.json()).then(data => console.log(data));
//Fetching data using language: en
fetch("https://UsedSelfishVisitor--five-nine.repl.co", {
method:"GET",
headers: {
language: "en"
}
}).then(res => res.json()).then(data => console.log(data));
不建议在 http 回调中执行此操作。 针对您的问题的最佳解决方案是导入所有可用语言,并为每个请求使用首选语言。
示例:
在你的mail-templates/index.js
中:
import * as en from './en';
import * as es from './es';
const defaultLanguage = 'en';
const availableLanguages = { en, es };
function getMailByLanguage(language) {
return availableLanguages[language] || availableLanguages[defaultLanguage];
}
module.exports = getMailByLanguage;
当你想使用它时,只需这样做:
import * as MailTemplates from './mail-templates';
app.get("/", (req, res) => {
const language = req.headers["language"];
const Mail = MailTemplates.getMailByLanguage(language);
// Do your stuff's here
...
});