复制安装命令
用 Codex 或 Claude 安装复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它先审查 Skill 页面再帮你安装。
复制前请先查看来源、License 和安全提示。
The type-safe i18n toolkit for TypeScript
用 Codex 或 Claude 安装复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它先审查 Skill 页面再帮你安装。
复制前请先查看来源、License 和安全提示。
来源文件:README.md
The type-safe i18n toolkit for TypeScript
better-translate.com »
Docs
·
Quick Start
·
Issues
Better Translate is a type-safe i18n toolkit for TypeScript. Write your translation config once and it works across Next.js, Astro, React, TanStack Router, Node, Bun, and Expo — same API, same types, same experience everywhere. A built-in CLI generates translated locale files using any AI provider or local Ollama models, so you ship multilingual apps without manual work.
i18n in TypeScript projects is usually an afterthought. You wire up a library, scatter translation keys across your codebase, and cross your fingers that nothing drifts out of sync. Better Translate treats translations as first-class TypeScript: keys are inferred, interpolations are typed, and the CLI handles generation so you never write a locale file by hand again.
# Install the CLI globally
npm install -g @better-translate/cli
# Install the core package (or a framework adapter)
npm install @better-translate/core
Create a better-translate.config.ts at the root of your project:
import { createGoogleGenerativeAI } from "@ai-sdk/google";
import { defineConfig } from "@better-translate/cli";
const google = createGoogleGenerativeAI({ apiKey: process.env.GOOGLE_API_KEY });
export default defineConfig({
model: google("gemini-2.0-flash"),
defaultLocale: "en",
targetLocales: ["es", "fr", "ja"],
messagesDir: "./messages",
});
Extract source strings and generate all locale files:
bt extract # scans source files and updates messages/en.json
bt generate # translates into every target locale
Use translations in your app:
import { configureTranslations, createTranslationHelpers } from "@better-translate/core";
import en from "./messages/en.json";
import es from "./messages/es.json";
const translator = await configureTranslations({
availableLocales: ["en", "es"] as const,
defaultLocale: "en",
messages: { en, es },
});
const { t } = createTranslationHelpers(translator);
t("hero.title") // fully typed, autocomplete works
t("hero.greeting", { params: { name } }) // interpolation is type-checked
t("hero.title", { locale: "es" }) // per-call locale override
| Framework / Runtime | Package |
|---|---|
| Next.js (App Router) | @better-translate/nextjs |
| Astro | @better-translate/astro |
| React | @better-translate/react |
| TanStack Router / Start | @better-translate/tanstack-router |
| Expo (React Native) | @better-translate/react |
| Node.js | @better-translate/core |
| Bun | @better-translate/core |
| Markdown / MDX | @better-translate/md |
t(".bt extract pulls source strings out of your code. bt generate calls your AI model and writes every locale file.Better Translate is free and open source under the MIT License. Contributions are welcome.
If you discover a security vulnerability, please see SECURITY.md for responsible disclosure instructions.
name: core
description: Smallest Better Translate setup for plain TypeScript, Node.js, Bun, APIs, and shared libraries.Use this guide when you want the smallest possible Better Translate setup.
@better-translate/core is the base translation engine.
Use it when:
Create one translator file and export it:
import { configureTranslations } from "@better-translate/core";
const en = {
home: {
title: "Hello",
},
} as const;
const es = {
home: {
title: "Hola",
},
} as const;
export const translator = await configureTranslations({
availableLocales: ["en", "es"] as const,
defaultLocale: "en",
fallbackLocale: "en",
messages: { en, es },
});
TypeScript key autocomplete depends on the shared exported translator.
as consttranslatortranslator.t(...) from code that needs typed keysimport { translator } from "./i18n";
translator.t("home.title");
translator.t("home.title", { locale: "es" });
That keeps locale values and translation keys inferred from the messages you configured.
@better-translate/react when React components need hooks or locale switching@better-translate/nextjs when Next.js routes or server helpers need locale awareness@better-translate/astro when Astro requests need locale awareness@better-translate/md when content files should follow the same locale setupUse { bt: true } in any t() call to write source text directly instead of inventing a key:
t("Hello world", { bt: true });
Then run npx bt extract to generate keys automatically and rewrite the calls. See skills/cli/SKILL.md for the full setup.
Do not create one translation system per framework.
Create one core translator first, then let adapters read from it.
评论 (0)
暂无评论,成为第一个评论者吧!