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

@wakeadmin/bbt

v0.2.3

Published

用来维护多语言信息的工具

Downloads

29

Readme

bbt

一个自动化的语言包管理和翻译工具,受启发于《圣经》中的巴别塔故事,旨在解决多语言之间的沟通障碍。

背景

bbtTower of Babel 即巴别塔的拼音首字母,巴别塔是出自《圣经》的一则故事,其中人们为显示自己的力量而建塔,最终由于语言的障碍而失败。我们的bbt工具旨在解决类似的多语言沟通问题。

工作流程和初衷

作为中文开发者,我们更习惯在中文环境下编程,并且也少有程序员能够掌握多门语言,尤其是比如日语、泰语、法语这类小语种。

因此,我们的程序主要以中文为第一公民。但是,如果我们的程序需要支持多语言,那么我们就需要将中文的内容翻译成其他语言,这就是bbt的初衷。

bbt的工作流程如下:

  1. 收集所有的翻译内容 - (npx bbt collect)
  2. 将翻译内容导出到 Excel(excel 文件或者 CSV) 表格中
  3. 将 excel 表格发送给翻译人员, 或者程序员通过翻译工具翻译 - (npx bbt translate)
  4. 翻译人员翻译或校准完成之后,将 excel 表格发送给开发人员
  5. 开发人员将 excel 表格导入到项目中
  6. 开发人员将 excel 表格回写到语言包中 - (npx bbt write)

安装

$ npm install @wakeadmin/bbt  // or
$ yarn add @wakeadmin/bbt  // or
$ pnpm add @wakeadmin/bbt

$ npx bbt [command] <options>

命令

bbt init

初始化,将生成配置文件

$ npx bbt init

执行 init 命令之后, 会生成 bbt.config.js 配置文件。默认配置如下:

module.exports = {
  langs: ['zh', 'en'],
  test: '.*\\.tr$',
  exclude: ['node_modules'],
};

langs 是我们需要支持的语言,这是一个字符串数组,其中数组的第一项为基准语言,也就是我们会通过基准语言来:

  • 作为基准翻译到其他语言
  • 已基准语言的语言包为基础,对比和推断需要翻译的其他语言包

查看更多配置项

bbt collect

收集所有的符合要求的语言包,并将信息提取到 bbt.csv 中,方便翻译人员进行翻译和校准。当然你也可以使用 bbt translate 自动翻译

| name | shortName | type | description | default | required | | -------- | --------- | --------- | ---------------------------- | ----------------- | -------- | | --config | -c | string | 配置文件地址 | ./bbt.config.js | false | | --strict | | boolean | 是否使用strict模式进行对比 | false | false |

$ npx bbt collect

bbt translate

$ npx bbt translate

使用翻译 API 对excel(bbt.csv)文件进行翻译

| name | shortName | type | description | default | required | | ------------ | --------- | ---------------------------------- | ------------------------------------------------------------------------------- | ----------------- | -------- | | --translator | -t | 'google' \| 'deepl' \| 'chatgpt' | 使用哪个翻译 API, 如果 bbt.config.js 自定义了 translator,则以配置为准 | 'google' | false | | --proxy | -p | string | 正向代理地址 , 如果为空的话,会通过环境变量进行获取 | - | false | | --force | -f | boolean | 是否强制进行翻译, 默认情况下只会翻按需翻译(即无翻译内容时翻译), 请谨慎开启 | false | false | | --model | | 'gpt-4' \| 'gpt-3.5-turbo' | 使用chatgpt进行翻译时所使用的模型 | 'gpt-3.5-turbo' | false | | --api-key | -k | string | 翻译服务的API Key , 如果为空的话,会通过环境变量进行获取 | - | false | | --base-url | | string | 反向代理地址, 如果为空的话,会通过环境变量进行获取 | - | false |

bbt 支持通过 Google、DeepL、ChatGPT 等方案进行初步的机器翻译。如果你想使用其他的翻译服务,可以通过自定义插件的方式进行扩展.

