だから...私はこれが正しい方法ではないことを知っています...しかし、「get」リクエストをノードで同期的に動作させる方法を見つけることができれば、ナビゲートするのが最も簡単に見えるボックスがあります。 js
はい、node-sync を試してみました (動作するように見えました)。
シナリオはこれ
- ファイルからパラメータ化された文字列を読み取る (完了、正常に動作)
- 文字列を再帰的にトークン化し、キーと値にチャンクします(完了、正常に動作します)
- Google 翻訳を呼び出して各値を翻訳する (中断!)
- パラメータ化された文字列を再構築し、出力ファイルに書き戻します(完了、正常に動作します)
Google 翻訳を呼び出す方法は知っていますが (文字列全体に対して簡単に実行できます)、非同期コールバックの順序を、パラメーター化された文字列の再帰に必要な再帰と並べて取得する方法に頭を悩ませることはできません。私が持っているデータセット。
Web サービスへの呼び出しを同期のように動作させることができれば、これは簡単です。例えば
read string (while not EOF)
recurse (get the next token)
translate the fragment (which is done with request.get()
add translated fragment to the current string
assemble translated string
write to file
node-sync の例は機能しましたが、request.get()
.
助言がありますか?
抜粋
// this function gets passed in a string and recursively pulls out and
// attempts to translate tokens. It needs to do this as the data source
// contains "key" = "fragment" + $(paramter1) + fragment + $(parameter2) etc
function getTokenSegment(sourceString){
s = sourceString; //lazy
// if the string contains a parameter
if (s.indexOf(leadingDelimiter) != -1) {
// extract the tokens...omitted the error checking (which all works)
translatedToken = syncTranslate(token); // <--- THIS IS WHAT I WANT...
parameter = s.substring(token.length+1, s.indexOf(trailingDelimiter)+1);
remainder = s.substring(token.length + parameter.length+1, s.length);
translatedString = translatedString + translatedToken + parameter
// recursive call to get the rest of the string
+ getTokenSegment(remainder);
}
else {
// the remainder of the string can be translated intact
translatedToken = syncTranslate(s);
translatedString = translatedString + translatedToken;
}
return (translatedString);
}
function syncTranslate(stringToTranslate) {
var sync = require('sync');
sync(function(){
var result = translate.sync(null, {key: key, q: stringToTranslate, target: language});
})
return result;
}
// translate module is from Mike Holly -- https://github.com/mikejholly/node-google-translate
// and worked perfectly when I used it on non-parameterized strings. only edit is the NULL as the
// first parameter in the callback, copied from the node-sync simple example]
var request = require('request')
, _ = require('underscore')
, querystring = require('querystring')
, util = require('util')
module.exports = function(opts, callback) {
// parse & default the arguments
opts = _.defaults(opts, {
source: 'en',
target: 'fr',
key: 'secret',
sourceText: 'text'
});
var url = 'https://www.googleapis.com/language/translate/v2?' + querystring.stringify(opts);
request.get(url, function(err, response, body){
if (err) throw err;
// parse the returned JSON
var json = JSON.parse(body);
if (json.error) {
throw json.error.message;
}
var strings = util.isArray(opts.sourceText) ? opts.sourceText : [opts.sourceText];
var result = {};
var translatedString = '';
strings.forEach(function(original, i){
result[original] = json.data.translations[i].translatedText;
translatedString = result[original];
});
callback(null, translatedString);
});
};