# gt-node: General Translation Node.js SDK: getRequestLocale
URL: https://generaltranslation.com/en-US/docs/node/reference/functions/get-request-locale.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: Detect the best supported locale from a request's Accept-Language header with General Translation. API reference for getRequestLocale.

Extracts the preferred locale from a request's `Accept-Language` header and matches it against your configured locales. Use it to pick the locale to bind with [`withGT`](/docs/node/reference/functions/with-gt).

## Overview [#overview]

Call `getRequestLocale` with a request object. It returns the best matching supported locale, or the `defaultLocale` when no match is found.

```ts
import { getRequestLocale } from 'gt-node';

const locale = getRequestLocale(req); // 'fr'
```

Signature:

```ts
getRequestLocale(request: { headers: Record<string, string | string[] | undefined> }): string
```

*Note: Call [`initializeGT`](/docs/node/reference/functions/initialize-gt) before using `getRequestLocale` so it knows which locales are supported.*

## How it works [#how-it-works]

- **Header parsing.** It parses the request's `Accept-Language` header and negotiates the best match against your configured `locales`.
- **Fallback.** When no supported locale matches, it returns the configured `defaultLocale`.
- **Framework-agnostic.** It works with any framework that exposes headers as `Record<string, string | string[] | undefined>`, including Express and Fastify.

## Parameters [#parameters]

| Parameter | Description | Type | Optional | Default |
| --- | --- | --- | --- | --- |
| [`request`](#request) | An object with a `headers` property containing the request headers. | `{ headers: Record<string, string \| string[] \| undefined> }` | No | — |

### `request` [#request]

**Type** `{ headers: Record<string, string | string[] | undefined> }` · **Required**

An object with a `headers` property containing the request headers. Works with Express, Fastify, and any framework that exposes headers as `Record<string, string | string[] | undefined>`.

## Returns [#returns]

**Type** `string`

The best matching BCP 47 locale code from your configured locales, or the `defaultLocale` if no match is found.

## Examples [#examples]

```ts title="middleware/locale.js"
// Express middleware
import { withGT, getRequestLocale } from 'gt-node';

export function localeMiddleware(req, res, next) {
  const locale = getRequestLocale(req);
  withGT(locale, () => next());
}
```

```ts title="middleware/locale.js"
// Combining with other strategies — use getRequestLocale as a fallback
import { withGT, getRequestLocale } from 'gt-node';

export function localeMiddleware(req, res, next) {
  const locale =
    req.query.lang ||
    req.cookies?.locale ||
    getRequestLocale(req);

  withGT(locale, () => next());
}
```

## Sitemap

See the full [sitemap](https://generaltranslation.com/sitemap.md) for all pages.
