angeloocana / gatsby-plugin-i18n

Multi language routes for Gatsby
435 stars 77 forks source link

Use language prefix for all routes and links #11

Open sedubois opened 7 years ago

sedubois commented 7 years ago

Currently the example at gatsby-starter-default-i18n only shows how to go to another page by hard-coding the language in the URL, e.g. <Link to="/en/page-2/">Go to page 2</Link>. However that's not very scalable and there should be a more generic solution to switch to some other page while preserving the current language, especially for the case (probably corresponding to most people's use case) where there isn't a separate source code file for each language.

I guess this is where the concept of Gatsby path prefix might come in, but I never used it and it's unclear how to change it on the fly.

angeloocana commented 7 years ago

I dont thinkGatsby path prefixwas created for that. You use it when you cant host your site at the root level, ex:

sedubois.github.io/ -> Do not need path prefix sedebois.github.io/repo_test/ -> You need add repo_test as path prefix.

I would create your own component and functions to get the current language to create the Link.

This is one example that has a link to /${langKey}/resume/ https://github.com/angeloocana/angeloocana/blob/master/src/components/IndexPage.js

This is how I create the menu for my website: https://github.com/angeloocana/angeloocana/blob/master/src/components/Menu.js

Thank you a lot for helping!!! =D This plugin is getting awesome after each contribution, new idea, discussions =D

sedubois commented 7 years ago

I think it would just be quite useful if the plugin exported a Link component which would automatically prefix whatever link is passed to it, if the passed location doesn’t already have a language prefix. Like <Link to=...> (same signature as gatsby-link).

A second parameter could be passed if one wants to change to another language, like <Link to=... lang=...>.

Happy to do what I can as I learn!

Most critical/blocking is by far #6, any idea when you can fix? I tried to fork and change code but no good so far.

angeloocana commented 7 years ago

@sedubois I'm working on it now. I hope I can finish it today.

And later the <Link component! =D