Reputation: 456
I dont know if im using i18n library wrong but I am pretty sure im doing it right.
I have been trying to change the locale. Here is my current code. As you can see I call setLocale on the glocal i81n variable but it still prints out en
I have tried to do res.setLocale(req.args.lang); in my controller BUT IT STILL PRINTS OUT the default language of en.
I honestly don't know if this library is just broken or I'm doing this completely wrong. Please help!
/**
* 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
Upvotes: 3
Views: 4531
Reputation: 456
I figured it out by accident.
This really should be in the documentation but I will post it here so that other poor souls who ran into this issue can try this out.
The reason why nothing was working WAS BECAUSE my .json file had a trailing comma. Therefore it wasn't a valid .json file.
It took me two days to accidentally discover this. i18n SHOULD HAVE THROW AN ERROR in the console if the .json files are invalid!!!
Anyways, I removed the trailing comma and everything just worked.
Upvotes: 4