yassinedoghri / astro-i18next

An astro integration of i18next + some utility components to help you translate your astro websites!
https://astro-i18next.yassinedoghri.com
MIT License
521 stars 37 forks source link

localizePath is not able to localize urls with a dynamic path #148

Open Foxandxss opened 1 year ago

Foxandxss commented 1 year ago

Describe the bug

localizePath is not able to localize urls with a dynamic path

To Reproduce

Say you have:

src/pages/solutions/[slug].astro

You will have routes like:

localhost:3000/solutions/foo
localhost:3000/solutions/bar
localhost:3000/solutions/baz

Then imagine I want to translate solutions to spanish, AKA soluciones

I tried:

routes: {
  es: {
    solutions: {
      index: 'soluciones',
      '[slug]': '[slug]'
    }
}

On the one hand I had to put the index part or it won't work but I tried to add [slug] which generates the correct page. I can even go to:

localhost:3000/es/soluciones/foo

and that will work, but if I tried something like:

localizePath(`/solutions/${slug}`)
localizePath('/solutions/foo')

it will generate:

localhost:3000/es/solutions/foo

So far I am doing:

`${localizePath('solutions')}/${slug}`

That will generate:

localhost:3000/es/soluciones/foo

Which works but breaks the LanguageSelector since it is not doing this trick.

localizePath should be able to localize a path with a dynamic path in it.

Context (please complete the following information):

darioguarascio commented 1 year ago

I found myself in exactly the same spot. Any news on the matter?

aditiakusuma commented 1 year ago

found my solution..

use redirect experimental where release in astro 2.6.


redirects: {
    "/es/solutions/[slug]": "/es/soluciones/[slug]",
    "/soluciones/[slug]": "/solutions/[slug]",
}
rogerhnn commented 9 months ago

You need to pass the full url to be localized, example:

mywebsite.com/[category]/[id] <- this is my url with dynamic paths.

const url = `items/${category}/${id}`
<a href="${localizePath(url)}">Localized link</a>