Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom cookie name for locale #486

Closed
NickNaso opened this issue Aug 29, 2023 · 4 comments
Closed

Custom cookie name for locale #486

NickNaso opened this issue Aug 29, 2023 · 4 comments
Labels
enhancement New feature or request has-workaround Stale unconfirmed Needs triage.

Comments

@NickNaso
Copy link

Is your feature request related to a problem? Please describe.

Hi everybody and thanks for this wonderful work. I'm using next-intl v3.0.0-beta.14 all works fine for my use case, but it would good to have the possibilities to set the COOKIE_LOCALE_NAME (today it's fixed and its value is NEXT_LOCALE).
The problem is that my Next.js application should works together with other web application that use another cookie name to handle the language.

Describe the solution you'd like

I think that a possible solution could be to allow to pass a new parameter to es. localeCookie to createMiddleware function like reported below:

import createMiddleware from 'next-intl/middleware';

export default createMiddleware({
  // A list of all locales that are supported
  locales: [''en', 'it'],

  // If this locale is matched, pathnames work without a prefix (e.g. `/about`)
  defaultLocale: 'en',

  localeCookie: 'MY-CUSTOM-LOCALE-COOKIE'
});

export const config = {
  // Skip all paths that should not be internationalized. This example skips the
  // folders "api", "_next" and all files with an extension (e.g. favicon.ico)
  matcher: ['/((?!api|_next|.*\\..*).*)'],
};

Describe alternatives you've considered

I think that the alternative should be to update the values of the cookie in the middleware and about that is there any suggusted workaround to synchronize the value of NEXT_LOCALE cookie with another one?

@NickNaso NickNaso added enhancement New feature or request unconfirmed Needs triage. labels Aug 29, 2023
@amannn
Copy link
Owner

amannn commented Aug 30, 2023

This question came up before here: #166

We probably won't add this in the near future since I currently have a particular use case in mind for #149 where it might be necessary to know the name of the cookie on the client side. If the name is configurable at the middleware level, we wouldn't be able to know its name directly on the client side, except if we add another layer on top, e.g. a cookie that the middleware adds to responses, which contains the name of the cookie.

If there's more demand for this in the future I might consider it though.

For the time being, to solve your use case, you can compose the middleware and configure NEXT_LOCALE as a cookie on the request object, before the next-intl middleware receives the request. Potentially you can also read NEXT_LOCALE from the response object and optionally sync it back to your other cookie.

Hope this helps!

@NickNaso
Copy link
Author

Thank you your suggestion helped me.❤️

@413n
Copy link

413n commented Oct 17, 2023

Just wanted to add my 2 cents. In my use case I wanted to modify the cookie of NEXT_LOCALE to be available cross subdomain so I had to rewrite the cookie after createMiddleware to change the domain option. It would be good to consider also the CookieOptions if this feature will be developed.

Copy link

github-actions bot commented Feb 8, 2024

This issue has been automatically closed because there was no recent activity and it was marked as unconfirmed. Note that issues are regularly checked and if they remain in unconfirmed state, they might miss information required to be actionable or are potentially out-of-scope. If you'd like to discuss this topic further, feel free to open a discussion instead.

@github-actions github-actions bot added the Stale label Feb 8, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request has-workaround Stale unconfirmed Needs triage.
Projects
None yet
Development

No branches or pull requests

4 participants
@NickNaso @amannn @413n and others