· 3 min read
A language in the URL
Phase 10 started: the site in English and Spanish. I chose a homemade setup over Paraglide JS: most of the text is markdown and YAML, which Paraglide doesn't…
What we did
- Phase 10 started: the site in English and Spanish. I chose a homemade setup over Paraglide JS: most of the text is markdown and YAML, which Paraglide doesn't translate, its URL handling is a SvelteKit hook I can write myself, and its v3 was days old. My messages are shaped like Paraglide's (
m.blogTitle()), so moving later is mostly a change of imports. - Decided in an interview: English at
/portfolio/, Spanish at/portfolio/es/, same paths (no translated slugs), Spain's Spanish (es-ES), no language detection, an ES/EN link in the header, ISO dates in both languages. - A spike on
/blogfirst, then every page:src/hooks.ts: areroutehook strips the/es, so/es/blogis the/blogroute. Route ids don't change, so the breadcrumbs, the nav marker andisInside()kept working.src/lib/i18n/:en.tsandes.ts(about 130 messages each;es.tsusessatisfies, so a missing one failspnpm check),locales.ts(no SvelteKit imports:split(),plural()withIntl.PluralRules),index.ts(m,route(),getLocale(),formatNumber()).<html lang>through a%lang%placeholder inapp.html, filled inhooks.server.ts, and updated in the browser after a client-side switch.Seo.svelte:hreflangalternates,x-default,og:locale. The sitemap lists each translated page once per language.
- Tests: the smoke test runs in both languages, plus a test for the switch; unit tests for
split(), plurals and every message.
What I learned
reroutechanges the route, not the URL.page.urlkeeps the/es(that's where the language comes from);page.route.idstays/blog.resolve()writes links relative to the real URL while prerendering: on/es/blog,resolve('/blog')is../blog, the English page. So every link goes throughroute(), which putses/right afterresolve('/'). A unit test fails if any other file importsresolve.- The language comes from the page, not from a variable: the build renders every page, in both languages, in one process.
getLocale()readspage.data.locale, which belongs to the page being rendered. - Same route, new language, same component: SvelteKit keeps a page's component when only the URL's language changes, so anything worked out once in a
<script>stays in the old language.{#key getLocale()}in the root layout rebuilds the page. - Spanish writes
4,53, not4.53:Intl.NumberFormatknows.
Problems & fixes
- Vite's
import.meta.env.BASE_URLis./under SvelteKit, so it can't tell the hook the base path.vite.config.tsdefinesimport.meta.env.BASE_PATHinstead. - The first sitemap had no Spanish URLs. It builds its page list when the module loads, where
resolve()is absolute, and localized it during the request, whereresolve('/')is relative.localizePath()works from the real base path instead. - The test for "every message has text" called each message with
{ count: 2 }; the skill level messages need alevel. It found nothing wrong in the messages, but it was a reminder that the test's sample has to fit every message.
Links
Next
- The content side: posts, entries and YAML in both languages.