Ask your question and get a summary of the document by referencing this page and the AI provider of your choice
Version History
- "Refactored to use options parameter with mode instead of prefixDefault"v7.1.011/16/2025
- "Init history"v5.5.106/29/2025
If 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: getMultilingualUrls Function in intlayer
Description
The getMultilingualUrls function generates a mapping of multilingual URLs by prefixing the given URL with each supported locale. It can handle both absolute and relative URLs, applying the appropriate locale prefix based on the provided configuration or defaults.
Key Features:
- Only 1 parameter is required:
url - Optional
optionsobject withlocales,defaultLocale, andmode - Uses your project's internationalization configuration as defaults
- Supports multiple routing modes:
prefix-no-default,prefix-all,no-prefix, andsearch-params - Returns a mapping object with all locales as keys and their corresponding URLs as values
Function Signature
Parameters
Required Parameters
url: string- Description: The original URL string to be prefixed with locales.
- Type:
string - Required: Yes
Optional Parameters
options?: object- Description: Configuration object for URL localization behavior.
- Type:
object Required: No (Optional)
options.locales?: Locales[]- Description: Array of supported locales. If not provided, uses the configured locales from your project configuration.
- Type:
Locales[] - Default:
Project Configuration
options.defaultLocale?: Locales- Description: The default locale for the application. If not provided, uses the configured default locale from your project configuration.
- Type:
Locales - Default:
Project Configuration
options.mode?: 'prefix-no-default' | 'prefix-all' | 'no-prefix' | 'search-params'- Description: The URL routing mode for locale handling. If not provided, uses the configured mode from your project configuration.
- Type:
'prefix-no-default' | 'prefix-all' | 'no-prefix' | 'search-params' - Default:
Project Configuration - Modes:
prefix-no-default: No prefix for default locale, prefix for all othersprefix-all: Prefix for all locales including defaultno-prefix: No locale prefix in URLsearch-params: Use query parameters for locale (e.g.,?locale=fr)
Returns
- Type:
StrictModeLocaleMap<string> - Description: An object mapping each locale to its corresponding multilingual URL.
Example Usage
Basic Usage (Uses Project Configuration)
Relative URLs with Options
Absolute URLs
Different Routing Modes
Edge Cases
No Locale Segment:
- The function removes any existing locale segment from the URL before generating the multilingual mappings.
Default Locale:
- When
modeis'prefix-no-default', the function does not prefix the URL for the default locale. - When
modeis'prefix-all', the function prefixes all locales including the default.
- When
Unsupported Locales:
- Only the locales provided in the
localesarray are considered for generating the URLs.
- Only the locales provided in the
Routing Modes:
'prefix-no-default': Default locale has no prefix, others do (e.g.,/dashboard,/fr/dashboard)'prefix-all': All locales have prefixes (e.g.,/en/dashboard,/fr/dashboard)'no-prefix': No locale prefixes in URLs (all locales return same URL)'search-params': Locale specified via query parameter (e.g.,/dashboard?locale=fr)
Usage in Applications
In a multilingual application, configuring the internationalization settings with locales and defaultLocale is critical for ensuring the correct language is displayed. Below is an example of how getMultilingualUrls can be used in an application setup:
The above configuration ensures that the application recognizes ENGLISH, FRENCH, and SPANISH as supported languages and uses ENGLISH as the fallback language.
Using this configuration, the getMultilingualUrls function can dynamically generate multilingual URL mappings based on the application's supported locales:
By integrating getMultilingualUrls, developers can maintain consistent URL structures across multiple languages, enhancing both user experience and SEO.