LoginSignup
0
0

More than 3 years have passed since last update.

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

Last updated at Posted at 2020-11-25
basic01.js
#! /usr/bin/node
//
//  basic01.js
//
//                      Nov/25/2000
//
// ---------------------------------------------------------------
'use strict'

async function translateText() {
    let [translations] = await translate.translate(text, target)
    translations = Array.isArray(translations) ? translations : [translations]
    console.log('Translations:')
    translations.forEach((translation, i) => {
        console.log(`${text[i]} => (${target}) ${translation}`)
        console.error ("*** 終了 ***")
    })
}

// ---------------------------------------------------------------
console.error ("*** 開始 ***")
const {Translate} = require('@google-cloud/translate').v2

const translate = new Translate()


const text = "Es war einmal ein kleines Mädchen."
const target = "ja"
translateText()

// ---------------------------------------------------------------

実行方法

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

実行結果

*** 開始 ***
Translations:
E => (ja) 昔々、小さな女の子がいました。
*** 終了 ***
0
0
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
0