Ask your question and get a summary of the document by referencing this page and the AI provider of your choice
Version History
- "Add usePathname utility"v10.0.023/06/2026
- "Init history"v8.2.022/06/2026
The content of this page was translated using an AI.
See the last version of the original content in EnglishIf you have an idea for improving this documentation, please feel free to contribute by submitting a pull request on GitHub.
GitHub link to the documentationCopy doc Markdown to clipboard
Svelte Integration: usePathname Documentation
The usePathname function returns the current browser pathname with the locale segment stripped, as a Svelte Readable<string> store. It is useful for building locale-aware navigation — for example, determining which nav item is active — without having to manually remove the locale prefix.
Importing usePathname in Svelte
Copy the code to the clipboard
import { usePathname } from "svelte-intlayer";Overview
usePathname creates a Svelte readable store that reads window.location.pathname, strips the locale prefix via getPathWithoutLocale, and emits a new value whenever the browser fires a popstate event (back/forward navigation). Subscribe with the $ store syntax in components.
Usage
Copy the code to the clipboard
<script lang="ts"> import { usePathname } from "svelte-intlayer"; export let href: string; export let label: string; const pathname = usePathname();</script><a {href} aria-current={$pathname === href ? "page" : undefined}> {label}</a>Return Value
Open the table in a modal to view all data content clearly
| Type | Description |
|---|---|
Readable<string> | Svelte readable store containing the current pathname without the locale prefix. |
Behaviour
- Locale stripping: Removes the leading locale segment (e.g.
/en-GB/dashboard→/dashboard). - Reactive: Emits a new value on every
popstateevent (browser back / forward navigation). - SSR-safe: Returns
""whenwindowis not available. - Cleanup: The
popstatelistener is removed automatically when the last subscriber unsubscribes.
Example
Copy the code to the clipboard
<script lang="ts"> import { usePathname } from "svelte-intlayer"; const links = [ { href: "/dashboard", label: "Dashboard" }, { href: "/settings", label: "Settings" }, ]; const pathname = usePathname();</script><nav> {#each links as link} <a href={link.href} style:font-weight={$pathname === link.href ? "bold" : "normal"} > {link.label} </a> {/each}</nav>Related
useLocale— current locale + locale switchergetPathWithoutLocale— the underlying utility used by this hook