i18next / i18next-express-middleware

[deprecated] can be replaced with i18next-http-middleware
https://github.com/i18next/i18next-http-middleware
MIT License
206 stars 52 forks source link

Why would you pass i18n instance from backend to frontend via ctx.req #209

Closed zhangyugege closed 4 years ago

zhangyugege commented 4 years ago

Hello ~ I see that the middleware on the node server depends on the i18n-express-middleware library.

In i18n-express-middleware, it pass the entire i18n example to the front end via ctx.req.

    // assert for req
    req.i18n = i18n;
    req.t = t;

Source code at: https://github.com/i18next/i18next-express-middleware/blob/master/src/index.js#L60

Why would you pass the entire i18n instance to ctx.req, is there anything special in it?:sparkles::seedling:Why not just pass locales data to the front end? Can you answer it for me, thank you~

jamuhl commented 4 years ago

While t is the main function to access all the translations - there are functions on i18n that you might need or not need (exist, dir, language, ... https://www.i18next.com/overview/api) -> does it hurt to pass it down - no...

zhangyugege commented 4 years ago

While t is the main function to access all the translations - there are functions on i18n that you might need or not need (exist, dir, language, ... https://www.i18next.com/overview/api) -> does it hurt to pass it down - no...

But i think i18next is a litte too big.... Does its size affect the first screen rendering?

jamuhl commented 4 years ago

How should it...it's on the server it's in the request object and gets passed to your template handler or can be used for serverside rendering eg. a react tree -> it's not on the client...you don't res.send(i18n)

zhangyugege commented 4 years ago

How should it...it's on the server it's in the request object and gets passed to your template handler or can be used for serverside rendering eg. a react tree -> it's not on the client...you don't res.send(i18n)

Oh i suddenly realized, thank you very much!:poop:

zhangyugege commented 4 years ago

And i have an idea about if the amount of locale data is too large, we can get it asynchronously via i18next-xhr-backend. A webapp, for example, requires thousands of locale datas. Is my idea feasible?

jamuhl commented 4 years ago

that's what it does...you can structure your translations into multiple namespaces and load them on demand

zhangyugege commented 4 years ago

that's what it does...you can structure your translations into multiple namespaces and load them on demand

yeah!