שאלו את שאלתכם וקבלו סיכום של המסמך על ידי הפניה לדף הזה ולספק ה-AI שתבחרו
היסטוריית גרסאות
- "Apply the locale prefix, and narrow the return type from the declared rewrite rules"v8.0.019.8.2026
- "Implement custom URL rewrites"v8.0.022.1.2026
התוכן של הדף הזה תורגם באמצעות AI.
צפייה בגרסה האחרונה של התוכן המקורי באנגליתאם יש לכם רעיון לשיפור התיעוד הזה, אל תהססו לתרום על ידי הגשת Pull Request ב-GitHub.
קישור GitHub לתיעודהעתקת ה-Markdown של המסמך ללוח
Documentation: getLocalizedPath Function in intlayer
Description
The getLocalizedPath function localizes a canonical path (internal application path): it resolves the custom rewrite rules, then applies the locale prefix of your routing mode. It is particularly useful for generating SEO-friendly URLs that vary by language.
It is the relative counterpart of getLocalizedUrl, for a relative input both return the same value. Unlike getLocalizedUrl, it never returns an absolute URL: the domains configuration is ignored, so a locale served from its own domain still yields a path. An absolute input is accepted, but its origin is dropped, only its path, query string and hash are kept.
Key Features:
- Supports dynamic route parameters using the
[param]syntax. - Resolves paths according to custom rewrite rules defined in your configuration.
- Applies the locale prefix of the configured routing mode (
prefix-no-default,prefix-all, …). - Automatically handles fallback to the canonical path if no rewrite rule is found for the specified locale.
- Narrows its return type: a literal path resolves to the localized string literal at compile time.
Function Signature
העתקת הקוד ללוח
Parameters
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.,
Optional Parameters
locale?: Locales- Description: The target locale for which the path should be localized.
- Type:
Locales - Default: The default locale of your project's configuration.
options?: object- Description: Routing overrides. Every entry defaults to your project's configuration.
Type:
objectoptions.locales?: Locales[]: supported locales. Default:configuration.internationalization.localesoptions.defaultLocale?: Locales: the default locale. Default:configuration.internationalization.defaultLocaleoptions.mode?: 'prefix-no-default' | 'prefix-all' | 'no-prefix' | 'search-params': how the locale appears in the path. Default:configuration.routing.modeoptions.rewrite?: RoutingConfig['rewrite']: custom rewrite rules. Default:configuration.routing.rewrite
Returns
- Type:
string - Description: The localized path for the specified locale.
The type is narrowed from the rewrite rules declared in your configuration, so the editor shows the resolved path rather than a bare string:
העתקת הקוד ללוח
The same narrowing flows into getLocalizedUrl, which applies the rewrite rules before prefixing the locale.
Two cases stay widened to string, because they cannot be resolved at compile time:
- a path that is not a string literal (e.g. one built from a variable);
- a path matched by a rule using a multi-segment or optional parameter (
[...slug],[[...slug]],:param?).
Example Usage
Basic Usage (With Configuration)
If you have configured custom rewrites in your intlayer.config.ts:
העתקת הקוד ללוח
Usage with Dynamic Routes
העתקת הקוד ללוח
Manual Rewrite Rules
You can also pass manual rewrite rules to the function:
העתקת הקוד ללוח
Omitting the Locale
When no locale is given, the path is localized for the configured default locale:
העתקת הקוד ללוח
