mcpField
Z
zoonk-translations

zoonk-translations

github.com/zoonk开发15844 次安装3 周前更新
代码仓库

关于 zoonk-translations

zoonk-translations 是用于遵循项目 next-intl 国际化规范的技能,面向开发者提供翻译调用、消息文件及复数和富文本处理指引。

主要功能

  1. 01next-intl 服务端与客户端用法
  2. 02静态翻译键与字符串插值
  3. 03元数据和服务端操作的语言环境规则
  4. 04JSON 消息文件工作流
  5. 05命名空间、复数和富文本

适用场景

  1. 01为页面或界面添加本地化文本
  2. 02处理翻译后的元数据和服务端操作
  3. 03实现复数或富文本消息

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/zoonk/zoonk --skill zoonk-translations

安装后在 Claude Code 中输入/zoonk-translations确认已加载。

详细介绍

国际化(i18n)工作流

概述

本项目使用 next-intl 实现国际化。翻译内容存储在 JSON 消息文件中。

主要函数

  • 服务端组件:使用 getTranslations(无需 locale)
  • 客户端组件:使用 useTranslations
  • 元数据/服务端操作:使用带 locale 参数的 getTranslations

重要规则

1. 翻译键中不能包含动态值

重要:t 函数不支持将动态值用作键。

// BAD: Dynamic key - will NOT work
const category = getCategory();
t(category); // Error!
t(MYLABELS[category]);

// GOOD: String literals only
t("Arts courses");
t("Business courses");
t("Technology courses");
2. 支持字符串插值

可以在翻译内容中使用字符串插值来传入动态值:

// GOOD: Interpolation with static key
t("Explore all {category} courses", { category: "arts" });
t("Welcome, {name}!", { name: user.name });
t("{count} items remaining", { count: 5 });
3. 不要传递 t 函数

不能将 t 函数传递给其他函数或组件:

// BAD: Passing t function
function myFunction(t, label) {
  return t(label);
}
myFunction(t, "Some label");

// GOOD: Call t directly
function myFunction(translatedLabel: string) {
  return translatedLabel;
}
myFunction(t("Some label"));
4. Locale 参数规则
// Server Component - no locale needed
const t = await getTranslations();
t("Hello");

// generateMetadata - needs locale
export async function generateMetadata({ params }) {
  const { locale } = await params;
  const t = await getTranslations({ locale });
  return { title: t("Page Title") };
}

添加翻译的工作流

  1. 在代码中添加翻译调用:

    const t = await getTranslations();
    return <h1>{t("New page title")}</h1>;
    
  2. 将键添加到消息文件中:

    在 locale 目录中找到 JSON 消息文件(例如,messages/en.json、messages/es.json),然后添加新键:

    {
      "New page title": "New page title"
    }
    
  3. 为每个 locale 翻译:

    // messages/es.json
    {
      "New page title": "Nuevo título de página"
    }
    

常见模式

条件文本
// Use separate translation keys
const status = isActive ? t("Active") : t("Inactive");
复数形式
// Use ICU message format
t("items", { count: 5 }); // "5 items"

在消息文件中:

{
  "items": "{count, plural, =0 {No items} =1 {1 item} other {# items}}"
}
富文本(含组件)
t.rich("Read our {link}", {
  link: (chunks) => <Link href="/terms">{chunks}</Link>,
});

在消息文件中:

{
  "Read our {link}": "Read our <link>terms of service</link>"
}

文件结构

messages/
  en.json      # English translations
  es.json      # Spanish translations
  pt.json      # Portuguese translations

命名空间

对于较大型应用,可按命名空间整理翻译:

// Server Component
const t = await getTranslations("Dashboard");
t("welcome"); // Looks up "Dashboard.welcome"

// Client Component
const t = useTranslations("Dashboard");
t("welcome");
{
  "Dashboard": {
    "welcome": "Welcome to your dashboard"
  }
}

评论

支持 Markdown