Skip to content

aymericzip/intlayer-skills / skills/intlayer-usage

intlayer-usage

Verified

Provides general guidelines for using Intlayer in any project. Use when the user asks to "get started with Intlayer", "declare content files", or understand the "project structure" for internationalization.

FileKindSize
LICENCE.mdmarkdown10.7 KB
SKILL.mdskill md3.6 KB
references/concept_how-works-intlayer.mdmarkdown28.7 KB
references/concept_per-locale-file.mdmarkdown7.6 KB
references/get-started.mdmarkdown26.6 KB
references/packages_intlayer_comparePaths.mdmarkdown5.0 KB
references/packages_intlayer_exports.mdmarkdown12.4 KB
references/packages_intlayer_getCanonicalPath.mdmarkdown3.4 KB
references/packages_intlayer_getConfiguration.mdmarkdown4.1 KB
references/packages_intlayer_getDictionary.mdmarkdown4.9 KB
references/packages_intlayer_getDictionaryAsync.mdmarkdown6.3 KB
references/packages_intlayer_getEnumeration.mdmarkdown3.3 KB
references/packages_intlayer_getHTMLTextDir.mdmarkdown2.3 KB
references/packages_intlayer_getIntlayer.mdmarkdown8.7 KB
references/packages_intlayer_getIntlayerAsync.mdmarkdown6.8 KB
references/packages_intlayer_getLocale.mdmarkdown1.1 KB
references/packages_intlayer_getLocaleLang.mdmarkdown1.9 KB
references/packages_intlayer_getLocaleName.mdmarkdown3.5 KB
references/packages_intlayer_getLocalizedPath.mdmarkdown6.8 KB
references/packages_intlayer_getLocalizedUrl.mdmarkdown9.3 KB
references/packages_intlayer_getMultilingualUrls.mdmarkdown8.7 KB
references/packages_intlayer_getPathWithoutLocale.mdmarkdown1.6 KB
references/packages_intlayer_getPrefix.mdmarkdown6.0 KB
references/packages_intlayer_getTranslation.mdmarkdown3.4 KB
references/packages_intlayer_validatePrefix.mdmarkdown1.0 KB
references/packages_react-intlayer_exports.mdmarkdown13.7 KB
references/packages_vite-intlayer_exports.mdmarkdown6.0 KB
references/packages_vite-intlayer_intlayer.mdmarkdown6.0 KB
references/packages_vite-intlayer_intlayerCompiler.mdmarkdown4.2 KB
references/packages_vite-intlayer_intlayerMinify.mdmarkdown3.9 KB
references/packages_vite-intlayer_intlayerProxy.mdmarkdown7.0 KB
references/packages_vite-intlayer_intlayerPrune.mdmarkdown4.7 KB
references/why.mdmarkdown58.2 KB

references/packages_intlayer_getPrefix.md

View on GitHub
---
createdAt: 2025-11-16
updatedAt: 2026-09-27
priority: 5
title: getPrefix Function Documentation | intlayer
description: "Use getPrefix to get the URL prefix of a locale according to your routing mode, in three formats ready for links and routes."
keywords:
  - getPrefix
  - prefix
  - Intlayer
  - intlayer
  - Internationalization
  - Documentation
  - Next.js
  - JavaScript
  - React
slugs:
  - doc
  - packages
  - intlayer
  - getPrefix
history:
  - version: 7.1.0
    date: 2025-11-16
    changes: "Initial documentation"
author: aymericzip
---

# 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

```typescript
getPrefix(
  locale: Locales,               // Required
  options?: {                    // Optional
    defaultLocale?: Locales;
    mode?: 'prefix-no-default' | 'prefix-all' | 'no-prefix' | 'search-params';
  }
): GetPrefixResult

type 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`](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/configuration.md#middleware)

  - `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`](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/configuration.md#middleware)
    - **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

```typescript codeFormat={["typescript", "esm", "commonjs"]}
import { getPrefix, Locales } from "intlayer";

// Check prefix for English locale
getPrefix(Locales.ENGLISH, {
  defaultLocale: Locales.ENGLISH,
  mode: "prefix-all",
});
// Returns: { prefix: 'en/', localePrefix: 'en' }

// Check prefix for French locale
getPrefix(Locales.FRENCH, {
  defaultLocale: Locales.ENGLISH,
  mode: "prefix-no-default",
});
// Returns: { prefix: 'fr/', localePrefix: 'fr' }
```

### Different Routing Modes

```typescript
import { getPrefix, Locales } from "intlayer";

// prefix-all: Always returns prefix
getPrefix(Locales.ENGLISH, {
  mode: "prefix-all",
  defaultLocale: Locales.ENGLISH,
});
// Returns: { prefix: '/en', localePrefix: 'en' }

// prefix-no-default: No prefix when locale matches default
getPrefix(Locales.ENGLISH, {
  mode: "prefix-no-default",
  defaultLocale: Locales.ENGLISH,
});
// Returns: { prefix: '', localePrefix: undefined }

// prefix-no-default: Returns prefix when locale differs from default
getPrefix(Locales.FRENCH, {
  mode: "prefix-no-default",
  defaultLocale: Locales.ENGLISH,
});
// Returns: { prefix: 'fr/', localePrefix: 'fr' }

// no-prefix & search-params: Never returns prefix
getPrefix(Locales.ENGLISH, { mode: "no-prefix" });
// Returns: { prefix: '', localePrefix: undefined }

getPrefix(Locales.ENGLISH, { mode: "search-params" });
// Returns: { prefix: '', localePrefix: undefined }
```

### Practical Example

```typescript
import { getPrefix, Locales } from "intlayer";

// Build URLs with the appropriate prefix for a specific locale
const locale = Locales.FRENCH;
const { prefix, localePrefix } = getPrefix(locale, {
  defaultLocale: Locales.ENGLISH,
  mode: "prefix-no-default",
});

// Using prefix for path construction
const url1 = `/${prefix}about`.replace(/\/+/g, "/");
// Result: "/fr/about"

// Using localePrefix for locale identification
console.log(`Current locale: ${localePrefix}`);
// Output: "Current locale: fr"
```

## Related Functions

- [`getLocalizedUrl`](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/packages/intlayer/getLocalizedUrl.md)
- [`getMultilingualUrls`](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/packages/intlayer/getMultilingualUrls.md)

## TypeScript

```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;
```
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall