Svelte: Getting Started
Install the packages
Section titled “Install the packages”vp add @lingui/core lingui-for-sveltevp add -D @lingui/cli @lingui/conf unplugin-lingui-macronpm install @lingui/core lingui-for-sveltenpm install -D @lingui/cli @lingui/conf unplugin-lingui-macropnpm add @lingui/core lingui-for-sveltepnpm add -D @lingui/cli @lingui/conf unplugin-lingui-macroyarn add @lingui/core lingui-for-svelteyarn add -D @lingui/cli @lingui/conf unplugin-lingui-macroConfigure Vite
Section titled “Configure Vite”import { defineConfig } from "vite";import { sveltekit } from "@sveltejs/kit/vite";import linguiForSvelte from "lingui-for-svelte/unplugin/vite";import linguiMacro from "unplugin-lingui-macro/vite";
export default defineConfig({ plugins: [linguiMacro(), linguiForSvelte(), sveltekit()],});Configure Lingui
Section titled “Configure Lingui”import babelExtractor from "@lingui/cli/api/extractors/babel";import { defineConfig } from "@lingui/conf";import { svelteExtractor } from "lingui-for-svelte/extractor";
export default defineConfig({ locales: ["en", "ja"], sourceLocale: "en", catalogs: [{ path: "src/lib/i18n/locales/{locale}" }], extractors: [svelteExtractor, babelExtractor],});Initialize Lingui
Section titled “Initialize Lingui”Call setLinguiContext near the root of the component tree, such as a layout, before any translated
component renders.
<script lang="ts"> import { setupI18n } from "@lingui/core"; import { setLinguiContext } from "lingui-for-svelte"; import { catalog } from "$lib/i18n/catalog";
const { children } = $props();
const i18n = setupI18n({ locale: "en", messages: catalog }); setLinguiContext(i18n);</script>
{@render children()}Use macros in .svelte
Section titled “Use macros in .svelte”<script lang="ts"> import { Trans, t } from "lingui-for-svelte/macro";
let count = $state(1);</script>
<h1>{$t`Hello from Svelte`}</h1>
<p><Trans>{count} item selected</Trans></p>In Svelte, the reactive form is available as $t. When the locale changes, this expression will automatically re-evaluate.
See Reactive Macros for when to use the $ prefix and how it works under the hood.
Next pages
Section titled “Next pages”- Read i18n Context for initialization patterns, island setup, and locale switching.
- Read Reactive Macros to understand how
$tand friends work and when to use the$prefix. - Read Locale Resolution for how to resolve the initial locale from URL params, cookies, and browser headers in SvelteKit.
- Read Svelte caveats for edge cases and limitations.
- Use the macro reference when you need precise authoring rules.