Skip to content

Commit

Permalink
Add new flag serializeRequests to slow down the API requests (#2)
Browse files Browse the repository at this point in the history
This flag can help mitigate API error responses about the rate limit
being exceeded, especially when used on the free tier.
  • Loading branch information
jrust authored Mar 6, 2017
1 parent 81a1827 commit ec24078
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 9 deletions.
12 changes: 12 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ module.exports = function(grunt) {
targetLanguages: ['fr', 'ru'],
dest: 'tmp/'
}]
},
serialized: {
options: {
msApiKey: process.env.MS_API_KEY,
serializeRequests: true
},
files: [{
src: 'test/fixtures/en.json',
sourceLanguage: 'en',
targetLanguages: ['de'],
dest: 'tmp/'
}]
}
},

Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,21 @@ grunt.initConfig({
```

#### Full Example
In this example, two files are being translated, one called ```locale-en.json``` and another called ```locale-fr.json```. They are in different folders, and will create translated files in the same ```i18n/``` folder.
In this example, two files are being translated, one called
```locale-en.json``` and another called ```locale-fr.json``` and the
`serializeRequests` flag is enabled. They are in different folders, and will create
translated files in the same ```i18n/``` folder.

Notice how the prefix and suffix is specified, it means the translated files will be named like ```locale-de.json``` instead of ```de.json```.
Notice how the prefix and suffix is specified, it means the translated files
will be named like ```locale-de.json``` instead of ```de.json```.

```js
grunt.initConfig({
ms_translate: {
default_options: {
options: {
msApiKey: YOUR_API_KEY_HERE
msApiKey: YOUR_API_KEY_HERE,
serializeRequests: true // Serializes each request to the API; slower, but can avoid rate limit errors on free tier
},
files: [{
src: '<%= yeoman.client %>/i18n/locale-en.json',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "grunt-ms-translate",
"description": "A build task to translate JSON files to other languages using Microsoft's Translation API. Pairs very well with angular-translate.",
"version": "1.0.2",
"version": "1.1.0",
"homepage": "https://github.com/seeq12/grunt-ms-translate",
"author": {
"name": "Jason Rust",
Expand Down
15 changes: 11 additions & 4 deletions tasks/ms_translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function revertVariablesInJson(translatedJson) {
return sourceJson;
}

function translate(origJson, translateArray, source, target, destPath, grunt) {
function translate(origJson, translateArray, source, target, destPath, serializeRequests, grunt) {
const jsonReferenceArray = [];
const sourceJson = _.cloneDeep(origJson);

Expand All @@ -88,8 +88,14 @@ function translate(origJson, translateArray, source, target, destPath, grunt) {
return _.chain(jsonReferenceArray)
.map('value')
.chunk(MAX_TRANSLATE_ELEMENTS)
.map((translateChunk) => translateArray({ texts: translateChunk, from: source, to: target }))
.thru(Promise.all)
.thru(translateChunks => {
if (serializeRequests) {
return _.reduce(translateChunks, (p, translateChunk) => p.then((translatedChunks) => translateArray({ texts: translateChunk, from: source, to: target })
.then(chunks => translatedChunks.concat(chunks))), Promise.resolve([]));
} else {
return Promise.all(_.map(translateChunks, (translateChunk) => translateArray({ texts: translateChunk, from: source, to: target })));
}
})
.value()
.then((translatedChunks) => {
const translations = _.flatten(translatedChunks);
Expand All @@ -110,6 +116,7 @@ module.exports = function(grunt) {
const MsTranslator = require('mstranslator');
const msTranslate = new MsTranslator({ api_key: this.options().msApiKey }, true);
const translateArray = Promise.promisify(msTranslate.translateArray, { context: msTranslate });
const serializeRequests = this.options().serializeRequests;

_.chain(this.files)
.map(function(file) {
Expand All @@ -121,7 +128,7 @@ module.exports = function(grunt) {

return _.map(file.targetLanguages, function(targetLanguage) {
const filePath = file.dest + file.prefix + targetLanguage + file.suffix;
return translate(variableSafeJson, translateArray, file.sourceLanguage, targetLanguage, filePath, grunt);
return translate(variableSafeJson, translateArray, file.sourceLanguage, targetLanguage, filePath, serializeRequests, grunt);
});
})
.flatten()
Expand Down
13 changes: 13 additions & 0 deletions test/expected/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"A": "Eine",
"B": "Zwei",
"C": "Drei",
"D": "Vier",
"E": "Fünf",
"F": "Sechs",
"G": "Sieben",
"H": "Acht",
"I": "Neun",
"FAILURE": "Fehler beim Abrufen des data{hasError, select, true{: {message}} other{}}. Klicken Sie auf wiederholen",
"SUCCESS": "Erfolg {COUNT}"
}
2 changes: 1 addition & 1 deletion test/expected/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"G": "Семь",
"H": "Восемь",
"I": "Девять",
"FAILURE": "Ошибка при получении data{hasError, select, true{: {message}} other{}}. Щелкните, чтобы повторить попытку",
"FAILURE": "Ошибка при получении data{hasError, select, true{: {message}} other{}}. Щелкните, чтобы повторить",
"SUCCESS": "Успех {COUNT}"
}
9 changes: 9 additions & 0 deletions test/ms_translate_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ exports.ms_translate = {
const expected = grunt.file.read('test/expected/ru.json');
test.equal(actual, expected, 'all 11 english keys should be translated into russian, except for variables');

test.done();
},
serialized_german: function(test) {
test.expect(1);

const actual = grunt.file.read('tmp/de.json');
const expected = grunt.file.read('test/expected/de.json');
test.equal(actual, expected, 'all 11 english keys should be translated into german, except for variables');

test.done();
}
};

0 comments on commit ec24078

Please sign in to comment.