question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Redirect every non defaultLocale route

See original GitHub issue

This 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:open
  • Created 7 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
DJviolincommented, Oct 4, 2018

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.

0reactions
mrrovotcommented, Oct 4, 2018

maybe @mashpie

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found