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.

how to use the NEXT_LOCALE

See original GitHub issue

hello, I need to persist the change when somebody goes to /en route or click

   # placed in components/header.tsx
   # href is empty so would not change the page, but when we move to 
   # another page it will be back to default, how to persist so it would only 
   # change when we click another locale="otherlang" ?

    <Link href="" locale="en" key="en">
      {t(`common:menuchangetodefaultinheader`)}
    </Link>

they should persist until anybody change the default language is there an example on how to use the next_locale in a nextjs project? where should it be placed? how do you guys use cookie or NEXT_lOCALE?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
felipe-aguirrecommented, Feb 10, 2021

Example on useEffect:

import { useRouter } from 'next/router'

// ...

function usePersistLocaleCookie() {
    const { locale, defaultLocale } = useRouter()

    useEffect(persistLocaleCookie, [locale, defaultLocale])
    function persistLocaleCookie() {
      if(locale !== defaultLocale) {
         const date = new Date()
         const expireMs = 100 * 365 * 24 * 60 * 60 * 1000 // 100 days
         date.setTime(date.getTime() + expireMs)
         document.cookie = `NEXT_LOCALE=${locale};expires=${date.toUTCString()};path=/`
      }
    }
}

This should be on the README, thanks a lot

1reaction
aralrocacommented, Feb 9, 2021

@BjoernRave, this is part of Next.js, not Next-translate:

https://nextjs.org/docs/advanced-features/i18n-routing#leveraging-the-next_locale-cookie

Next-translate since version 0.19 uses the i18n routing of Next.js, and all the logic is exactly the same as Next.js. The only new logic that Next-translate introduces now is to inject code inside the loaders (getStaticProps, getServerSideProps…) to download the translations that each page needs according to the current language.

By this, I mean that the reason why by default is so, it’s something that wasn’t discussed here in the library. Surely any suggestions on routing are best addressed to @timneutkens’s RFC.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Features: Internationalized Routing - Next.js
By using domain routing you can configure locales to be served from different domains: // next.config.js module.exports = { i18n: { locales: ['en-US',...
Read more >
Next.js: Adding Localization with Next-Intl - This Dot Labs
Adding locales to a project in NextJs is as easy as creating a few files, and installing the handy npm package next-intl ....
Read more >
How to access the current i18n locale in an API route? #21798
I used to be able to use req.query.__nextLocale , but is not available anymore after 10.0.5. Tried to check cookies, try to import...
Read more >
Localizing Your Next.js App - Smashing Magazine
Create valid ones using locale code and/or country codes and stick with lower-case because they will generate a url soon. Now your app...
Read more >
How to access locale in custom app on server-side in Next.js?
You can access the locale in your custom app's getInitialProps through the router prop. static async getInitialProps({ Component, ctx, ...
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