# General Translation Python SDKs: msg
URL: https://generaltranslation.com/zh/docs/python/reference/functions/msg.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 在 General Translation Python 中将消息及其翻译选项注册为编码后的字符串。msg 的 API 参考。

返回一个将消息及其翻译选项一并包含在内的编码后的字符串，以便后续解码和解析。当你在一处构建消息、在另一处解析时，请使用此功能。

## 概览 [#overview]

调用 `msg` 时，传入源字符串以及任意变量或保留选项。如果不传关键字参数，它会原样返回该消息；如果传入选项，则会返回一个编码后的 `{interpolated}:{base64options}` 字符串，[`m_fallback`](/docs/python/reference/functions/m-fallback)、[`decode_msg`](/docs/python/reference/functions/decode-msg) 和 [`decode_options`](/docs/python/reference/functions/decode-options) 都可以读取它。

```python
from gt_i18n import msg

msg("Save", _context="button")
```

签名：

```python
msg(message: str, **kwargs: object) -> str
```

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

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

* **无选项快捷方式。** 当 `kwargs` 为空时，会直接原样返回 `message`。
* **插值。** 它会使用用户变量 (移除保留键后) 对消息进行插值，生成便于阅读的前缀。
* **编码。** 它会计算消息哈希 (或复用传入的 `__hash`) ，然后将包含所有 kwargs 以及 `__source` 和 `__hash` 的 JSON 选项 payload 进行 base64 编码，并返回 `"{interpolated}:{encoded}"`。
* **错误兜底。** 如果插值失败，会返回原始消息。

## 参数 [#parameters]

| 参数                    | 描述                                               | Type  | 可选 | 默认 |
| --------------------- | ------------------------------------------------ | ----- | -- | -- |
| [`message`](#message) | ICU MessageFormat 的源字符串。                         | `str` | 否  | —  |
| [`kwargs`](#kwargs)   | 插值变量和保留的 GT 选项 (`_context`、`_id`、`_max_chars`) 。 | 关键字参数 | 是  | —  |

### `message` [#message]

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

用于注册的 ICU MessageFormat 源字符串。

### `kwargs` [#kwargs]

**类型** 关键字参数 · **可选**

插值变量以及保留选项 `_context`、`_id` 和 `_max_chars`，它们都会被纳入哈希计算。所有键都会保留在编码后的 payload 中。

## 返回值 [#returns]

**类型** `str`

未提供选项时，消息保持不变；否则，返回编码后的 `{interpolated}:{base64options}` 字符串。

## 示例 [#example]

```python
from gt_i18n import msg, decode_msg, decode_options

encoded = msg("Hi, {name}!", name="Bob", _id="greet")
decode_msg(encoded)      # "Hi, Bob!"
decode_options(encoded)  # {"name": "Bob", "_id": "greet", "__source": "Hi, {name}!", "__hash": "..."}
```

## Sitemap

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