これは、YQL を使用して Google 翻訳を行う、私が作成したクラスです。
var Translator = {
source: 'ro', // default
target: 'en', // default
url: 'http://query.yahooapis.com/v1/public/yql?q=select * from google.translate where q="',
urlRemaining: '";&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys&callback=',
diacritics: Array(),
newCharacters: Array(),
replaceAll: function( string, replace, replaceWith ) {
return string.replace( new RegExp( replace, 'g' ), replaceWith );
},
replaceDiacritics: function( text ) {
string = text;
// diacritics and newCharacters should be arrays of the same length
// diacritics should only be specified in lowercase - uppercased version will be assumed
// durring the process
for ( i = 0; i < this.diacritics.length; i++ ) {
string = this.replaceAll( string, this.diacritics[i], this.newCharacters[i] );
string = this.replaceAll( string, this.diacritics[i].toUpperCase(), this.newCharacters[i].toUpperCase() );
}
return string;
},
translate: function( text, target, source ) {
target = target || this.target;
source = source || this.source;
return $.ajax({
url: this.url + encodeURIComponent( this.replaceDiacritics( text ) ) + '" and source="' + source + '" and target="' + target + this.urlRemaining,
dataType: 'json',
cache: false
});
},
spitResult: function( x, container ) {
x.success(function(realData) {
$report = realData.query.results.json.sentences;
$result = '';
if ($.isArray($report)) {
for (i = 0; i < $report.length; i++) {
$result += $report[i].trans;
}
} else {
$result = $report.trans;
}
if (container instanceof jQuery) {
container.html($result);
} else {
container.innerHTML = $result;
}
});
}
}
そして今、ページ内の一連の要素でそれを呼び出しています
promises = Array();
Translator.diacritics = Array('ă', 'â', 'î', 'ș', 'ț');
Translator.newCharacters = Array('a', 'a', 'i', 's', 't');
$('.translate').each(function() {
$this = $(this);
promises[promises.length] = Translator.translate($this.html(), 'en', 'ro');
Translator.spitResult(promises[promises.length-1], $this);
});
これは Firefox と Chrome で問題なく動作しています。ただ、相変わらずInternet Explorer(私の場合は9)が問題のようです。私が推測できたことから、それはプロミスリゾルバー(Translate.spitResult
)に存在します-呼び出されますが、データが渡されないようです。コンソールで見ました。promise 配列要素には 3 つのオブジェクトが取り込まれていますが (これは正常だと思います)、次のようになります。
readyState: 0
responseJSON: undefined, status: 0
statusText: "No Transfer".
分音記号関数を削除しようとしました(とにかく応答があったはずなので、理由は正確にはわかりません)、cache: false
ajax呼び出しでモードも試しましたが、役に立ちませんでした。
何が問題なのか誰か知っていますか?
前もって感謝します。