Javascript 初始化Azure函数中的i18next
我有一个azure http触发器函数,该函数由带有一些数据和客户端的区域性字符串(例如en US)的客户端调用。index.js处理azure请求。myWorker.js有一个doStuff函数,用于准备返回到客户端的数据。我正在使用i18next进行本地化 index.js需要将区域性字符串传递给i18next。将i18next放在myWorker.js中似乎是合乎逻辑的,但它需要在index.js调用doStuff函数之前加载。原谅我,如果这是一个混乱,但我是新的节点,不知道最好的方式来设置它 index.js如何将区域性字符串传递给myworker.js,等待i18next加载,将主数据传递给dostuff并使用context.done完成Javascript 初始化Azure函数中的i18next,javascript,node.js,azure,Javascript,Node.js,Azure,我有一个azure http触发器函数,该函数由带有一些数据和客户端的区域性字符串(例如en US)的客户端调用。index.js处理azure请求。myWorker.js有一个doStuff函数,用于准备返回到客户端的数据。我正在使用i18next进行本地化 index.js需要将区域性字符串传递给i18next。将i18next放在myWorker.js中似乎是合乎逻辑的,但它需要在index.js调用doStuff函数之前加载。原谅我,如果这是一个混乱,但我是新的节点,不知道最好的方式来设
index.js
module.exports = async function (context, req) {
switch (req.body.data.action) {
case 'doWork':
let myworker = require('./myWorker')(req.body.data.culture) //culture string for i18next
let retval = myworker.dostuff(); //i18next fails as it isn't loaded yet.
context.res = {
status: 200, body: {someData: retval}
};
break;
case 'anotherCommand':
....
}
context.done();
}
myWorker.js
let i18next = require("i18next");
let backend = require("i18next-node-fs-backend");
function dostuff() {
calc some stuff using i18next.t(key);
}
function setup_i18next(cultr) {
i18next
.use(backend)
.init({
fallbackLng: 'en',
lng: cultr,
backend: {
loadPath: 'locales/{{lng}}/{{ns}}.json'
},
ns: ['myspace1', 'myspace2']
})
.then(function (t) {
????
});
}
module.exports = function(cultre) {
setup_i18next(cultre);
return {
dostuff
}
}
使用fs后端,如下所示:
// i18n.js
const { join } = require('path')
const { readdirSync, lstatSync } = require('fs')
const i18next = require('i18next')
const Backend = require('i18next-fs-backend')
i18next
.use(Backend)
.init({
// debug: true,
initImmediate: false, // setting initImediate to false, will load the resources synchronously
fallbackLng: 'en',
lng: 'en',
preload: readdirSync(join(__dirname, '../locales')).filter((fileName) => {
const joinedPath = join(join(__dirname, '../locales'), fileName)
const isDirectory = lstatSync(joinedPath).isDirectory()
return isDirectory
}),
ns: 'backend-app',
defaultNS: 'backend-app',
backend: {
loadPath: join(__dirname, '../locales/{{lng}}/{{ns}}.json')
}
})
module.exports = (lng) => i18next.getFixedT(lng || 'en')
const t = require('../i18n')(lng)
const title = t('invitation.subject')
那么就这样使用它:
// i18n.js
const { join } = require('path')
const { readdirSync, lstatSync } = require('fs')
const i18next = require('i18next')
const Backend = require('i18next-fs-backend')
i18next
.use(Backend)
.init({
// debug: true,
initImmediate: false, // setting initImediate to false, will load the resources synchronously
fallbackLng: 'en',
lng: 'en',
preload: readdirSync(join(__dirname, '../locales')).filter((fileName) => {
const joinedPath = join(join(__dirname, '../locales'), fileName)
const isDirectory = lstatSync(joinedPath).isDirectory()
return isDirectory
}),
ns: 'backend-app',
defaultNS: 'backend-app',
backend: {
loadPath: join(__dirname, '../locales/{{lng}}/{{ns}}.json')
}
})
module.exports = (lng) => i18next.getFixedT(lng || 'en')
const t = require('../i18n')(lng)
const title = t('invitation.subject')
基于@adrai的答案,我永远也想不到,我认为可以对它进行调整,以在init中指定语言。否则,如果通过lang指定了另一种语言,我们将不必要地加载“en”翻译
const { join } = require('path')
const { readdirSync, lstatSync } = require('fs')
const i18next = require('i18next')
const SyncBackend = require('i18next-sync-fs-backend')
module.exports = (lang) => {
i18next
.use(SyncBackend)
.init({
// debug: true,
initImmediate: false,
fallbackLng: 'en',
lng:lang,
preload: readdirSync(join(__dirname, '../locales')).filter((fileName) => {
const joinedPath = join(join(__dirname, '../locales'), fileName)
const isDirectory = lstatSync(joinedPath).isDirectory()
return isDirectory
}),
ns: ['k2locals','sfaDebugger'],
defaultNS: 'k2locals',
debug: true,
backend: {
loadPath: join(__dirname, '../locales/{{lng}}/{{ns}}.json')
}
})
return i18next.getFixedT(lang || 'en', 'k2locals')
}
你好,已经开始工作了-谢谢!但这是一个问题。在我进入tinvitation之前,打开调试。主题“我发现您的应用程序有问题。”。getFixedT应该设置我的语言,所以我不确定为什么它说语言是未定义的。这没什么大不了的…对不起,有一块丢了。。。。您需要设置lng选项,以防止上面的示例更新,这将在每次函数调用时初始化i18next。。。就像我在上面最后的评论中建议的那样设置lng道具