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

On demand localised urls #196

Closed stevethatcodes closed 5 years ago

stevethatcodes commented 5 years ago

Firstly, thanks for all the great work with i18next!

I had a question regarding the ability to specify localised urls on-demand, instead of setting the localised URLs upfront using i18nextMiddleware.addRoute(...).

What I am trying to achieve is something along the lines of (pseudo-code):

en.json:

{
    "some-translated-path": "some-translated-path",
    "some-other-translated-path": "some-other-translated-path"
}

de.json:

{
    "some-translated-path": "some-translated-path-de",
    "some-other-translated-path": "some-other-translated-path-de"
}
app.use(i18nextMiddleware.handle(i18next), { removeLngFromUrl: false });

app.get('/:lng/some-translated-path', (req, res, next) => { ... });
app.get('/:lng/some-other-translated-path', (req, res, next) => { ... });

Where the above urls will match when the language and translated path are compatible with each-other.

E.g. /en/some-translated-path <-- 200 /en/some-translated-path-de <-- 404

Is this something that i18next-express-middleware would facilitate, or should I write a custom middleware specifically for this need?

jamuhl commented 5 years ago

Currently, this is not possible. The middleware is rather simple: https://github.com/i18next/i18next-express-middleware/blob/master/src/index.js#L136

So one option is to create your own. Or add a PR opting out of translating a route part, eg. like: /:lng/[some-other-translated-path]' or similar

stevethatcodes commented 5 years ago

@jamuhl Thanks for the response. I suspected this was the case but thank you for confirming that it's not currently possible. If it's something I come up with a nice solution to I'll be sure to raise a PR.

jamuhl commented 5 years ago

would be great...rather sure others would like this feature too

martpie commented 5 years ago

You may find that useful:

https://github.com/zeit/next.js/issues/8241#issuecomment-519551275

stevethatcodes commented 5 years ago

@jamuhl We actually ended up solving this without this middleware, but instead writing our own middleware that translate routes and integrates with our Inversify-Express server.