생성:2025-11-16마지막 업데이트:2025-11-16
이 문서를 원하는 AI 어시스턴트에 참조하세요ChatGPTClaudeDeepSeekGoogle AI modeGeminiPerplexityMistralGrok
이 페이지와 원하는 AI 어시스턴트를 사용하여 문서를 요약합니다
MCP Server를 AI 어시스턴트에 추가
Intlayer MCP 서버를 통해 ChatGPT, DeepSeek, Cursor, VSCode 등에서 직접 문서를 검색할 수 있습니다.
MCP 서버 문서 보기버전 기록
- Initial documentationv7.1.02025. 11. 16.
이 페이지의 콘텐츠는 AI를 사용하여 번역되었습니다.
영어 원본 내용의 최신 버전을 보기문서 수정
이 문서를 개선할 아이디어가 있으시면 GitHub에 풀 리퀘스트를 제출하여 자유롭게 기여해 주세요.
문서에 대한 GitHub 링크복사
문서의 Markdown을 클립보드에 복사
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 options object with defaultLocale and mode
- Returns an object with prefix, and localePrefix properties
- Supports all routing modes: prefix-no-default, prefix-all, no-prefix, and search-params
- Lightweight utility for determining when to add locale prefixes
Function Signature
getPrefix( locale: Locales, // Required options?: { // Optional defaultLocale?: Locales; mode?: 'prefix-no-default' | 'prefix-all' | 'no-prefix' | 'search-params'; }): GetPrefixResulttype GetPrefixResult = { prefix: string; // e.g., 'fr/' or '' localePrefix?: Locale; // e.g., 'fr' or undefined}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 locale
- prefix-all: Returns prefix for all locales including default
- no-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
import { getPrefix, Locales } from "intlayer";// Check prefix for English localegetPrefix(Locales.ENGLISH, { defaultLocale: Locales.ENGLISH, mode: "prefix-all",});// Returns: { prefix: 'en/', localePrefix: 'en' }// Check prefix for French localegetPrefix(Locales.FRENCH, { defaultLocale: Locales.ENGLISH, mode: "prefix-no-default",});// Returns: { prefix: 'fr/', localePrefix: 'fr' }Different Routing Modes
import { getPrefix, Locales } from "intlayer";// prefix-all: Always returns prefixgetPrefix(Locales.ENGLISH, { mode: "prefix-all", defaultLocale: Locales.ENGLISH,});// Returns: { prefix: '/en', localePrefix: 'en' }// prefix-no-default: No prefix when locale matches defaultgetPrefix(Locales.ENGLISH, { mode: "prefix-no-default", defaultLocale: Locales.ENGLISH,});// Returns: { prefix: '', localePrefix: undefined }// prefix-no-default: Returns prefix when locale differs from defaultgetPrefix(Locales.FRENCH, { mode: "prefix-no-default", defaultLocale: Locales.ENGLISH,});// Returns: { prefix: 'fr/', localePrefix: 'fr' }// no-prefix & search-params: Never returns prefixgetPrefix(Locales.ENGLISH, { mode: "no-prefix" });// Returns: { prefix: '', localePrefix: undefined }getPrefix(Locales.ENGLISH, { mode: "search-params" });// Returns: { prefix: '', localePrefix: undefined }Practical Example
import { getPrefix, Locales } from "intlayer";// Build URLs with the appropriate prefix for a specific localeconst locale = Locales.FRENCH;const { prefix, localePrefix } = getPrefix(locale, { defaultLocale: Locales.ENGLISH, mode: "prefix-no-default",});// Using prefix for path constructionconst url1 = `/${prefix}about`.replace(/\/+/g, "/");// Result: "/fr/about"// Using localePrefix for locale identificationconsole.log(`Current locale: ${localePrefix}`);// Output: "Current locale: fr"Related Functions
- getLocalizedUrl: Generates a localized URL for a specific locale
- getMultilingualUrls: Generates URLs for all configured locales
TypeScript
type GetPrefixResult = { prefix: string; // The path prefix with trailing slash (e.g., 'fr/' or '') localePrefix?: Locale; // The locale identifier without slashes (e.g., 'fr' or undefined)};function getPrefix( locale: Locales, options?: { defaultLocale?: Locales; mode?: "prefix-no-default" | "prefix-all" | "no-prefix" | "search-params"; }): GetPrefixResult;