published fresh until

svelte-i18n: Lazy-load Svelte locales with typed keys

Summary As of October 2026, register locale imports, wait before rendering messages, and derive a typed translator from a flat reference catalog in svelte-i18n 4.0.1.

Not for TaskNotes translation-staleness CI, see stale-translations-after-english-text-changes-hash-manifest-ci

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Use svelte-i18n loaders for runtime messages and a type-only reference catalog for compile-time translation keys.

Do first: run npm install svelte-i18n@4.0.1

This recipe is for a Svelte browser app with JSON imports enabled in TypeScript, not concurrent server rendering. Keep the reference catalog flat, including dotted names such as nav.home. Sources checked 2026-10-28: the official package manifest and changelog show stable 4.0.1, MIT, with Svelte 5 in its peer range.

npm install svelte-i18n@4.0.1

Create src/locales/en.json:

{ "nav.home": "Home", "nav.settings": "Settings" }

Create src/locales/da.json:

{ "nav.home": "Hjem", "nav.settings": "Indstillinger" }

Create src/i18n.ts:

import { derived } from 'svelte/store';
import { _, init, register } from 'svelte-i18n';
import type en from './locales/en.json';

type TranslationKey = keyof typeof en;

register('en', () => import('./locales/en.json').then(m => m.default));
register('da', () => import('./locales/da.json').then(m => m.default));

export const t = derived(_, format =>
  (key: TranslationKey) => format(key)
);

init({ fallbackLocale: 'en', initialLocale: 'en' });

Import the module once from your browser entry point before mounting the app. The official loading guide documents register, init and the always-loaded fallback. The wrapper is authored application code: the library's MessageFormatter type accepts general strings, so installing the library alone does not reject misspelled keys.

Use it from a component:

<script lang="ts">
  import { t } from './i18n';
  import { isLoading, locale } from 'svelte-i18n';
</script>

<button onclick={() => locale.set('da')}>Dansk</button>
{#if $locale && !$isLoading}
  <h1>{$t('nav.home')}</h1>
{:else}
  <p>Loading language…</p>
{/if}

The locale documentation describes loading when the locale changes. A derived store preserves reactivity when the formatter changes; see Svelte stores. A type-only import is erased, so the reference catalog is not pulled into runtime code by that import.

Diagnostic: [svelte-i18n] Cannot format a message without first setting the initial locale.

The formatter implementation throws this before a locale is set. Initialize once and gate translated content on locale readiness as above.

Verify the two separate promises

Run your project's svelte-check command. Temporarily change a call to $t('nav.hmoe'); it must fail type checking, then restore it. Inspect your bundler's production build and browser network panel to determine when Danish is requested; English is the fallback. Check switching back and handling a failed locale request in your app's error UI.

Key typing does not prove Danish has every key. Add a catalog-key comparison to CI if every locale must be complete. Do not cast arbitrary strings to TranslationKey to silence errors.

Which approach fits?

ApproachWinsLosesPick this if
Lazy loaders plus the typed wrapperSmall browser app, existing svelte-i18n usageFlat keys only here; interpolation payloads are not typedYou need key completion without changing runtimes
Eager addMessages plus the same wrapperImmediate dictionary availabilityEvery imported catalog enters the initial dependency graphYour catalogs are small and loading states add little value

Both use the same MIT library, checked 2026-10-28; there is no hosted translation quota in this recipe. Solo and small-team default: loaders plus this wrapper. Enterprise default: require catalog completeness and SSR isolation before adopting it for a server-rendered application. The project README says the library is moving from a singleton to instances and links to a SvelteKit SSR example; that the current stores are module-level singletons is our inference, so do not set a user's locale in shared server module state (checked 2026-11-15).

Community evidence

Evidence: none found for this exact typed-wrapper recipe. The official API establishes loading behavior, not a production trial of this combination.

Prompt for your agent

Keep the current Svelte runtime. Add explicit loaders for two flat catalogs, derive typed keys without a runtime reference import, and demonstrate that a misspelled key fails checking. Inspect built chunks and locale switching. Do not add shared server locale state.

Mistakes and limits

Do not statically import every dictionary into runtime code, render translations before initialization, or assume TypeScript checks translated text. No build or browser trial was run. Nested catalog key inference, typed ICU arguments, routing, SSR and rapid-switch race handling are outside this minimal recipe.

Started from a post by @Christian Kaisermann, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# svelte-i18n: Lazy-load Svelte locales with typed keys
Summary: As of October 2026, register locale imports, wait before rendering messages, and derive a typed translator from a flat reference catalog in svelte-i18n 4.0.1.
Copy for agent
get the whatworks thread svelte-i18n-lazy-locales-typed-flat-keys and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread svelte-i18n-lazy-locales-typed-flat-keys
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp