npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

i18n-replace-react

v1.0.7

Published

i18n helper scripts for extracting and managing translations

Readme

i18n-replace-react

i18n-replace-react 是一组命令行工具,帮助在 Next.js(App Router)项目中:

  • 自动扫描并提取源码中的中文文本;
  • 将中文映射为语义化的 i18n keys;
  • 按映射替换源码中的 t(...) 调用以及 zh 目录下的 messages 文件;
  • 生成国际化需要的json文件;
  • 导出用于翻译的 Excel 文件(translations.xlsx)。

这些脚本会直接修改源码文件,使用前请务必对项目做 git 提交备份。

安装

npm install i18n-replace-react
# 或(开发依赖)
pnpm add -D i18n-replace-react

快速开始

  • 查看可用命令:
npx i18n-replace-react
  • 运行所有步骤(按固定顺序执行):
npx i18n-replace-react all

CLI 命令与说明

  • i18n-replace-react app — 扫描 app/[locale]/**/* 与相关 _components,提取中文并写回源码。
  • i18n-replace-react components — 只处理 components/ 目录。
  • i18n-replace-react translate — 将 zh 目录中提取到的中文翻译为英文 key(调用 DeepSeek API)。
  • i18n-replace-react replace-message-keys — 按 keys-map.json 替换 zh 目录下 messages 文件的 key。
  • i18n-replace-react replace-key — 在源码中按 keys-map.json 替换 t() 调用的随机 key 为最终 key。
  • i18n-replace-react convert-excel — 导出 translations.xlsx(便于交付给翻译)。

示例:只运行提取页面与组件中的中文

npx i18n-replace-react app

注意:translate.js 中实现会把生成的 keys-map.json 写到 zh 目录的上一级(通常为项目根)。

示例工作流程

  1. 提取页面与组件中的中文:
npx i18n-replace-react app
  1. 将提取的 zh JSON 翻译为英文 key(需要 API Key):
DEEPSEEK_API_KEY=sk-xxx npx i18n-replace-react translate
  1. keys-map.json 替换源码中 t() 的随机 key:
npx i18n-replace-react replace-key
  1. keys-map.json 替换 zh 目录下 messages 的 key:
npx i18n-replace-react replace-message-keys
  1. 导出 Excel(供翻译团队使用):
npx i18n-replace-react convert-excel

或者一键运行全部步骤:

npx i18n-replace-react all

常见问题与注意事项

  • Node.js 版本:需要 Node.js >= 18(脚本使用 ESM 与顶层 await)。
  • 备份:脚本会直接修改源码,请在运行前提交 git。
  • keys-map.json:translate 会生成该文件;也可以手动准备并通过 KEYS_MAP 指定路径。
  • 如果遇到找不到 keys-map.jsonzh 目录的错误,可通过 KEYS_MAP/ZH_DIR 环境变量指定路径,或在项目根创建 keys-map.json

代码位置概览

  • app.js — 页面 + _components 提取入口
  • components.js — components 目录提取入口
  • translate.js — 将提取的中文翻译为 key(调用 DeepSeek)
  • replace-key.js — 在源码中替换 t() 的 key
  • replace-message-keys.js — 替换 zh 下 messages 的 key
  • convert-excel.js — 导出 translations.xlsx
  • lib.js — 公共工具(AST、读写、路径处理)