我不知道即时通讯使用i18n库是否错误,但是我很确定即时通讯操作正确。
我一直在尝试更改语言环境。这是我当前的代码。如您所见,我在glocal i81n变量上调用了setLocale,但是它仍然打印出en
我试图做res.setLocale(req.args.lang);在我的控制器中,但仍会打印出默认的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
答案 0 :(得分:0)
我是偶然发现的。
这确实应该在文档中,但我将其发布在此处,以便其他遇到此问题的可怜人可以尝试一下。
之所以无法执行任何操作,是因为我的.json文件带有逗号结尾。因此,它不是有效的.json文件。
我花了两天时间才偶然发现了这个。如果.json文件无效,则i18n应该在控制台中引发错误!!!
无论如何,我删除了结尾的逗号,并且一切正常。