Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9409

Node.js: Google Cloud Translation API の使い方 (Advanced)

$
0
0
advanced01.js
#! /usr/bin/node
////  advanced01.js////                      Nov/25/2000//// ---------------------------------------------------------------'use strict'// ---------------------------------------------------------------constprojectId="project-translation"constlocation='global'consttext="Es war einmal ein kleines Mädchen."const{TranslationServiceClient}=require('@google-cloud/translate')consttranslationClient=newTranslationServiceClient()asyncfunctiontranslateText(){constrequest={parent:`projects/${projectId}/locations/${location}`,contents:[text],mimeType:'text/plain',sourceLanguageCode:'de',targetLanguageCode:'ja',}try{const[response]=awaittranslationClient.translateText(request)for(consttranslationofresponse.translations){console.log(`Translation: ${translation.translatedText}`)}console.error("*** 終了 ***")}catch(error){console.error(error.details)}}// ---------------------------------------------------------------console.error("*** 開始 ***")translateText()// ---------------------------------------------------------------

実行方法

export GOOGLE_APPLICATION_CREDENTIALS=./***.json
#
advanced01.js

実行結果

*** 開始 ***
Translation: 昔々、小さな女の子がいました。
*** 終了 ***

Viewing all articles
Browse latest Browse all 9409

Trending Articles