如何Efficiently Implement i18n:Claude Code 实战指南
学习如何efficiently implement i18n:Claude Code 实战. 包含实用代码示例和分步指导。
多言語支持でClaude Code 活きる場面
国際化(i18n)の实现は、翻訳文件の管理、ロケール切换、日期・数値のフォーマット、RTL支持など多岐にわたります。Claude Code 这らの作業を一括で処理し、翻訳漏れのチェックまで行えます。
Next.jsでのi18n基盤构建
> Next.js App Routerでnext-intl使用...的多言語支持を配置して。
> 日本語と英語をサポートして。URLパスにロケールを含める形式で。
目录结构
src/
messages/
ja.json
en.json
app/
[locale]/
layout.tsx
page.tsx
i18n/
request.ts
routing.ts
路由配置
// src/i18n/routing.ts
import { defineRouting } from 'next-intl/routing';
export const routing = defineRouting({
locales: ['ja', 'en'],
defaultLocale: 'ja',
pathnames: {
'/': '/',
'/about': {
ja: '/about',
en: '/about',
},
'/blog': {
ja: '/blog',
en: '/blog',
},
},
});
// src/i18n/request.ts
import { getRequestConfig } from 'next-intl/server';
import { routing } from './routing';
export default getRequestConfig(async ({ requestLocale }) => {
let locale = await requestLocale;
if (!locale || !routing.locales.includes(locale as 'ja' | 'en')) {
locale = routing.defaultLocale;
}
return {
locale,
messages: (await import(`../messages/${locale}.json`)).default,
};
});
翻訳文件
// src/messages/ja.json
{
"common": {
"siteName": "マイアプリ",
"navigation": {
"home": "ホーム",
"about": "概要",
"blog": "blog",
"contact": "お問い合わせ"
},
"footer": {
"copyright": "© {year} マイアプリ. All rights reserved."
}
},
"home": {
"hero": {
"title": "最高のサービスを提供します",
"subtitle": "{count, plural, =0 {ユーザーはまだいません} one {# 人のユーザー} other {# 人のユーザー}}が利用中"
}
}
}
// src/messages/en.json
{
"common": {
"siteName": "MyApp",
"navigation": {
"home": "Home",
"about": "About",
"blog": "Blog",
"contact": "Contact"
},
"footer": {
"copyright": "© {year} MyApp. All rights reserved."
}
},
"home": {
"hero": {
"title": "We provide the best service",
"subtitle": "{count, plural, =0 {No users yet} one {# user} other {# users}} using our service"
}
}
}
组件での使用
import { useTranslations } from 'next-intl';
export default function HomePage() {
const t = useTranslations('home');
const tc = useTranslations('common');
return (
<main>
<h1>{t('hero.title')}</h1>
<p>{t('hero.subtitle', { count: 1500 })}</p>
<nav aria-label={tc('navigation.home')}>
<a href="/">{tc('navigation.home')}</a>
<a href="/about">{tc('navigation.about')}</a>
</nav>
</main>
);
}
翻訳の一括生成
让 Claude Code翻訳文件の生成を依頼可以。
> src/messages/ja.json を元に英語の翻訳文件を生成して。
> 技術用語は那个まま英語で、UIテキストは自然な英語にして。
翻訳漏れのチェック
> ja.jsonとen.jsonのキーを比較して、翻訳漏れがないかチェックして。
> 不足しているキーがあれば添加して。
// scripts/check-translations.ts
import ja from '../src/messages/ja.json';
import en from '../src/messages/en.json';
function getKeys(obj: object, prefix = ''): string[] {
return Object.entries(obj).flatMap(([key, value]) => {
const path = prefix ? `${prefix}.${key}` : key;
if (typeof value === 'object' && value !== null) {
return getKeys(value as object, path);
}
return [path];
});
}
const jaKeys = new Set(getKeys(ja));
const enKeys = new Set(getKeys(en));
const missingInEn = [...jaKeys].filter(k => !enKeys.has(k));
const missingInJa = [...enKeys].filter(k => !jaKeys.has(k));
if (missingInEn.length) {
console.log('Missing in en.json:', missingInEn);
}
if (missingInJa.length) {
console.log('Missing in ja.json:', missingInJa);
}
日期・数値のフォーマット
import { useFormatter } from 'next-intl';
function PriceDisplay({ amount, date }: { amount: number; date: Date }) {
const format = useFormatter();
return (
<div>
<p>{format.number(amount, { style: 'currency', currency: 'USD' })}</p>
<time>{format.dateTime(date, { year: 'numeric', month: 'long', day: 'numeric' })}</time>
<p>{format.relativeTime(date)}</p>
</div>
);
}
// ja: $1,500 / 2026年3月24日 / 2週間前
// en: $1,500 / March 24, 2026 / 2 weeks ago
言語切换组件
'use client';
import { useRouter, usePathname } from 'next/navigation';
import { useLocale } from 'next-intl';
function LanguageSwitcher() {
const locale = useLocale();
const router = useRouter();
const pathname = usePathname();
const switchLocale = (newLocale: string) => {
const newPath = pathname.replace(`/${locale}`, `/${newLocale}`);
router.push(newPath);
};
return (
<div role="radiogroup" aria-label="Language selection">
<button
role="radio"
aria-checked={locale === 'ja'}
onClick={() => switchLocale('ja')}
className={locale === 'ja' ? 'font-bold' : ''}
>
日本語
</button>
<button
role="radio"
aria-checked={locale === 'en'}
onClick={() => switchLocale('en')}
className={locale === 'en' ? 'font-bold' : ''}
>
English
</button>
</div>
);
}
总结
借助 Claude Code,i18n基盤の构建から翻訳文件の生成、翻訳漏れのチェックまでを高效地進められます。尤其翻訳文件の一括生成と整合性チェックは、Claude Codeの強みが発揮される場面です。项目の多言語支持方針はCLAUDE.mdに記述しておくと一貫性を保てます。此外プロンプトテクニックを使って翻訳の品質を高めることも可以。
Claude Code 的详细信息请参阅Anthropic官方文档。
免费 PDF:5 分钟看懂 Claude Code 速查表
只需留下邮箱,我们就会立即把这份 A4 一页速查表 PDF 发送给你。
我们会严格保护你的个人信息,绝不发送垃圾邮件。
把 Claude Code 变成真正能带来结果的工作流
先领取中文说明的免费 PDF,再进入英文商品页选择合适的教材。如果你需要团队落地、流程设计或内容变现支持,也可以直接咨询。
本文作者
Masa
深度使用 Claude Code 的工程师。运营 claudecode-lab.com——一个涵盖 10 种语言、超过 2,000 页内容的科技媒体。
相关文章
每天发布多语言 Claude Code 文章前,要先检查的 7 件事
一份实用清单,帮助你每天发布多语言 Claude Code 文章时避免漏语言、CTA 错位和线上内容未更新。
Codex Automations 是什么?让 AI 在你睡觉时完成内容运营
用 Codex Automations 自动查看流量、选择主题、写文章、改善转化路径并部署网站的实用指南。
Claude Code × GCP Cloud Functions 完全指南 | 极速开发无服务器函数
用 Claude Code 高效开发 GCP Cloud Functions。从 HTTP/Pub/Sub/Firestore 触发器实现到本地测试、部署自动化,基于 Masa 的实战经验,附完整可运行代码示例。