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 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-i18n-replace

v1.0.8

Published

一个批量支持国际化方法的脚本

Downloads

6

Readme

react-i18n-replace

一个批量支持国际化方法的脚本

为什么需要这个脚本?

  • 项目中没有支持国际化,需要手动将所有字符串替换为国际化方法,此脚本可以自动化替换字符串为国际化方法

脚本做了什么?

自动引入项目中的国际化方法

import { t } from "@/i18n"; //@i18n 是一个自定义的路径

jsx代码转换

<!-- 转换前 -->
<SelfComp title="测试标题">测试文本</SelfComp>
<!-- 转换后 -->
<SelfComp title={t('测试标题')}>{t('测试文本')}</SelfComp>

字符串转换

// 转换前
const user = "张三";
// 转换后
const user = t("张三");

模板字符串转换

// 转换前
const user = "张三";
const age = 18;
const words = `现在时间${Date.now()}, ${user}已经${age}岁了`;
// 转换后
const user = t("张三");
const age = 18;
const words = t("现在时间{{0}}, {{user}}已经{{age}}岁了", {
  0: Date.now(),
  user,
  age,
});

安装方式

  • 全局安装
npm i react-i18n-replace -g
# or
yarn add react-i18n-replace -g
  • 项目安装
npm i react-i18n-replace --save-dev
# or
yarn add react-i18n-replace --dev

脚本选项

react-i18n-replace [path] [options]

path: 以当前终端所在目录为根目录, 示例src, 相当于当前终端目录下的src目录

-wd, --wrapped: 字符串包裹的方法名,示例:t或者lang

-impLib, --importLibrary: 需要导入的包,示例: i18nxt

-impFuncs, --importFunctions: 从impLib需要导入的方法名,需和--wrapped参数保持一致
可多次添加此参数,示例:--impFuncs t --impFuncs useTranslate

-ext, --extension: 需要执行脚本文件的后缀,示例: js,ts,tsx

-ignore, --ignore: 需要忽略的文件夹或文件,示例:node_modules/**
可多次添加此参数,示例:--ignore node_modules/** --ignore **/*.d.ts

使用方法

  • 全局安装下执行
react-i18n-replace src --wd t --impLib @src/i18n --impFuncs t --ext ts,tsx
  • 项目下安装执行
npx react-i18n-replace src --wd t --impLib @src/i18n --impFuncs t --ext ts,tsx

注:配合i18next-scanner搭配使用,效果更佳