-
-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
language detection does not work as expected #201
Comments
you can't use use |
I tried that as well, and the output of the line |
looks, like you calling |
beside that ... don't call changeLanguage of i18next - but the one you get in request... just keep in mind: concurrent request - using singleton is bad - only use what is on req. |
Sure and Thank you. |
well, seeing the key instead of the value is a clear sign of it does not exist (neither in detected nor in fallback language)...you will have to dig deeper as I can't...sorry... |
you also might try |
I tired the above one as well... here is the output of all i18next.t('unauthorized') : User is not authorized to access Am I missing anything... When should we defines the I18Next .. I did it globally in app.js of express |
can you make a minimal sample for reproduction...honestly...I can't follow this by all the small snipplets pasted... |
sure.. will make a sample and send it |
I created a simple app, to check the language switch. |
did following changes: var express = require('express');
var request = require('request');
var i18next = require('i18next');
var i18nextMiddleware = require("i18next-express-middleware");
var i18NodefsBackend = require('i18next-node-fs-backend');
i18next
.use(i18nextMiddleware.LanguageDetector)
.use(i18NodefsBackend)
.init({
debug: true,
preload: ['de','en','zh'],
fallbackLng: ['en'],
ns: ['translation'],
backend: {
loadPath: __dirname + "/locales/{{lng}}/{{ns}}.json"
}
});
var app = express();
module.exports.app = app;
app.use(i18nextMiddleware.handle(i18next));
app.get('/i18test', function (req, res) {
//i18next.changeLanguage(req.headers["accept-language"]);
console.log("i18next.t('unauthorized') : "+i18next.t('unauthorized'));
console.log("req.t('unauthorized') : "+req.t('unauthorized'));
res.send(req.language + ' == ' +req.t('unauthorized'));
});
app.listen(8443, function() {
console.log("Server listening on port", 8443);
});
==> all works http://localhost:8443/i18test --> en-US == User is not authorized to access What's the deal? -> it even tells you it's not loading the translations in the log (if not correctly setting path with __dirname) |
Yeah.. I dint include the src... that was correct in my case. |
If you like this module don’t forget to star this repo. Make a tweet, share the word or have a look at our https://locize.com to support the devs of this project. If you liked my support / work - I would enjoy a coffee sponsored using the “:purple_heart:Sponsor Button”. There are many ways to help this project 🙏 |
OS: windows
Verisons used
"i18next": "^11.2.3",
"i18next-browser-languagedetector": "^2.1.0",
"i18next-express-middleware": "^1.8.2",
"i18next-xhr-backend": "^1.5.1",
I am using i18next with nodeJs express server.
When any request flows into the server, the language is changed based on req.language but the key value of the message is always english
I have the code something like this
i18next
.use(i18nextMiddleware.LanguageDetector)
.use(i18NodefsBackend)
.init({
debug: true,
preload: ['de','en','es','fr','it','ja','ko','pt','zh'],
fallbackLng: ['en'],
ns: ['translation'],
backend: {
loadPath: "src/server/locales/{{lng}}/{{ns}}.json"
}
});
app.use(i18nextMiddleware.handle(i18next));
and in my route handler I have this code
function handler(req, res) {
//i18next.changeLanguage(req.language); // LINE-1
i18next.t('unauthorized'); // I always get english strings
}
When I un-comment LINE-1 this works. But I do not want to changeLanguage setting for every request and this is supposed to be handled by i18nextMiddleware.
In the logs I can see a message
i18next: languageChanged de
though the language is changed, the translated values is always in english. I tried all the options but could not figure out the issue.
The text was updated successfully, but these errors were encountered: