A command-line utility to automatically translate .xlf translation files using Google Translate
DISCLAIMER
This library was forked from https://github.com/nfriend/google-translate-xlf
Some additions to package dependecies were made according to https://www.npmjs.com/package/google-translate-xlf-updated
The main goal to fork and edit original tool was to preserve word and interpolation blocks order while translating original .xlf file, as base library copied this interpolation blocks to the end of target element.
While making this improvements, the original translate.js file eas fully rewritten starting from library we are using to read and build object model of .xlf to ending with how we are parsing object model tree itself and creating queue to translate.
Also most of redundant dependecies were removed, file and folder structure were redefined.
This is a command-line utility. To install this utility, run:
npm install -g xlf2xlf
This will install the utility globally so that it can be run from any location. The executable installed by this module is xlf2xlf
.
You also could run library using npx utility.
npx xlf2xlf [options]
Before you begin, you will need an .xlf
file to translate!
If you are using Angular 2+ and the Angular CLI, you will probably extract an .xlf
file from your app by running ng xi18n
inside your Angular project. This produces a file named messages.xlf
inside your project folder.
Say we have an .xlf
file named messages.xlf
. Let's translate this file from English ("en") to Hindi ("hi") and save the translated file as messages.hi.xlf
:
xlf2xlf --in messages.xlf --out messages.hi.xlf --from en --to hi
Or, if you don't like typing too much, you can use the abbreviated versions of the arguments:
xlf2xlf -i messages.xlf -o messages.hi.xlf -f en -t nl
For more usage help, run:
xlf2xlf help
If the utility not installed globally, just use it via npx, e.g.
npx xlf2xlf --in messages.xlf --out messages.hi.xlf --from en --to hi
Tool adds 2 kind of extra messages to final xlf file according to certain criterias:
- If we are using
skip
option, there will be information messages inside target tags to ease spot of where translation should be placed.
Message:
[INFO] Add your translation here
- If there is any errors during quering the translation, there will be warning messages inside problematic target tags to ease spot where things went wrong.
Message:
[WARN] Failed to translate
Option | Default | Mandatory | Description |
---|---|---|---|
i / in | - | true | The input .xlf file to translate |
o / out | - | true | The name of the translated output file |
f / from | - | true | The language code of the input file |
t / to | - | true | The language code to translate to |
r / rate | 500 | false | How long to wait after launching a job before launching another one in ms. For more information see https://github.com/SGrondin/bottleneck#docs |
c / concurrent | 4 | false | How many jobs can be executing at the same time. For more information see https://github.com/SGrondin/bottleneck#docs |
s / skip | false | false | Skips translating and adds only target tag with boilerplate text [INFO] Add your translation here |
-
The speed of quering / getting response is depends on network quality.
-
Sometimes translation could end up with tons of errors like
{"name":"HTTPError","statusCode":429,"statusMessage":"Too Many Requests"}
, in this case better to wait for some time before next iteration.
In case you are starting to get {"name":"HTTPError","statusCode":429,"statusMessage":"Too Many Requests"}
you could do the following:
- Wait a bit before Google Translate will stop blocking your IP
- Try to set bigger rate
npx xlf2xlf --in messages.xlf --out messages.hi.xlf --from en --to hi --rate 5000
, where rate is ms between queries. For more information on that property see https://github.com/SGrondin/bottleneck - Try to limit rate and amount of concurrent requests
npx xlf2xlf --in messages.xlf --out messages.hi.xlf --from en --to hi --rate 10000 --concurrent 1
The easiest way to develop on this module is to use test-driven development.
Unit tests can be run with npm test
. To automatically re-run the tests when you make changes to the files, run npm run test:watch
.
This utility uses Google Translate's online API for its translations using this NPM package: https://github.com/vitalets/google-translate-api