【问题标题】:Initialize i18next in Azure function在 Azure 函数中初始化 i18next
【发布时间】:2020-04-20 21:24:07
【问题描述】:

我有一个 azure http 触发函数,它由客户端调用,带有一些数据和客户端的文化字符串,例如“美国”。 index.js 处理 azure 请求。 myWorker.js 有一个函数 doStuff() 准备数据以返回给客户端。我正在使用 i18next 进行本地化。

index.js 需要将文化字符串传递给 i18next。将 i18next 放在 myWorker.js 中似乎是合乎逻辑的,但它需要在 index.js 调用 doStuff 函数之前加载。如果这是一团糟,请原谅我,但我是 node 新手,不知道设置它的最佳方法。

index.js 如何将culture string 传递给myworker.js,等待i18next 加载,将主要数据传递给dostuff() 并以context.done() 结束?

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
    }
}

【问题讨论】:

    标签: javascript node.js azure


    【解决方案1】:

    使用 fs-backend,像这样:

    // 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')
    

    【讨论】:

    • 您好,已经成功了 - 谢谢!而是一个问题。调试后,在我到达t("invitation.subject') 之前,我得到“您的应用程序有问题”。 getFixedT 应该设置我的语言,所以我不确定为什么它说语言未定义。它的工作原理没什么大不了的......
    • 对不起,有一个缺失的部分......你需要设置 lng 选项来防止这种情况(更新了上面的示例)
    【解决方案2】:

    基于@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')
    }
    

    【讨论】:

    • 这将在每个函数调用中初始化 i18next... 只需按照我上一条评论中的建议设置 lng 属性(上图)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 2019-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多