# gt-node: General Translation Node.js SDK: 将翻译存储在本地 URL: https://generaltranslation.com/zh/docs/node/guides/storing-translations.mdx --- title: 将翻译存储在本地 description: 如何在 gt-node 中从本地文件而非 CDN 加载 General Translation 翻译。 related: links: - /docs/node/guides/configuring - /docs/node/guides/translating-strings - /docs/node/guides/detecting-locale --- 默认情况下,`gt-node` 可以从 General Translation CDN 加载翻译。或者,你也可以生成翻译文件,随服务一同发布,并通过 [`loadTranslations`](/docs/react/reference/functions/load-translations) 回调加载这些文件。这样可以消除对运行时 CDN 的依赖,但代价是每次更新翻译都需要重新部署。 ## 配置本地输出路径 [#configure] 在 `gt.config.json` 中,使用 `[locale]` 占位符,将 `gt` 文件的输出路径设置为项目中的某个路径。 ```json title="gt.config.json" { "defaultLocale": "en", "locales": ["es", "fr"], "files": { "gt": { "output": "translations/[locale].json" } } } ``` ## 生成文件 [#generate] 运行 CLI,翻译你的内容,并为每个区域设置生成一个文件。 ```bash npx gt translate ``` 将其加入构建或部署流程,这样这些文件就能保持最新。 ## 启动时加载这些翻译 [#load] 向 [`initializeGT`](/docs/node/reference/functions/initialize-gt) 传入一个 `loadTranslations` 回调,用于读取对应区域设置的文件。 ```ts title="server.js" import { readFile } from 'node:fs/promises'; import { initializeGT } from 'gt-node'; initializeGT({ defaultLocale: 'en', locales: ['en', 'es', 'fr'], loadTranslations: async (locale) => JSON.parse(await readFile(`./translations/${locale}.json`, 'utf8')), }); ``` 启用本地加载器后,翻译会从磁盘读取,不会发起 CDN 请求。要切换回通过 CDN 交付,请移除 `loadTranslations` 并提供 `projectId` (请参阅[配置 gt-node](/docs/node/guides/configuring#delivery)) 。 ## Next steps - /docs/node/guides/configuring - /docs/node/guides/translating-strings - /docs/node/guides/detecting-locale