Ask your question and get a summary of the document by referencing this page and the AI provider of your choice
Version History
- "Implement custom URL rewrites"v8.0.022/01/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
Documentation: getLocalizedPath function in intlayer
Description
The getLocalizedPath function resolves a canonical path (internal application path) into its localised equivalent based on the provided locale and rewrite rules. It is particularly useful for generating SEO-friendly URLs that vary by language.
Key Features:
- Supports dynamic route parameters using the
[param]syntax. - Resolves paths according to custom rewrite rules defined in your configuration.
- Automatically handles fallback to the canonical path if no rewrite rule is found for the specified locale.
Function Signature
Copy the code to the clipboard
getLocalizedPath( canonicalPath: string, // Required locale: Locales, // Required rewriteRules?: RoutingConfig['rewrite'] // Optional): stringParameters
Required Parameters
canonicalPath: string- Description: The internal application path (e.g.,
/about,/product/[id]). - Type:
string - Required: Yes
- Description: The internal application path (e.g.,
locale: Locales- Description: The target locale for which the path should be localised.
- Type:
Locales - Required: Yes
Optional Parameters
rewriteRules?: RoutingConfig['rewrite']- Description: An object defining custom rewrite rules. If not provided, it defaults to the
routing.rewriteproperty from your project's configuration. - Type:
RoutingConfig['rewrite'] - Default:
configuration.routing.rewrite
- Description: An object defining custom rewrite rules. If not provided, it defaults to the
Returns
- Type:
string - Description: The localised path for the specified locale.
Example Usage
Basic Usage (With Configuration)
If you have configured custom rewrites in your intlayer.config.ts:
Copy the code to the clipboard
import { getLocalizedPath, Locales } from "intlayer";// Configuration: { '/about': { en: '/about', fr: '/a-propos' } }getLocalizedPath("/about", Locales.FRENCH);// Output: "/a-propos"getLocalizedPath("/about", Locales.ENGLISH);// Output: "/about"Usage with Dynamic Routes
Copy the code to the clipboard
import { getLocalizedPath, Locales } from "intlayer";// Configuration: { '/product/[id]': { en: '/product/[id]', fr: '/produit/[id]' } }getLocalizedPath("/product/123", Locales.FRENCH);// Output: "/produit/123"Manual Rewrite Rules
You can also pass manual rewrite rules to the function:
Copy the code to the clipboard
import { getLocalizedPath, Locales } from "intlayer";const manualRules = { "/contact": { en: "/contact-us", fr: "/contactez-nous", },};getLocalizedPath("/contact", Locales.FRENCH, manualRules);// Output: "/contactez-nous"Related Functions
getCanonicalPath: Resolves a localised path back to its internal canonical path.getLocalizedUrl: Generates a fully localised URL (including the protocol, host and locale prefix).