0.1.0 • Published 4 years ago

@arcblock/markdown-translator v0.1.0

Weekly downloads
1
License
MIT
Repository
-
Last release
4 years ago

Markdown Translator

A smart translator that helps to translate your markdown to any language without messing up the format. This tool is built upon google translation service.

Usage

Markdown translator comes with both programmatic and command-line support.

CLI Usage

First install the command line tool:

npm install -g @arcblock/markdown-translator

Then, print usage information:

❯ translator -h
Usage: translator [options]

Options:
  -V, --version             output the version number
  -i, --input-path <path>   Which file to translate
  -t, --target <lang>       Target language to translate (default: "en")
  -o, --output-path <path>  Where to put translated file
  -k, --api-key <key>       Google API Key, default to $GOOGLE_TRANSLATE_API_KEY
  -c, --cache-dir <dir>     Where to put translation cache, defaults to tmp
  -v, --verbose             Do we want to enable debug mode
  -h, --help                output usage information

Then, translate a file

translator -i /path/to/your/doc.md
translator -i /path/to/your/doc.md --target en

Programmatic

First, add dependency:

npm install @arcblock/markdown-translator -S

Then, create a translator and do translation:

const os = require('os');
const MarkdownTranslator = require('@arcblock/markdown-translator');

const apiKey = ''; // your google translation api key
const target = 'en';
const sourceFile = '/path/to/your/doc.md';
const cacheDir = os.tmpdir();
const translator = new MarkdownTranslator(apiKey);

translator
  .translate({ sourceFile, target, cacheDir })
  .then(files => {
    files.forEach(({ fileAbsolutePath, markdownResult }) => {
      console.log('translated', { fileAbsolutePath, markdownResult });
    });
  })
  .catch(err => {
    console.error(err);
  });