gpt4 book ai didi

javascript - i18n node.js setLocale 不工作! getLocale 始终返回默认语言环境

转载 作者:行者123 更新时间:2023-12-05 02:58:45 25 4
gpt4 key购买 nike

我不知道我是否使用了 i18n 库错误,但我很确定我做对了。

我一直在尝试更改语言环境。这是我当前的代码。如您所见,我在 glocal i81n 变量上调用了 setLocale,但它仍然打印出 en

我试过做 res.setLocale(req.args.lang);在我的 Controller 中,但它仍然打印出 en 的默认语言。

老实说,我不知道是这个库坏了还是我做的完全错了。请帮忙!

/**
* Main Express Application: set up express app
*/

'use strict';

// require third-party node modules
const express = require('express');
const sslRedirect = require('heroku-ssl-redirect');
const compression = require('compression');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const passport = require('passport');
const helmet = require('helmet');
const http = require('http');
const cors = require('cors'); // handle cors
const i18n = require('i18n'); // set up language

// env variables
const { NODE_ENV, COOKIE, REDIS_URL, REDIS_HOST, REDIS_PORT } = process.env;

// server
function server() {
// require custom
const models = require('./models'); // establish and grab db connection
const cfgPassport = require('./services/passport'); // configuration for passport

// require custom middleware
const args = require('./middleware/args');
const error = require('./middleware/error');
const exit = require('./middleware/exit');
const { attachJWTAuth } = require('./middleware/auth');

// set up express app
const app = express();
const newServer = http.createServer(app);
const io = require('socket.io')(newServer); // socket.io


// set up redis with socket
const redis = require('socket.io-redis');
const socket = require('./services/socket'); // require socket service to initiate socket.io
io.adapter(redis(REDIS_URL));

// enable ssl redirect in production
app.use(sslRedirect());

// only log requests in development
NODE_ENV === 'development' ? app.use(require('morgan')('dev')) : null;

// add middleware and they must be in order
app.use(compression()); // GZIP all assets
app.use(cors()); // handle cors
app.use(helmet()); // protect against vulnerabilities
// app.use(rawBody); // adds rawBody to req object

// set up language
i18n.configure({
locales: ['en', 'ko'], // set the languages here
defaultLocale: 'en',
queryParameter: 'lang', // query parameter to switch locale (ie. /home?lang=ch) - defaults to NULL
cookie: COOKIE,
directory: __dirname + '/locales'
});

// you will need to use cookieParser to expose cookies to req.cookies
app.use(cookieParser());

// i18n init parses req for language headers, cookies, etc.
// NOTE: If user is logged in, locale is set in verifyJWTAuth method
app.use(i18n.init);
i18n.setLocale('ko');
console.log(i18n.getLocale());

// save raw body
function rawBodySaver(req, res, buf, encoding) {
if (buf && buf.length) req.rawBody = buf.toString(encoding || 'utf8');
}

// body parser
app.use(bodyParser.json({ limit: '32mb', verify: rawBodySaver })); // raw application/json
app.use(bodyParser.urlencoded({ limit: '32mb', verify: rawBodySaver, extended: false })); // application/x-www-form-urlencoded

// NOTE: take this out because it interferes with multer
// app.use(bodyParser.raw({ limit: '32mb', verify: rawBodySaver, type: () => true }));

// only secure in production
if (NODE_ENV === 'production') app.set('trust proxy', 1); // get ip address using req.ip

// passport config, must be in this order!
app.use(passport.initialize());
cfgPassport(passport); // set up passport

// custom middleware
app.use(args.attach); // set req.args
app.use(attachJWTAuth(passport));
app.use(exit.middleware); // stops here if server is in the middle of shutting down

// host public files
// app.use(express.static(__dirname + '/public'));

// set up routes
const router = require('./routes')(passport); // grab routes
app.use('/', router); // place routes here

// error middleware MUST GO LAST
app.use(error);

// io connection, call socket.connect
io.on('connection', socket.connect);

return newServer; // return newServer
}

module.exports = server(); // return server app for testing

最佳答案

我偶然发现的。

这确实应该在文档中,但我会在此处发布,以便遇到此问题的其他可怜人可以尝试一下。

没有任何效果的原因是因为我的 .json 文件有一个尾随逗号。因此它不是有效的 .json 文件。

我花了两天时间无意中发现了这一点。如果 .json 文件无效,i18n 应该在控制台中抛出错误!!!

无论如何,我删除了尾随的逗号,一切正常。

关于javascript - i18n node.js setLocale 不工作! getLocale 始终返回默认语言环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58673349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com