I also made a website and added a language change function there. Translations of words and texts are in files of the type ruTrans.json, enTrance.json and the like. This function works on my local server on my laptop, but after I uploaded the project to ispmanager hosting, when you visit the site and try to change the language, it changes, but immediately the site is updated and the language becomes Russian again.
I read on the Internet that the problem may be caching. I turned it off but it still doesn’t work. The server is written in node js.
const http = require('http');
const fs = require('fs');
const path = require('path');
const url = require('url');
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
if (parsedUrl.pathname === '/') {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html;charset=utf-8');
const data = fs.readFileSync(process.env.INDEX_PATH, 'utf8');
res.end(data);
} else if (parsedUrl.pathname.startsWith('/translates/') && parsedUrl.pathname.endsWith('.json')) {
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
const filePath = path.join(__dirname, parsedUrl.pathname.substring(1));
if (fs.existsSync(filePath)) {
const jsonData = fs.readFileSync(filePath, 'utf8');
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.end(jsonData);
} else {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/plain;charset=utf-8');
res.end('Not Found');
}
} else {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/plain;charset=utf-8');
res.end('Not Found');
}
});
if ("SOCKET" in process.env) {
const socket = process.env.SOCKET;
if (fs.existsSync(socket)) {
fs.unlinkSync(socket);
}
server.listen(socket, () => {
fs.chmodSync(socket,0660);
console.log(`Listening ${socket}`);
});
} else if ("PORT" in process.env) {
const hostname = process.env.INSTANCE_HOST;
const port = process.env.PORT;
server.listen(port, hostname, () => {
console.log(`Listening http://${hostname}:${port}/`);
});
}
2
Answers
You should use browser’s dev tools / network tabs to inspect the loading of your website.
More specifically, you should identify the last request which loaded the page again after user had change the language. Then, click on the request, go to "initiator" tab, you can find which piece of code/entity had sent the request.
Are you sure it’s not the client side code that is triggering a page reload.