svelte-i18n icon indicating copy to clipboard operation
svelte-i18n copied to clipboard

Add default lang and translated page url to main site url (sapper-i18n)

Open oratoran opened this issue 3 years ago • 2 comments

Hi all,

I wonder how can I make sapper-i18n-template's url structure more i18n friendly (better for seo as well)

Eg: example.com (default lang: en) example.com/about (default lang: en)

example.com/pt/ (translated lang: pt with added lang 'pt' end of url) example.com/pt/sobre (translated lang: pt with translated 'about' text as 'sobre' added to url

There will be two benefits:

  1. shareable direct links for translated content (eg: example.com/pt/about/)
  2. easy to read full url for seo

Any idea to achieve this?

Thanks

oratoran avatar Feb 25 '21 08:02 oratoran

This is the exactly I am looking for: https://i18n.nuxtjs.org/routing/

Is it possible to sapper as well?

oratoran avatar Feb 25 '21 20:02 oratoran

Sapper use the architecture of Express/Polka middlewares.

You can add in routes folder a directory called "[lang]", and put the main layout file as the file "[lang]/_layout.svelte".

In this layout file you accept the parameter lang, and initialize svelte-I18n to the correct language. All the other routes files of your website should be as well under the directory "[lang]".

This will handle I18n paths, and for non-I18n oath you'd like to change the URL to an I18n path with the default language, this is how: In server.js/ts, you'll see it uses sapper middleware. You can prepend a custom middleware which redirect the user or just override the request header path from non-I18n paths to the the I18 path with the default language.

Tal500 avatar Feb 25 '22 14:02 Tal500