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

@umu-team/i18n-tool

v0.1.15

Published

umu 多语工具,抽取及回填多语。

Downloads

5

Readme

@umu-team/i18n-generator

umu 多语工具,抽取及回填多语。

npm package npm package

安装

npm i -D @umu-team/i18n-tool

使用

npx i18n-tool <cmd> [locales...]

多语分界符

代码中使用特定分界符将需要国际化的文案进行包裹,分两种分界符:

  • __(xxx) 标识文案 xxx 只在单元(见下方工作原理中单元的介绍)中使用
  • ___(xxx) 标识文案 xxx 为全局共用

抽取及翻译

其中 cmd 为需要执行的命令, locales 为空格分格的语言。

  • 可用的 cmd 有
    • extract 抽取多语
    • translate 翻译回填
  • locales 为指定的语言或多个语言,譬如 cn

示例

npx i18n-tool extract cn en

运行后将抽取中文及英文文案到相应多语文件中,如果没有则生成相应文件。

工作原理

所涉及的概念和各文件作用如下,流程图见下方杂项部分。

单元

项目中文件夹以 @ 开头的表示该文件夹定义了一个多语单元。其下包含的文件及子文件夹中文件,如有多语,则统一抽取到该文件夹下的 __i18n__ 目录。

解决的问题:一个中文对应多种翻译的情况。如果不同单元中都有相同一句中文,因为不同单元的多语文件是分开的,所以各自单元中其中文对应的翻译可以不同。

.i18n-cache

存放项目中所有多语。只工具使用,不可手动修改。

__i18n__

存放项目中的多语文件。包含两种类型:

  • 项目根目录下的 __i18n__ 存放全局共用的多语文件
  • 项目中对应单元的多语文件,为单元所使用。

__i18n_changes__

该文件夹存放项目中变更过的多语,只用于抽取多语及多语回填,其他需求不使用该文件夹中的文件。

抽取过程

运行 npx i18n-tool extract <locales...> 进行多语抽取。

抽取时,当文件存在多语,首先判断其多语分界符,

  • 如果是 __(xxx) ,表示文案为单元使用,尝试抽取到文件所在目录最近的单元中。如果文件路径向上层未找到任何单元的定义,则抽取到全局。
  • 如果是 ___(xxx) 表示为共用文案,抽取到全局。

确定收取到何处之后,读取该位置的多语文件,如果没有,则创建。

  • 如果原来该位置的多语文件中,没有包含当前文案,则进行添加,同时添加到缓存 .i18n-cache 及变更中 __i18n_changes__
  • 哪颗原来位置的多语文件中已经存在该文案,
    • 如果缓存中没有,则同步到缓存。
    • 中文情况下,不处理;非中文情况下,对比其翻译与缓存中的翻译是否一致,如果不一致,则以存在的多语翻译中的值为准,更新到缓存。

翻译回填过程

翻译后运行 npx i18n-tool translate <locales...> 会将变更中的多语回填到对应多语文件中,回填过程只处理非中文的多语。

读取变更多语并解析出其中的共用文案和单元文案。分别回填到相应位置的多语文件中。同时更新缓存中的翻译。

单个文案的更新

  • 中文文案的更新修改源码中的文案,形成一条新的多语,需手工同步其原来对应的多语翻译
  • 非中文文案的更新直接修改其对应的多语文案

杂项

流程图

i18n-tool workflow

帮助

查看完整的示例项目前往 @umu-team/presentation

更多关于命令的帮助查看 npx i18n-tool -h