SvelteKit library that handles URL localization and routing.
Note This package is WIP. It works but the future updates may introduce breaking changes to simplify setup or introduce features.
/about
, /ru/o-nas
;/en/about
, /ru/o-nas
;example.com/about
, example.ru/o-nas
./ru/about-us
to the corrected /ru/o-nas
;<link rel="alternate"
tags and for a language switcher;https://sveltekit-localize-url.vercel.app/
This library was initially created to be used with the typesafe-i18n library, but should work fine with any i18n library (or even without one).
npm install sveltekit-localize-url
Please take a look the example for the full setup.
Let's imagine that:
en
and enabled locales are en
, ru
and it
;/[[lang=lang]]/[l_about=l_about]
;l_about
param is registered like this:// src/params/l_about.ts
import { localizeParam, matchParam } from 'sveltekit-localize-url';
const localizedParam = localizeParam(1, 'l_about', {
en: 'about-us',
ru: 'o-nas'
// there is NO version in Italian
});
export const match: ParamMatcher = (param) => matchParam(param, localizedParam);
When SvelteKit will match params, it will decide that all of following paths are valid:
/about-us
/en/about-us
/ru/about-us
/it/about-us
/o-nas
/en/o-nas
/ru/o-nas
/it/o-nas
However the validateUrlLocale()
function that is called in the src/routes/[[lang=lang]]/+layout.ts
will take care of that by taking registered params and using them to construct a correct path for the current locale.
So in the end we'll get the following:
/about-us
- Correct;/ru/o-nas
- Correct;/o-nas
, /en/about-us
, /en/o-nas
- Redirect to /about-us
;/ru/about-us
- Redirect to /ru/o-nas
;/it/about-us
, /it/o-nas
- 404 Not Found.