# gt-node: General Translation Node.js SDK: Detecting the request locale
URL: https://generaltranslation.com/en-US/docs/node/guides/detecting-locale.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: How to resolve and bind a locale for each request with General Translation in gt-node.

Every translation call in `gt-node` runs against the current request's locale. You resolve that locale from the request and bind it with [`withGT`](/docs/node/reference/functions/with-gt), so all translation functions downstream use it.

## Bind a locale with `withGT` [#with-gt]

Wrap request handling in `withGT(locale, fn)`. It stores the locale for the duration of the request so [`getGT`](/docs/node/reference/functions/get-gt), [`getMessages`](/docs/node/reference/functions/get-messages), [`tx`](/docs/node/reference/functions/tx), and [`getLocale`](/docs/node/reference/functions/get-locale) resolve against it.

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

app.use((req, res, next) => withGT(resolveLocale(req), () => next()));
```

*Note: Calling a translation or locale function outside a [`withGT`](/docs/node/reference/functions/with-gt) scope throws in development; in production it falls back to the default locale.*

## Detect from `Accept-Language` [#accept-language]

Use [`getRequestLocale`](/docs/node/reference/functions/get-request-locale) to pick the best supported locale from the request's `Accept-Language` header, falling back to the default locale.

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

app.use((req, res, next) => withGT(getRequestLocale(req), () => next()));
```

## Detect from your own signals [#custom]

To honor an explicit user choice, resolve the locale yourself — for example from a cookie or path segment — and fall back to [`getRequestLocale`](/docs/node/reference/functions/get-request-locale). Pass the result to [`withGT`](/docs/node/reference/functions/with-gt).

```ts
function resolveLocale(req) {
  return req.cookies?.locale || getRequestLocale(req);
}

app.use((req, res, next) => withGT(resolveLocale(req), () => next()));
```

[`withGT`](/docs/node/reference/functions/with-gt) resolves the value you pass against your configured `locales`, so an unsupported locale falls back to the default. Read the active locale anywhere in a request with [`getLocale`](/docs/node/reference/functions/get-locale).

## Next steps

- /docs/node/guides/translating-strings
- /docs/node/guides/configuring
- /docs/node/guides/storing-translations

## Sitemap

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