Google Cloud TranslationのAPIをローカルのNode.jsからアクセスしてみました。
前回(といってももう1年半前ですが)Pythonで試しましたが、今回はNode.jsです。
Cloud Translation APIを有効化
Google Cloudのコンソールにブラウザでアクセスして、使用するプロジェクトのCloud Translation APIを有効化します。
ちなみに、有効化をせずにアクセスすると、以下のようなエラーがでました。
PERMISSION_DENIED: Cloud Translation API has not been used in project xxxxxxxxxxxx before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/translate.googleapis.com/overview?project=xxxxxxxxxxxx then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
認証設定
Google Cloud Platformのコンソール画面のIAM & AdminのService accountsというメニューから操作して、Create keyでキーを作成し、JSONファイルをダウンロードしておきます。そのJSONファイルを以下のように環境変数で指定しておきます。
$ export GOOGLE_APPLICATION_CREDENTIALS=$HOME/path/to/key.json
パッケージインストール
$npm install @google-cloud/translate
ソースコード
const{TranslationServiceClient}=require("@google-cloud/translate").v3;constprojectId="xxxxxxxx";constlocation="us-central1";// 言語判定asyncfunctiondetectLanguage(text){consttranslationClient=newTranslationServiceClient();constreq={parent:translationClient.locationPath(projectId,location),content:text,mimeType:"text/plain",};constres=awaittranslationClient.detectLanguage(req);letsourceLang=null;for(constelemofres){if(elem==null)// なぜかnullがレスポンスに含まれるcontinue;returnelem["languages"][0]["languageCode"];}}// 翻訳asyncfunctiontranslate(text,sourceLang,targetLang){consttranslationClient=newTranslationServiceClient();constreq={parent:translationClient.locationPath(projectId,location),contents:[text],mimeType:"text/plain",sourceLanguageCode:sourceLang,targetLanguageCode:targetLang,};constres=awaittranslationClient.translateText(req);for(constelemofres){if(elem==null)// なぜかnullがレスポンスに含まれるcontinue;returnelem["translations"][0]["translatedText"];}}asyncfunctionsample(text){console.log("original: "+text);// 言語の判定constsourceLang=awaitdetectLanguage(text);// 翻訳for(consttargetLangof["en","ja","zh-TW","zh-CN","ko"]){if(targetLang==sourceLang)// Target language can't be equal to source language. というエラーを防ぐためcontinue;consttargetText=awaittranslate(text,sourceLang,targetLang);console.log(targetLang+": "+targetText);}console.log();}consttexts=["Hello, world!","Firebase is Google's mobile platform that helps you quickly develop high-quality apps and grow your business.","Vue是一套用于构建用户界面的渐进式框架。",];asyncfunctionmain(){for(consttextoftexts){awaitsample(text);}}main().catch(err=>{console.log(err);});
実行結果
$node sample.js
original: Hello, world!
ja: こんにちは世界!
zh-TW: 你好世界!
zh-CN: 你好世界!
ko: 안녕, 세상!
original: Firebase is Google's mobile platform that helps you quickly develop high-quality apps and grow your business.
ja: Firebaseは、高品質のアプリをすばやく開発してビジネスを成長させるのに役立つGoogleのモバイルプラットフォームです。
zh-TW: Firebase是Google的移動平台,可幫助您快速開發高質量的應用程序並發展業務。
zh-CN: Firebase是Google的移动平台,可帮助您快速开发高质量的应用程序并发展业务。
ko: Firebase는 고품질 앱을 빠르게 개발하고 비즈니스를 성장시키는 데 도움이되는 Google의 모바일 플랫폼입니다.
original: Vue是一套用于构建用户界面的渐进式框架。
en: Vue is a progressive framework for building user interfaces.
ja: Vueは、ユーザーインターフェイスを構築するための進歩的なフレームワークです。
zh-TW: Vue是一套用於構建用戶界面的漸進式框架。
ko: Vue는 사용자 인터페이스 구축을위한 진보적 인 프레임 워크입니다.
参考
JavaScript用のGoogle Cloud Translation APIレファレンスはこちら。