s00d / nuxt-i18n-micro

Nuxt I18n Micro is a fast, simple, and lightweight internationalization (i18n) module for Nuxt
https://s00d.github.io/nuxt-i18n-micro/
MIT License
86 stars 11 forks source link

[Feature] Add options to disable localized routing and customize locale detection strategy #16

Closed lehuuphuc closed 1 month ago

lehuuphuc commented 1 month ago

It would be nice if there is an option to disable localized routing when we're only using $t or similar functions for translations.

Additionally, having an option to define the priority for locale detection (e.g., cookie, router path, etc.) would be helpful. I've encountered some issues with the router in hashMode. For example, with the URL http://localhost:3000/#/vi, the locale is detected as "vi" on the client side but defaults to "en" on the server side, even though the "user-locale=vi" cookie was sent.

s00d commented 1 month ago

Hello, I definitely won’t be doing localization without routing; there’s no point in that. You can integrate i18n directly, which should take you about 10 minutes and 10-20 lines of code. In my opinion, using i18n in this way makes no sense at all. Regarding hashMode, I tested it on my own sample project, but unfortunately, I couldn’t replicate the issue. If you provide an example, I’ll do my best to fix it.

lehuuphuc commented 1 month ago

I am working on an admin dashboard project using a micro-frontend architecture with iframes. Each menu item is registered with an exact URL path, and localization is handled via cookies, so the URL should remain consistent across all locales.

Regarding the hashMode issue, please check out this playground

hashMode: false image

hashMode: true image

s00d commented 1 month ago

Hello, I reviewed your example and considered your ideas, but they seem to disrupt the existing workflow logic. Currently, the cookie is used solely to check if a locale has been set. Could you please provide more details on why you need this and how you intend to use it? I can simply add another entry in the cookie to set the locale, something like force-locale, to avoid breaking the logic. However, I need a more detailed description of your requirements.

lehuuphuc commented 1 month ago

I think it would be nice to give users the option to choose whether the locale is determined by a cookie or the URL path. Were you able to identify the reason for the differences when hashMode is enabled or disabled?

s00d commented 1 month ago

In version 1.16.1, I added a couple of workarounds to solve some of the issues, but in hash mode, the locale needs to be passed twice, for example: http://localhost:3000/de/#/de/, because the browser does not pass the hash to the backend when using SSR. When switching the locale, you must use $switchLocale. Here’s a small example: https://github.com/s00d/nuxt-i18n-micro/tree/main/test/fixtures/hashmode.

Unfortunately, there are no other ways; this is how hash mode works.

lehuuphuc commented 1 month ago

Thanks for the updates.

s00d commented 1 month ago

I will most likely be reworking it further. There are still issues with cookies, so I didn't look into it any further.

s00d commented 1 month ago

I have released a new version 1.17.0 in which the hash mode logic has been completely redesigned.

Now, routes will follow the format http://localhost:3000/#/de without a locale prefix. Everything operates through the hash-locale cookie for page reloads. Please test the update; in theory, this approach should fully replace the no_prefix mode.