如何将i18next与Parse Server一起使用?

时间:2018-11-14 13:41:39

标签: node.js npm parse-platform parse-server i18next

我尝试将i18next与在Heroku上运行的解析服务器一起使用。

package.json中,我添加了:

"dependencies": {
    <...>
    "i18next": "~12.0.x",
    "i18next-node-remote-backend": "~1.0.0"
}

cloud所在的main.js目录中,添加了strings.json(这是一个 i18next多语言 JSON文件,即所有翻译在一个文件中):

{
    "en": {
        "translation": {
            "astring": "The string in English.",
        }
    },
    "es": {
        "translation": {
            "astring": "El string en enspañol.",
        }
    }
}

main.js的云代码功能中,我添加了:

var i18n = require("i18next");
var backend = require("i18next-node-remote-backend");

Parse.Promise.as()
.then(function() {

    i18n
    .use(backend)
    .init({
        "lng": "en",
        "debug": true,
        "backend": {
            "loadPath": __dirname + '/strings.json',
            "allowMultiLoading": false
        }
    }, (err, t) => {
        if (err) return console.log('something went wrong loading', err);
        console.log("i18n: "+ t('astring'));
        return Parse.Promise.as();
    });    
})
.then(
    <...>
);

但是在调用云代码函数时会引发错误:

Error: Invalid URI "/app/cloud/strings.json"
    at Request.init (/app/node_modules/request/request.js:274:31)
    at new Request (/app/node_modules/request/request.js:128:8)
    at request (/app/node_modules/request/index.js:53:10)
    at ajax (/app/node_modules/i18next-node-remote-backend/lib/index.js:27:30)
    at Backend.loadUrl (/app/node_modules/i18next-node-remote-backend/lib/index.js:91:7)
    at Backend.read (/app/node_modules/i18next-node-remote-backend/lib/index.js:86:12)
    at Connector.read (/app/node_modules/i18next/dist/commonjs/BackendConnector.js:186:32)
    at Connector.loadOne (/app/node_modules/i18next/dist/commonjs/BackendConnector.js:243:10)
    at /app/node_modules/i18next/dist/commonjs/BackendConnector.js:221:14
    at Array.forEach (<anonymous>)

"loadPath": './strings.json'也会发生错误。

这是怎么了?

0 个答案:

没有答案