Google 翻译

默认使用的就是 Google 翻译

npx bbt translate -t google --api-key GOOGLE_TRANSLATION_API_KEY

可以通过 --api-key 选项或者 BBT_GOOGLE_API_KEY 环境变量来配置 Google 翻译的 API KEY。

我们也建议你将环境变量配置在用户目录下的 .profile 或者 .bashrc 这类文件中,这样可以避免每次都需要输入 API KEY。

Google 翻译需要依赖科学上网,需要自行解决。有两种代理配置方式:

  • 通过 --proxy 选项或者 BBT_PROXY 环境变量来配置正向代理地址,bbt 会自动将请求转发到代理地址上
  • 通过终端代理的方式进行配置,例如:export http_proxy=YOU_PROXY_ADDRESS

DeepL 翻译

和 Google 翻译类似,需要通过 --api-key 选项或者 BBT_DEEPL_API_KEY 环境变量来配置 DeepL 翻译的 API KEY。

ChatGPT 翻译(实验性)

  • --model 确定使用的 ChatGPT 模型版本。默认为 gpt-3.5-turbo
  • --base-urlBBT_OPEN_AI_BASE_URL 环境变量。如果你自己搭建了 ChatGPT 的代理服务,可以通过这个选项来配置
  • --api-keyBBT_OPEN_AI_API_KEY 配置 OPEN AI 的 API KEY

bbt write

$ npx bbt write

bbt.csv 的翻译结果回填到对应的语言包中,如果对应的语言包不存在,则会自动创建

| name | shortName | type | description | default | required | | -------- | --------- | -------- | ------------ | ----------------- | -------- | | --config | -c | string | 配置文件地址 | ./bbt.config.js | false |

高级

支持的环境变量

bbt 依赖于以下环境变量

| name | description | | -------------------- | ---------------------------------------- | | BBT_OPEN_AI_API_KEY | chatGPT(openAI)服务所依赖的API Key | | BBT_OPEN_AI_BASE_URL | chatGPT(openAI)服务的反向代理地址 | | BBT_DEEPL_API_KEY | DeepL服务所依赖的API Key | | BBT_DEEPL_BASE_URL | DeepL服务的反向代理地址 | | BBT_GOOGLE_API_KEY | Google服务所依赖的API Key | | BBT_GOOGLE_BASE_URL | Google服务的反向代理地址 | | BBT_PROXY | 使用translate命令时的正向代理地址 |

自定义解析文件

有时候国际化资源并不是使用JSON格式, 因此可以通过plugins.parser来自定义文件的解析方式

方法签名

export interface FileParser<T = any> {
  parse(str: string): Record<string, T>;
  stringify(record: Record<string, T>): string;
}

example

zh.tr

hello 你好
world 世界

bbt.config.js

module.exports = {
  // ...other
  plugins: {
    parser: {
      parse(content) {
        return Object.fromEntries(content.split(/\n/).map(str => str.split(/\s/)));
      },
      stringify(record) {
        return Object.entries(record)
          .map(arr => arr.join(' '))
          .join('\n');
      },
    },
  },
};

自定义翻译 API

函数签名

/**
 *
 * @param record - 需要翻译的数据源
 * @param target - 翻译的目标语言
 * @param sourceLanguage - 数据源原本的语言
 * @returns Observable<TranslatedList<string>> | Promise<TranslatedList<string>>;
 */
translator: (record: Record<string, string>, target: string, sourceLanguage: string) =>
  Observable<TranslatedList<string>> | Promise<TranslatedList<string>>;

example

// bbt.config.js

module.exports = {
  // other
  plugins: {
    translator: (textMap, target, sourceLanguage) => {
      return Promise.resolve(
        Object.entries(textMap).map(([key, value]) => ({
          target,
          key,
          translatedText: `${value} - ${target}`,
        }))
      );
    },
  },
};

FAQ

faq

License

MIT