# General Translation Python SDKs: hash_message
URL: https://generaltranslation.com/zh/docs/python/reference/functions/hash-message.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 对 General Translation Python 中用于翻译查找的 ICU MessageFormat 字符串进行哈希处理。hash_message 的 API 参考。

返回通过对字符串进行哈希计算得到的、用于查找其翻译的十六进制键。这与 [`t`](/docs/python/reference/functions/t) 内部计算的哈希值相同，因此你可以用它来构建或检查翻译字典。

## 概览 [#overview]

使用源字符串以及可选的仅限关键字参数上下文、id 和 max-chars 调用 `hash_message`。它会先为消息中的变量建立索引，然后在 `data_format="ICU"` 下计算哈希值。

```python
from gt_i18n import hash_message

h = hash_message("Hello, world!")
```

签名：

```python
hash_message(
    message: str,
    *,
    context: str | None = None,
    id: str | None = None,
    max_chars: int | None = None,
) -> str
```

从 `gt_i18n` 导入 `hash_message`。框架包不会再次导出它。

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

* **为变量编制索引。** 它会先通过为消息中的变量编制索引，将消息标准化，这样等价的消息就会生成相同的哈希。
* **哈希。** 它会将标准化后的消息与 `context`、`id` 和 `max_chars` 一起计算哈希，这与 JS gt-i18n 的实现一致。更改其中任意一项都会生成不同的哈希值。

## 参数 [#parameters]

| 参数                        | 描述                     | 类型            | 可选 | 默认值    |
| ------------------------- | ---------------------- | ------------- | -- | ------ |
| [`message`](#message)     | ICU MessageFormat 源字符串 | `str`         | 否  | —      |
| [`context`](#context)     | 用于消歧的上下文字符串            | `str \| None` | 是  | `None` |
| [`id`](#id)               | 显式消息标识符                | `str \| None` | 是  | `None` |
| [`max_chars`](#max-chars) | 最大字符数限制                | `int \| None` | 是  | `None` |

### `message` [#message]

**类型** `str` · **必填**

用于计算 哈希 的 ICU MessageFormat 源字符串。

### `context` [#context]

**类型** `str | None` · **可选** · **默认值** `None`

可选的上下文 (即 [`t`](/docs/python/reference/functions/t) 中的 `_context` 选项) ，用于区分相同的源文本。

### `id` [#id]

**类型** `str | None` · **可选** · **默认值** `None`

可选的显式指定消息标识符 ([`t`](/docs/python/reference/functions/t) 中的 `_id` 选项) 。

### `max_chars` [#max-chars]

**类型** `int | None` · **可选** · **默认值** `None`

可选的最大字符数限制 (在 [`t`](/docs/python/reference/functions/t) 中的 `_max_chars` 选项) 。

## 返回值 [#returns]

**类型** `str`

一个十六进制哈希字符串，用作翻译查找的键。

## 示例 [#example]

```python
from gt_i18n import hash_message

# 构建以哈希值为键的翻译字典
translations = {
    hash_message("Hello, world!"): "Hola, mundo!",
}
```

## Sitemap

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