# atool-l10n

> automatical solution for generating localization resource using middlewares

Latest version **0.4.0** (published 2019-04-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install atool-l10n
pnpm add atool-l10n
yarn add atool-l10n
bun add atool-l10n
```

Provides the commands `atool-l10n`, `atool-l10n-debug`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2019-04-11 |
| First published | 2016-08-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 13 |
| Unpacked size | 61.1 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 46 |
| Author | jaredleechn |
| Maintainers | jaredleechn |

## Links

- npm: https://www.npmjs.com/package/atool-l10n
- Repository: https://github.com/ant-tool/atool-l10n
- Issues: https://github.com/ant-tool/atool-l10n/issues
- npm.io page: https://npm.io/package/atool-l10n

## Dependencies (13)

- [co](https://npm.io/package/co.md) ~4.6.0
- [glob](https://npm.io/package/glob.md) ~7.0.3
- [resolve](https://npm.io/package/resolve.md) ~1.1.7
- [spm-log](https://npm.io/package/spm-log.md) ~0.1.3
- [inquirer](https://npm.io/package/inquirer.md) ~1.0.3
- [commander](https://npm.io/package/commander.md) ~2.9.0
- [co-request](https://npm.io/package/co-request.md) ~1.0.0
- [ensure-dir](https://npm.io/package/ensure-dir.md) ^0.1.0
- [is-ali-env](https://npm.io/package/is-ali-env.md) ^0.1.2
- [loader-utils](https://npm.io/package/loader-utils.md) ~0.2.15
- [query-string](https://npm.io/package/query-string.md) ^4.2.2
- [babel-polyfill](https://npm.io/package/babel-polyfill.md) ^6.9.1
- [@vitalets/google-translate-api](https://npm.io/package/@vitalets/google-translate-api.md) ^2.8.0

## Recent versions

- 0.4.0 (latest) — 2019-04-11
- 0.3.1 — 2018-02-28
- 0.3.0 — 2017-10-26
- 0.2.2 — 2017-03-20
- 0.2.1 — 2016-12-29
- 0.2.0 — 2016-11-10
- 0.1.1 — 2016-08-09
- 0.1.0 — 2016-08-08

## README

# atool-l10n

- collection all meta data generated by babel
- fetch translate result from machine translation center
- save the translation into local

automatical solution for generating localization resource using middlewares

[Demo in React](https://github.com/ant-design/intl-example)

![](https://zos.alipayobjects.com/rmsportal/JoGKhgfuFXaJNzK.gif)

## Usage


- setup

  ```bash
  $ npm i atool-l10n --save-dev
  ```
- add `l10n.config.js` into the same root dir of your project

  ```js
  // default config
  module.exports = {
    middlewares: {
      summary: ['summary?sourcePattern=i18n-messages/**/*.json'],
      process: [
        'fetchLocal?source=locales,skip',
        'metaToResult?from=defaultMessage,to=zh',
        'youdao?apiname=iamatestmanx,apikey=2137553564',
        'reduce?-autoPick,autoReduce[]=local,autoReduce[]=meta',
      ],
      emit: ['save?dest=locales'],
    },
  };
  ```
- run

  ```bash
  $ node_modules/.bin/atool-l10n
  ```

## Options

```bash

  Usage: atool-l10n [options]

  Options:

    -h, --help        output usage information
    -v, --version     output the version number
    --config <dir>    where is the config file, default is l10n.config.js

```

## Middleware

`atool-l10n` middlewares will execute one by one, with parameter `query` and shared context

A middleware may looks like this: 

```
export default function something(query) {
  this.getMeta();
  this.setResult();
  this.setOption();
  ...
}
```

- query: parameters passed to current middleware
  - parse from `option.config(default is l10n.config.js)`
  - parsed by [loader-utils](https://github.com/webpack/loader-utils)

#### Context

There are necessary operation and usefull methods on `this` context in the function 

You can check the detail API via [file](https://github.com/ant-tool/atool-l10n/tree/master/src/context.js)

#### Built-in middlewares

- `summary`: collect origin data generated from `babel-plugin-react-intl`

|parameter|default|description|
|:-------:|:-----:|:---------:|
|`sourcePattern`|`'i18n-messages/**/*.json'`|where the messages json files is, specified in `babel-plugin-react-intl`, array supported|

- `fetchLocal`: add local locales messages as an option of translation result

|parameter|default|description|
|:-------:|:-----:|:---------:|
|`source`|`'locales'`|where the local locales messages file is, file name is same as language name, eg: `zh`|
|`skip`|`true`|if add the id into translating skip array when all local locales messages for it is not empty|

- `metaToResult`: take defaultMessage or other key of meta into an option of translation result

|parameter|default|description|
|:-------:|:-----:|:---------:|
|`from`|`'defaultMessage'`|using which key of meta, eg: `defaultMessage`, `id`, `description`...|
|`to`|`'zh'`|the language you want to save as|

- `youdao`: fetch translate result from zh to en from youdao

|parameter|default|description|
|:-------:|:-----:|:---------:|
|`apiname`|`'iamatestmanx'`|apiname you applied for machine translation from youdao|
|`apikey`|`'2137553564'`|apikey you applied for machine translation from youdao|

you can easily apply the apiname and apikey from [youdao](http://fanyi.youdao.com/openapi?path=data-mode)

- `google`: fetch translate result from zh to en from google translate

|parameter|default|description|
|:-------:|:-----:|:---------:|
|`from`|`'zh-cn'`|string, what languages you want to translate from|
|`to`|`'en'`|string, what languages you want to translate to|
|`tld`|`'cn'`|string, which TLD of Google Translate you want to use, form: `translate.google.${tld}`|

use [google-translate-api](https://github.com/matheuss/google-translate-api)

- `gugu`: automatic contextualized translate for multi languages of each id from gugu, only available in alibaba-network

|parameter|default|description|
|:-------:|:-----:|:---------:|
|`from`|`'zh'`|can be an array, what languages you want to translate from|
|`to`|`'en'`|can be an array, what languages you want to translate to|

- `reduce`: pick the best translation, among all translation options in terminal

|parameter|default|description|
|:-------:|:-----:|:---------:|
|`autoPick`|`false`|auto pick the value of index `[autoPick]`|
|`autoReduce`|`['local', 'meta']`|auto reduce some options, the smallest index wins, values of the other index in the array will be delete|


- `save`: save translation result into local locale files, which are required directly by source code

|parameter|default|description|
|:-------:|:-----:|:---------:|
|`dest`|`'locales'`|save locales messages into where, message file named by language name|

---
_Source: https://npm.io/package/atool-l10n · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
