# General Translation Integrations: 翻译数据
URL: https://generaltranslation.com/zh/docs/integrations/databricks/guides/translating-data.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 如何在 Databricks App 中使用 gt-node 翻译查询结果和模型回答。

Databricks App 中的数据 (例如查询结果和模型回答) 只在运行时才会产生。请在数据发送到浏览器之前，先在服务器端使用 `gt-node` 进行翻译。

## 开始之前 [#before-start]

请先完成[快速入门](/docs/integrations/databricks/quickstart)。以下所有示例均运行在 `/api` 下的路由中，这些路由中的请求区域设置已由快速入门配置的中间件通过 [`withGT`](/docs/node/reference/functions/with-gt) 完成绑定。

## 翻译查询结果 [#query-results]

对每个需要展示给用户的文本列调用 [`tx`](/docs/node/reference/functions/tx)。标识符、数字和日期保持原样即可：

```ts title="server/server.ts"
app.get('/api/products', async (_req, res) => {
  const rows = await getProducts(); // 替换为你自己的查询
  const translated = await Promise.all(
    rows.map(async (row) => ({
      ...row,
      description: await tx(row.description, {
        $context: 'Product description from the catalog table',
      }),
    }))
  );
  res.json(translated);
});
```

* `$context` 用于告诉翻译模型文本出现在何处。请描述文本的出处，而非其措辞，例如 &quot;销售仪表板中的列标题&quot;。
* `gt-node` 会缓存译文，因此在缓存条目有效期内，跨行或跨请求重复出现的文本只会翻译一次。条目会在默认有效期结束后过期；可通过 [`cacheExpiryTime`](/docs/node/reference/config#cache-expiry) 修改有效期。
* 当请求的区域设置为 `defaultLocale` 时，[`tx`](/docs/node/reference/functions/tx) 会原样返回文本。

## 翻译模型回答 [#model-answers]

要用访问者的语言显示模型的回答，请在模型返回结果后，在服务器端对其进行翻译：

```ts title="server/server.ts"
app.get('/api/summary', async (_req, res) => {
  const answer = await summarizeSales(); // 你的模型服务端点或智能体
  res.json({ answer: await tx(answer, { $context: 'Answer from a data assistant' }) });
});
```

如果模型本身就能用访问者的语言输出，则改为将区域设置传给模型。可通过 [`getLocale`](/docs/node/reference/functions/get-locale) 获取区域设置。

## 何时使用哪种方式 [#when]

* **在 `client/` 中使用 [`<T>`](/docs/react/reference/components/t)：** 适用于界面文本，在构建时翻译。面向用户的消息应放在客户端，以便 CLI 提前完成翻译。
* **在服务器端使用 [`tx`](/docs/node/reference/functions/tx)：** 适用于仅在运行时才能确定的数据，例如查询结果和模型回答。

## Next steps

- /docs/integrations/databricks/guides/deploying

## Sitemap

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