# @halofe/global-locale 中文文档

规范地址：https://npmcn.com/package/%40halofe/global-locale/

版本：1\.0\.3
npm 元数据更新时间：2022-04-05T16:45:29.137Z
中文译文更新时间：2026-11-17T06:54:17.945Z
软件包许可：ISC

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40halofe/global-locale
原文对照：https://npmcn.com/package/%40halofe/global-locale/?lang=original

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：60.3 kB，14 个文件
- 模块格式：CommonJS
- TypeScript 类型：未声明
- 运行依赖：0 个
- 首次发布：2022-02-16T07:43:23.812Z
- 最新版本发布：2022-02-16T08:03:36.074Z
- 近 30 天下载量：7837（2026-10-17 至 2026-11-15，npm 官方下载统计）
- 已发布版本数：3（近 12 个月 0 个）

## 中文简介

语言信息管理器

## 安装

```sh
npm install @halofe/global-locale
```

也可以使用：`pnpm add @halofe/global-locale`、`yarn add @halofe/global-locale`、`bun add @halofe/global-locale`

## README

## @halofe/global-locale

### 主要特征

-   提供获取和设置地区/语言/货币/时区等 locale 信息能力
-   遵循[国际化开发规约](https://yuque.antfin.com/ioc-doc/must-doc)
-   抹平不同容器(web/weex/windVane/小程序)之间的差异

### 安装

```
tnpm install @halofe/global-locale --save
```

### API

#### getLocale

获取当前 locale 信息

```js
import locale from "@halofe/global-locale";

const { region, lang, currency, tz } = locale.getLocale();

console.log(region, lang, currency, tz); // CN, zh-CN, CNY, Asia/Shanghai
```

注意: 所有的 locale 信息的 set 操作, 在 web 环境下生效, weex 和 windVane 跟随应用的设置.

#### setRegion

> locale cookie 存储默认 domain 为 location.hostname, path 为'/', 时间为一个月

设置地区, 地区编码见[https://yuque.antfin.com/ioc-1/specification/region](https://yuque.antfin.com/ioc-1/specification/region)

```js
import locale from "@halofe/global-locale";
locale.setRegion("CN");
```

#### setLang

设置语言, 语言编码见[https://yuque.antfin.com/ioc-1/specification/language](https://yuque.antfin.com/ioc-1/specification/language)

```js
import locale from "@halofe/global-locale";
locale.setLang("zh-CN");
```

#### setCurrency

设置货币, 货币编码见[https://yuque.antfin.com/ioc-1/specification/currency](https://yuque.antfin.com/ioc-1/specification/currency)

```js
import locale from "@halofe/global-locale";
locale.setCurrency("CNY");
```

#### setTimeZone

设置时区, 时区编码见[https://yuque.antfin.com/ioc-1/specification/time-zone](https://yuque.antfin.com/ioc-1/specification/time-zone)

````
```js
import locale from '@halofe/global-locale';
locale.setTimeZone('CNY');
````

#### setLocale

设置自定义的 locale 信息

```js
import locale from "@halofe/global-locale";
locale.setLocale("site", "rus");

const localeInfo = locale.getLocale();
console.log(localeInfo.site); // rus
```

#### 提供标准的 cookie 读写函数

```js
import { Cookie } from '@halofe/global-locale';

/**
 * 获取 cookie 的值
 * @param {string} key , the key of cookie , if not set, will return all the cookie
 */
Cookie.get('x-hng')  ==> //region=CN&lang=zh-CN&currency=CNY

/**
 * 设置 cookie 的值
 * @param {string} key
 * @param {string} value
 * @param {object} attributes
 */
Cookie.set('x-hng','region=CN&lang=zh-CN&currency=CNY', {
  maxAge: 24*30*60*60, //最好设置 cookie 的过期时间, 不然 cookie 的过期时间就是 session 时间, 也就是关闭窗口的时间
  path: '/', // domain 为当前页面 domain
})

/**
 * 移除 cookie
 * @param {string} key
 */
Cookie.remove('x-hng')

```

### 自定义 Locale 信息,

我们提供用户自定义的方式来处理 locale 信息.

```js
const myLocale = locale.create({
  key: "my-name", // lolale 存储的 cookie 的 key 或者 在 windvane / weex 等环境下 locale 存放的位置, 默认为 x-hng
  domain: ".lazada.vn", // cookie domain, 默认为  location.hostname
  path: "/", // cookie path, 默认为'/',
  maxAge: 30 * 24 * 60 * 60, // cookie 过期时间, 秒为单位, 默认时长为 180 天.
});
```

## 常见问题

### @halofe/global-locale 是什么？

语言信息管理器

### 如何安装 @halofe/global-locale？

在项目目录运行 npm install @halofe/global-locale，或使用 pnpm add / yarn add / bun add。

### @halofe/global-locale 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/halofe\_\_global-locale 是否存在。

### @halofe/global-locale 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @halofe/global-locale 可以商用吗？

@halofe/global-locale 使用 ISC 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @halofe/global-locale 的下载量有多少？

根据 npm 官方下载统计，@halofe/global-locale 在 2026/10/17 – 2026/11/15 这 30 天内被下载 7,837 次，日均约 261 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @halofe/global-locale 还在维护吗？

最新版本 1\.0\.3 发布于 2022/02/16（4 年前）；至今共发布 3 个版本，其中过去 12 个月发布了 0 个；首次发布于 2022/02/16。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

