Creation:2025-11-16Last update:2025-11-16
Reference this doc to your favorite AI assistantChatGPTClaudeDeepSeekGoogle AI modeGeminiPerplexityMistralGrok
Ask your question and get a summary of the document by referencing this page and the AI provider of your choice
Version History
- "Initial documentation"v7.1.016/11/2025
The content of this page was translated using an AI.
See the last version of the original content in EnglishEdit this doc
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
Copy doc Markdown to clipboard
Documentation: getPrefix Function in intlayer
Description
The getPrefix function determines the URL prefix for a given locale based on the routing mode configuration. It compares the locale with the default locale and returns an object containing three different prefix formats for flexible URL construction.
Key Features:
- Takes a locale as the first parameter (required)
- Optional
optionsobject withdefaultLocaleandmode - Returns an object with
prefix, andlocalePrefixproperties - Supports all routing modes:
prefix-no-default,prefix-all,no-prefix, andsearch-params - Lightweight utility for determining when to add locale prefixes
Function Signature
Parameters
locale: Locales- Description: The locale to generate the prefix for. If the value is falsy (undefined, null, empty string), the function returns an empty string.
- Type:
Locales - Required: Yes
options?: object- Description: Configuration object for prefix determination.
- Type:
object Required: No (Optional)
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: Returns empty strings when locale matches default localeprefix-all: Returns prefix for all locales including defaultno-prefix: Returns empty strings (no prefix in URLs)search-params: Returns empty strings (locale in query parameters)
Returns
- Type:
GetPrefixResult - Description: An object containing three different prefix formats:
prefix: The path prefix with trailing slash (e.g.,'fr/','')localePrefix: The locale identifier without slashes (e.g.,'fr',undefined)
Example Usage
Basic Usage
Different Routing Modes
Practical Example
Related Functions
getLocalizedUrl: Generates a localised URL for a specific localegetMultilingualUrls: Generates URLs for all configured locales