Communitygithub.com

YepAPI/skills

next-intl 与 react-i18next 国际化模式:本地化路由、消息提取、复数化、RTL 支持及 Intl API 格式化。

skills 是什么?

此技能为构建国际化(i18n)应用的开发者提供了权威指南,覆盖两大主流 React 框架:基于 Next.js App Router 的 `next-intl` 和用于 React SPA 的 `react-i18next`。其核心价值在于提供一套清晰、可执行的规则集,涵盖本地化路由(如 `/en/about`)、按命名空间拆分的 JSON 翻译文件管理(如 `auth.json`、`dashboard.json`)、ICU 消息格式的复数化处理、通过 `Intl.DateTimeFormat` 等 API 实现日期/数字/货币格式化,以及完善的 RTL(从右至左)支持方案。该技能还通过具体的代码示例(如 middleware.ts 的配置、useTranslations() 和 useFormatter() 的使用)展示了常见模式,并明确列出了必须避免的陷阱(如硬编码字符串、不使用逻辑 CSS 属性)。适用于希望快速、规范地在其项目中集成多语言支持,并确保 SEO 友好和用户体验一致性的开发团队。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/i18n

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

i18n

规则

  • Next.js App Router:使用 next-intl — useTranslations() 钩子、NextIntlClientProvider、用于本地化检测的中间件
  • React SPA:使用 react-i18next — useTranslation() 钩子、使用 i18next-http-backend 进行懒加载的 i18n.init()
  • 本地化路由:/en/about、/fr/about — 使用中间件检测 Accept-Language 头并进行重定向
  • 翻译文件:每个区域每个命名空间一个 JSON 文件 — messages/en/common.json、messages/fr/common.json
  • 命名空间拆分:每个功能/页面一个命名空间 — auth.json、dashboard.json、settings.json — 只加载页面所需的资源
  • 复数化:使用 ICU 消息格式 — {count, plural, one {# 个项目} other {# 个项目}}
  • 插值:t('greeting', { name }) — 永远不要拼接翻译后的字符串
  • 日期/数字/货币:使用 Intl.DateTimeFormat、Intl.NumberFormat — 永远不要硬编码格式
  • RTL 支持:在 <html> 上使用 dir="rtl"、CSS 逻辑属性(使用 margin-inline-start 而非 margin-left)、使用阿拉伯语/希伯来语进行测试

模式

// next-intl: middleware.ts
import createMiddleware from "next-intl/middleware";
export default createMiddleware({
  locales: ["en", "fr", "de", "ar"],
  defaultLocale: "en",
});

// next-intl: 组件用法
import { useTranslations } from "next-intl";
function ProductCard({ price, date }: { price: number; date: Date }) {
  const t = useTranslations("products");
  const format = useFormatter();
  return (
    <div>
      <h2>{t("title")}</h2>
      <p>{format.number(price, { style: "currency", currency: "USD" })}</p>
      <p>{format.dateTime(date, { dateStyle: "medium" })}</p>
    </div>
  );
}

避免

  • 硬编码面向用户的字符串 — 每个字符串都必须通过 t() 处理
  • 为翻译后的文本进行字符串拼接 — 使用插值或 ICU 格式
  • 预先加载所有语言环境 — 按区域和命名空间进行懒加载
  • 在布局中使用 CSS left/right — 使用逻辑属性以确保 RTL 兼容性
  • 仅将语言环境存储在 localStorage 中 — 使用基于 URL 的语言环境以确保 SEO 和可分享性

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

YepAPI/skills

WCAG 2.1 AA — semantic HTML, keyboard navigation, screen readers.

YepAPI/skills

Experiment setup, variant splitting, statistical significance, and feature flag integration.

YepAPI/skills

CRUD generators, data tables, user management, role-based access, and bulk operations.

YepAPI/skills

Monitor what ChatGPT and Gemini say about your brand using YepAPI.

YepAPI/skills

Queue workers with BullMQ/Inngest/Trigger.dev, job retries, dead letter queues, concurrency.

YepAPI/skills

Commander.js, interactive prompts with Clack, spinners, colors, config files, exit codes, and npm publishing.

YepAPI/skills

cmdk integration, Cmd+K trigger, fuzzy search, grouped actions, keyboard navigation, and dynamic action registration.

YepAPI/skills

Content planning with trend and sentiment data from YepAPI.

YepAPI/skills

Content calendar, keyword-driven planning, and content briefs.

YepAPI/skills

Theme switching with CSS variables, system preference detection, next-themes, and persistence.

YepAPI/skills

Threaded comments, @mentions, reactions, moderation queue, optimistic UI, and cursor pagination.

YepAPI/skills

Competitive analysis between domains using YepAPI.

YepAPI/skills

Layout grids, KPI cards, data tables with sorting/filtering, sidebar navigation, and responsive breakpoints.

YepAPI/skills

Data encryption — TLS everywhere, AES-256-GCM for PII, bcrypt passwords, encrypted backups.

YepAPI/skills

Chart patterns with Recharts/Nivo/D3, responsive containers, color scales, and accessibility.

YepAPI/skills

Drizzle schema definition, push vs migrate, type-safe queries, relations, transactions, and prepared statements.

YepAPI/skills

Upload handling, resize/compress, CDN delivery, blur placeholders, and next/image patterns.

YepAPI/skills

Local business and Google Maps research using YepAPI.

YepAPI/skills

Cursor-based pagination, IntersectionObserver, virtualized lists with @tanstack/react-virtual, and scroll position restoration.

YepAPI/skills

Chat UI components, streaming responses with AI SDK, context window management, and RAG patterns.

相关技能