最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

在Azure函数中初始化i18next

IT培训 admin 14浏览 0评论

在Azure函数中初始化i18next

我有一个天蓝色的http触发函数,该函数由客户端调用,其中包含一些数据和客户端的区域性字符串,例如“ en-US”。 index.js处理azure请求。 myWorker.js具有doStuff()函数,该函数准备数据以返回到客户端。我正在使用i18next进行本地化。

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

index.js如何将文化字符串传递给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
    }
}
回答如下:

使用sync-fs-backend,就像这样:

// i18n.js
const { join } = require('path')
const { readdirSync, lstatSync } = require('fs')
const i18next = require('i18next')
const SyncBackend = require('i18next-sync-fs-backend')
i18next
  .use(SyncBackend)
  .init({
    // debug: true,
    initImmediate: false,
    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')

在Azure函数中初始化i18next

我有一个天蓝色的http触发函数,该函数由客户端调用,其中包含一些数据和客户端的区域性字符串,例如“ en-US”。 index.js处理azure请求。 myWorker.js具有doStuff()函数,该函数准备数据以返回到客户端。我正在使用i18next进行本地化。

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

index.js如何将文化字符串传递给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
    }
}
回答如下:

使用sync-fs-backend,就像这样:

// i18n.js
const { join } = require('path')
const { readdirSync, lstatSync } = require('fs')
const i18next = require('i18next')
const SyncBackend = require('i18next-sync-fs-backend')
i18next
  .use(SyncBackend)
  .init({
    // debug: true,
    initImmediate: false,
    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')

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论