# gt-node: General Translation Node.js SDK: getLocale
URL: https://generaltranslation.com/zh/docs/node/reference/functions/get-locale.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 在 General Translation gt-node 中读取当前请求的生效区域设置。getLocale 的 API 参考。

以 BCP 47 代码形式返回生效区域设置。在 [`withGT`](/docs/node/reference/functions/with-gt) 层级内调用它，以读取绑定到该请求的区域设置。

## 概览 [#overview]

调用不传参数的 `getLocale`，即可获取当前使用的 BCP 47 区域设置代码。

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

const locale = getLocale(); // 'en-US'
```

签名：

```ts
getLocale(): string
```

*注意：必须在 [`withGT`](/docs/node/reference/functions/with-gt) 的回调函数中调用 `getLocale`，这样它才能知道应使用哪个区域设置。*

## 工作原理 [#how-it-works]

* **请求范围内。** 它会读取外围 [`withGT`](/docs/node/reference/functions/with-gt) 调用绑定的区域设置。
* **同步。** 它会立即返回，无需使用 await。

## 参数 [#parameters]

`getLocale` 不接受任何参数。

## 返回 [#returns]

**类型** `string`

当前 BCP 47 区域设置代码 (例如 `'en-US'`) 。

## 示例 [#examples]

```ts title="handler.js"
// 基本用法
import { withGT, getLocale } from 'gt-node';

app.use((req, res, next) => {
  const locale = req.headers['accept-language']?.split(',')[0] || 'en-US';
  withGT(locale, () => {
    console.log(getLocale()); // 'en-US'
    next();
  });
});
```

## Sitemap

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