jlarmstrongiv / astro-i18n-aut

The i18n integration for Astro 🧑‍🚀
https://www.npmjs.com/package/astro-i18n-aut
MIT License
132 stars 12 forks source link

redirectDefaultLocale behaviour #37

Closed dallyh closed 1 year ago

dallyh commented 1 year ago

Hello,

this is more of a question than issue.

I'm struggling a little bit with the redirectDefaultLocale option. I want my site to have paths for every language possible, that is for example "en, cs, fr" -> /en/page/, /cs/page/, /fr/page/.

When i set the redirectDefaultLocale to false, and my defaultLocale is "en", then /en/page/ returns 404. When the redirectDefaultLocale is in it's default state, then /en/page/ redirects to /page/. When I manually create /en/page/, then every other language is not automatically generated (404's).

Is it possible to make it so this integration generates paths for every language, even if it's the default one?

Thanks!

jlarmstrongiv commented 1 year ago

Hi @dallyh,

This library is mostly meant to support the use-case of having a defaultLocale with Astro, without having to duplicate /about.astro and /es/about.astro

If you don’t want a defaultLocale, then you can use this Astro cookbook recipe or their upcoming their experimental router. Or, you can just use src/pages/[lang]/about.astro. I think those options are better for your use case, and do not plan to support it in this library. I hope these links point you in the right direction though

This library solves a problem that neither their cookbook nor experimental router solves—having a defaultLocale without page duplication


The redirectDefaultLocale controls the behavior of /en/about, when en is the defaultLocale. Setting redirectDefaultLocale means /en/about will redirect to /about, while setting it to false means /en/about will return a 404

dallyh commented 1 year ago

@jlarmstrongiv

Thanks for the response, and I got it thanks.

But this integration solves more than the defaultLocale and that is, it solves the manual work required (set up every page individually) to use either the cookbook recipe or experimental router. Of course I could set up a script that does this for me, but this integration does everything conveniently in the background with minimal configuration. So for me a "mode" where it could generate paths for every language (and maybe discard the default ones, or redirect it the other way around) could be great in this combination.

So I would like to reopen this for further discussion (if possible) from your side.

jlarmstrongiv commented 1 year ago

@dallyh the src/pages/[lang]/about.astro should allow you to use one Astro page for all of your locales. You may have to define/pass your locales (["en", "es"]) in getStaticPaths or in SSR, but it should let you skip creating files like /en/about.astro and /es/about.astro. You may have to write your own helper functions to make it more ergonomic though. A custom 404 page (if using SSG) or custom middleware (if using SSR) could handle your defaultLocale redirects with that approach.

Having the options to avoid using a defaultLocale or redirect the defaultLocale to its prefixed path /en/about would pretty much mean re-writing this library. It’s certainly possible, but would take time to write, manually test all configuration options, and review. Are you interested in contributing this refactor?

For your particular use case without a defaultLocale, I think sticking with those Astro options is probably the easiest path forward for you. Plus, it avoids https://github.com/jlarmstrongiv/astro-i18n-aut/issues/19.

dallyh commented 12 months ago

@jlarmstrongiv Thanks for your help and explanation, I appreciate it. I don't feel like a developer that could handle such task of refactoring the whole library.

The combination of dynamic [lang] path and Astro's new i18n router works well, but there are some quirks as of now.

Again thanks for all the information :)

jlarmstrongiv commented 12 months ago

@dallyh it’s certainly possible—I rewrote the library once to support the base option. But, it’s definitely a lot faster to use the dynamic [lang] path. Anyway, glad the suggestions helped 😄