Enable internationalization in the SvelteKit project.
Syntax
$ vela enable i18nThis sets up Wuchale with locale-aware routing. Translatable strings are extracted from the source rather than written into message files by hand, so no keys are maintained separately from the markup.
Has no dependencies — it works with or without a backend, and composes with content negotiation’s reroute hook.
What you get
wuchale.config.js, with the localesenandes. English is the source locale and keeps the unprefixed URLs; Spanish is served under/es. It also lists the components whosehrefis localized like an<a>’s:Button,Navbar.Brand,Breadcrumb.Linkand the other link components a project starts with.- A
reroutehook insrc/hooks.tsthat maps localized URLs back to their routes, and the URL helpers it uses insrc/lib/url.ts. The file is created if the project has none; otherwise the hook is composed with the existingreroute. - A language select in
src/lib/components/language-select.svelte, added to the root layout. - A handle in
src/hooks.server.tsthat loads the catalogs on the server and renders each request in its locale. The file is created if the project has none; otherwise the handle is composed with the existinghandle. - A
loadinsrc/routes/+layout.tsthat loads the locale’s catalog in the browser. It is added to a file that has route options of its own; a file that already has aloadis left alone, with a note on what to merge. lang="%sveltekit.lang%"on the<html>tag insrc/app.html, which the handle fills in with the locale.- The Wuchale plugin in the Vite config, and a
#locales/*entry in thepackage.jsonimportsthat maps tosrc/locales, where the catalogs live. - The
wuchaleand@wuchale/sveltepackages, and theselectcomponent in a project with shadcn-svelte. - A
.gitignoreentry forsrc/locales/.wuchale, and a.prettierignoreentry forsrc/locales, which Wuchale rewrites in its own style on every extract. A project without a.prettierignoreis left without one.
In a project with shadcn-svelte, the language select is a shadcn Select, added as the last item of the navbar in the root layout. Without shadcn-svelte, as in a project from sv create, it’s a native <select> placed above {@render children()} in src/routes/+layout.svelte.
If src/routes/+page.svelte is still the sv create welcome page, the command also writes its Spanish translation to src/locales/es.po, so switching language visibly changes the page. Any other copy is yours to translate.
Choosing the pages
A page gets a URL in each language, such as /es/about, only when its path is listed under url.patterns in wuchale.config.js. The config starts with / alone, so once it is written the command asks which of the project’s other pages to add. Until a page is listed, links to it stay in the default language and it answers 404 under /es.
Without a terminal, or if you cancel the question, nothing is added and the next steps name vela i18n routes, which asks again. Run that command whenever you add a page.
A link component of your own, or one imported under another name such as BreadcrumbLink, is matched by the name it is written with: add it to urlAttribs in wuchale.config.js beside the ones already there.
A file the command can’t safely edit is left as it is, and the change to make is printed instead. The common case is a src/routes/+layout.ts that already has a load, as the static template’s does: merge the printed loader into it by hand.
Working with translations
Once enabled, the vela i18n commands manage the catalogs:
vela i18n extract- Extract translatable stringsvela i18n watch- Extract on changevela i18n status- Show what is translatedvela i18n clean- Drop strings no longer usedvela i18n routes- Choose which pages have a URL in every language
Reverting
vela disable i18n removes the generated files and undoes the changes above. Your catalogs in src/locales are kept.