Redirect every non defaultLocale route
See original GitHub issueThis is my i18n configuration:
locales: ['en', 'hu'],
fallbacks: { en: 'hu' },
defaultLocale: 'en',
i18n initialized to the routes with this middleware:
const langRouter = (req, res, next) => {
const paramsLang = req.params.lang;
const resLocals = res.locals;
if (/^(en|hu)$/.exec(paramsLang)) {
i18n.setLocale([req, resLocals], paramsLang);
resLocals.language = `/${paramsLang}`;
}
next();
};
app.all('/:lang/*', langRouter);
app.use('/:lang', langRouter);
In the past I redirected every route this way for example when someone requested the frontpage:
app.use('/:lang', index);
app.use('/', (req, res) =>
res.status(302).redirect(`/${req.getLocale()}`)
);
Now I try to only redirect the /hu
route:
app.use('/hu', index);
app.use('/', (req, res, next) => {
if (req.headers['accept-language'] === 'hu') {
return res.status(302).redirect('/hu');
}
return next();
});
app.use('/', index);
This is not works, not redirecting the frontpage. Another problem is the frontpage (and the entire site) translated to Hungarian.
What I want is the users will be redirected to /hu
route when their browsers are set to hungarian language. The /
route should always return the defaultLocale
(english).
So these routes always be english:
/
/blog
/contact
And I want to redirect to these routes only those users whose browsers set to hungarian:
/hu
/hu/blog
/hu/contact
How can this be achieved?
Issue Analytics
- State:
- Created 7 years ago
- Comments:12 (5 by maintainers)
Top Results From Across the Web
Symfony 3 Redirect All Routes To Current Locale Version
If it detects a locale that is not in the list it will fallback to the default locale, which in this case is...
Read more >Redirect to default locale if not given and fortify routes
My intention is to automatically redirect my routes with a default locale or the last locale set by the user, for instance if...
Read more >Advanced Features: Internationalized Routing - Next.js
If user locale is nl-BE and it is not listed in your configuration, they will be redirected to nl if available, or to...
Read more >Multi language routing with Laravel: one domain fits all
If the locale length does not match 2 characters, the app will redirect to the default locale, as defined in config/app.php. If the...
Read more >How to prefix URLs with the locale in Symfony - Needlify
It serves as a fallback solution when # every other rewrite/redirect fails (e.g. in an ... It will # work in environments without...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In the last 2 years? Honestly, I haven’t used i18n-node at all, moreover, I moved to API design.
In the basic, yes, I wanted something similar.
maybe @mashpie