0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

kintone から Azure の Translator Text API を使う

Last updated at Posted at 2017-11-14

Cyboze kintone をカスタマイズし、Translator Text API を呼び出す。
これにあたり、kintone で用意されている API を使う。
( Node.js や jQuery も使えるらしいのだが、あまりに知識がなく>< )

参考

実装

仕様(なんちゃって仕様)

  • 新規「保存」処理で翻訳している。(編集時には翻訳してない)
  • 「日本語」に入力した値を英語に翻訳して、「英語」に設定する
  • 言語は、ja => en 固定としている(簡単のため)

実装上のポイント

  • kintone.Promise を用いて、非同期処理の終了を待つ
  • kintone.proxy を用いて、https リクエストを発行する
(function() {
    "use strict";

    // レコード追加画面の保存前処理
    kintone.events.on('app.record.create.submit', function(event) {
        var record = event.record;
        var text = record["日本語_0"]["value"];

		var subscriptionKey = "アクセスキーを設定してください";
  		var options = 'https://api.microsofttranslator.com/v2/http.svc/Translate?' + 
  				'&from=ja' +
    	        '&to=en' +
    	        '&text=' + text;

        return new kintone.Promise(function(resolve, reject) {

			kintone.proxy(options, 'GET', {'Ocp-Apim-Subscription-Key': subscriptionKey}, {
			}, function(body, status, headers) {

			    //success
		        const data = body;
		        const translated = data.replace(/<("[^"]*"|'[^']*'|[^'">])*>/g, '');
		       	record['英語_0']['value'] = translated;
		       	resolve(event);

			}, function(error) {

			    //error
				alert("error: " + error); //proxy APIのレスポンスボディ(文字列)を表示
				resolve(event);

			});

	    });
    });

})();

とても気に入らないこと

  • アクセスキーは環境変数化できないものか。。
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?