Closed johannesschobel closed 5 years ago
Never used nest.js...the trick in this package is we use https://github.com/i18next/i18next-express-middleware#language-detection detection on every request (handle middleware) like: https://github.com/i18next/i18next-express-middleware/blob/master/src/index.js#L40
Dear @jamuhl , thanks a lot for your response.. I did manage to load my translations as follows:
const backend = new Backend(); // from i18next-node-fs-backend
const middlewareDetector = new LanguageDetector(); // from i18next-express-middleware
const i18n = await i18next
.use(backend)
.use(middlewareDetector)
.init({
backend: {
loadPath: PathHelper.assetsPath() + '/i18n/{{lng}}/{{ns}}.i18n.json',
addPath: PathHelper.assetsPath() + '/i18n/{{lng}}/{{ns}}.missing.i18n.json',
jsonIndent: 10,
},
detection: {
order: ['header', 'querystring'],
lookupQuerystring: 'lang',
},
ns: ['mail', 'basic', 'ui'],
defaultNS: 'basic',
fallbackLng: 'en',
preload: ['en', 'de'],
saveMissing: true,
},
);
if i then use
i18n.t('some.key', {ns: 'mail');
it works as expected.. Even if i (manually) change the language..
However, if I try to access the request
(and the properties that should have been set via this middleware) i always get undefined
..
When i access request.i18n.language
i get the value de
, which is correct..
However, the value is not set in i18next
and therefore i only get the default language (which is en
)..
that is expected...on the request the i18n instance is cloned and fixed to this requests language -> using the i18next instance as is would result in requests "stealing" each others language during async processes...2 requests working with one singleton - bad idea
ok, so i need to access the data via request.i18n.t('my.custom.key');
right?
this, in turn, means, that i need to inject the request.i18n
to my pug templates as well..
should be already added:
https://github.com/i18next/i18next-express-middleware/blob/master/src/index.js#L64
Dear @jamuhl , i have played around a bit, and it seems that this is an issue related to a combination of NestJS
and the mailer-package i am using.
The respective mailer-package uses pug
(or handlebar
) under the hood for defining the HTML templates..
I had to manually pass the request.i18n
object as variable to the pug
template.. This may be a bit dirty, but it worked for me.. .
Thanks for your help! Awesome work!
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 -> there are many ways to help this project :pray:
i starred it.. and will consider i18next
for further projects..
i think, it would be nice to have proper typescript support in your packages, without relying on @types/...
dependencies..
i18next, react-i18next have those typings in the repo since a few months now. https://github.com/i18next/i18next/blob/master/index.d.ts
yeah, i am already using the ones from i18next
.. thanks a lot
For other people who came here via any search engine: There's a good maintained package for NestJS with everything you need: https://github.com/ToonvanStrijp/nestjs-i18n
@kkoomen This has nothing to do with i18next... Nevertheless i18next-express-middleware is deprecated and replaced by i18next-http-middleware
This has everything to do with i18next. The author of this issue can use the package I suggested with NestJS which solves his problem.
It's about "NestJS and I18next" is nestjs-i18n compatible with the i18next ecosystem? Don't see any evidence for this... but I'm not a NestJS expert. // cc @ToonvanStrijp
@adrai @kkoomen nestjs-i18n
replaces the need for i18next
inside a nestjs
project. So the packages aren't compatible with each other.
@ToonvanStrijp thx for the clarification
Dear @jamuhl , i have played around a bit, and it seems that this is an issue related to a combination of
NestJS
and the mailer-package i am using. The respective mailer-package usespug
(orhandlebar
) under the hood for defining the HTML templates..I had to manually pass the
request.i18n
object as variable to thepug
template.. This may be a bit dirty, but it worked for me.. .Thanks for your help! Awesome work!
can you share your solution. I would like to try i18nnext on nestjs too
Dear @rubiin ,
i am very sorry, but i dropped the use of i18n about a year ago :( I don't recall how i solved the described issue :sob: All the best and good luck, Johannes
Dear @jamuhl and community,
first of all, I would like to thank you for this nice package - great work :+1:
I am currently developing an API using
NestJS
and would like to use theI18Next
package for managing the translations (e.g., translating emails, ...).I managed to load my translation language files, however, i am not able to use this middleware to detect the requested language. Can you give me some advice how to get this package to work with
NestJS
?All the best and thanks a lot for your help