From c86b9e7e1d5b722dea5b67bf1df8db7472b7730d Mon Sep 17 00:00:00 2001 From: "Benjamin E. Coe" Date: Mon, 28 Oct 2019 13:33:52 -0700 Subject: [PATCH] feat!: v3 is now the default API surface (#355) BREAKING CHANGE: this significantly changes TypeScript types and API surface from the v2 API. Reference samples/ for help making the migration from v2 to v3. --- translate/package.json | 2 +- translate/quickstart.js | 34 +++-- translate/test/quickstart.test.js | 2 +- translate/test/translate.test.js | 4 +- .../v3/translate_batch_translate_text.test.js | 77 ++++++++++ ...batch_translate_text_with_glossary.test.js | 110 ++++++++++++++ ...slate_text_with_glossary_and_model.test.js | 111 ++++++++++++++ ...te_batch_translate_text_with_model.test.js | 78 ++++++++++ .../test/v3/translate_create_glossary.test.js | 58 ++++++++ .../test/v3/translate_delete_glossary.test.js | 68 +++++++++ .../test/v3/translate_detect_language.test.js | 37 +++++ .../test/v3/translate_get_glossary.test.js | 83 +++++++++++ .../translate_get_supported_languages.test.js | 32 +++++ ...et_supported_languages_for_targets.test.js | 33 +++++ .../test/v3/translate_list_codes.test.js | 33 +++++ .../test/v3/translate_list_glossary.test.js | 86 +++++++++++ .../v3/translate_list_language_names.test.js | 35 +++++ .../test/v3/translate_translate_text.test.js | 36 +++++ ...slate_translate_text_with_glossary.test.js | 83 +++++++++++ ...slate_text_with_glossary_and_model.test.js | 84 +++++++++++ ...ranslate_translate_text_with_model.test.js | 37 +++++ translate/translate.js | 136 ++++++++++-------- .../v3/translate_batch_translate_text.js | 73 ++++++++++ ...late_batch_translate_text_with_glossary.js | 80 +++++++++++ ..._translate_text_with_glossary_and_model.js | 85 +++++++++++ ...anslate_batch_translate_text_with_model.js | 78 ++++++++++ translate/v3/translate_create_glossary.js | 71 +++++++++ translate/v3/translate_delete_glossary.js | 57 ++++++++ translate/v3/translate_detect_language.js | 57 ++++++++ translate/v3/translate_get_glossary.js | 53 +++++++ .../v3/translate_get_supported_languages.js | 59 ++++++++ ...late_get_supported_languages_for_target.js | 60 ++++++++ translate/v3/translate_list_codes.js | 50 +++++++ translate/v3/translate_list_glossary.js | 54 +++++++ translate/v3/translate_list_language_names.js | 52 +++++++ translate/v3/translate_translate_text.js | 57 ++++++++ .../translate_translate_text_with_glossary.js | 63 ++++++++ ..._translate_text_with_glossary_and_model.js | 66 +++++++++ .../v3/translate_translate_text_with_model.js | 60 ++++++++ 39 files changed, 2256 insertions(+), 78 deletions(-) create mode 100644 translate/test/v3/translate_batch_translate_text.test.js create mode 100644 translate/test/v3/translate_batch_translate_text_with_glossary.test.js create mode 100644 translate/test/v3/translate_batch_translate_text_with_glossary_and_model.test.js create mode 100644 translate/test/v3/translate_batch_translate_text_with_model.test.js create mode 100644 translate/test/v3/translate_create_glossary.test.js create mode 100644 translate/test/v3/translate_delete_glossary.test.js create mode 100644 translate/test/v3/translate_detect_language.test.js create mode 100644 translate/test/v3/translate_get_glossary.test.js create mode 100644 translate/test/v3/translate_get_supported_languages.test.js create mode 100644 translate/test/v3/translate_get_supported_languages_for_targets.test.js create mode 100644 translate/test/v3/translate_list_codes.test.js create mode 100644 translate/test/v3/translate_list_glossary.test.js create mode 100644 translate/test/v3/translate_list_language_names.test.js create mode 100644 translate/test/v3/translate_translate_text.test.js create mode 100644 translate/test/v3/translate_translate_text_with_glossary.test.js create mode 100644 translate/test/v3/translate_translate_text_with_glossary_and_model.test.js create mode 100644 translate/test/v3/translate_translate_text_with_model.test.js create mode 100644 translate/v3/translate_batch_translate_text.js create mode 100644 translate/v3/translate_batch_translate_text_with_glossary.js create mode 100644 translate/v3/translate_batch_translate_text_with_glossary_and_model.js create mode 100644 translate/v3/translate_batch_translate_text_with_model.js create mode 100644 translate/v3/translate_create_glossary.js create mode 100644 translate/v3/translate_delete_glossary.js create mode 100644 translate/v3/translate_detect_language.js create mode 100644 translate/v3/translate_get_glossary.js create mode 100644 translate/v3/translate_get_supported_languages.js create mode 100644 translate/v3/translate_get_supported_languages_for_target.js create mode 100644 translate/v3/translate_list_codes.js create mode 100644 translate/v3/translate_list_glossary.js create mode 100644 translate/v3/translate_list_language_names.js create mode 100644 translate/v3/translate_translate_text.js create mode 100644 translate/v3/translate_translate_text_with_glossary.js create mode 100644 translate/v3/translate_translate_text_with_glossary_and_model.js create mode 100644 translate/v3/translate_translate_text_with_model.js diff --git a/translate/package.json b/translate/package.json index e7d46106ef..1de8f46676 100644 --- a/translate/package.json +++ b/translate/package.json @@ -11,7 +11,7 @@ "node": ">=8" }, "scripts": { - "test": "mocha --recursive --timeout 90000" + "test": "mocha --recursive --timeout 150000" }, "dependencies": { "@google-cloud/automl": "^1.0.0", diff --git a/translate/quickstart.js b/translate/quickstart.js index 95af33d727..2423f4dd53 100644 --- a/translate/quickstart.js +++ b/translate/quickstart.js @@ -1,5 +1,5 @@ /** - * Copyright 2017, Google, Inc. + * Copyright 2017 Google LLC * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -15,28 +15,36 @@ 'use strict'; -async function main( +function main( projectId = 'YOUR_PROJECT_ID' // Your GCP Project Id ) { // [START translate_quickstart] + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // Imports the Google Cloud client library - const {Translate} = require('@google-cloud/translate'); + const {Translate} = require('@google-cloud/translate').v2; // Instantiates a client const translate = new Translate({projectId}); - // The text to translate - const text = 'Hello, world!'; + async function quickStart() { + // The text to translate + const text = 'Hello, world!'; + + // The target language + const target = 'ru'; - // The target language - const target = 'ru'; + // Translates some text into Russian + const [translation] = await translate.translate(text, target); + console.log(`Text: ${text}`); + console.log(`Translation: ${translation}`); + } - // Translates some text into Russian - const [translation] = await translate.translate(text, target); - console.log(`Text: ${text}`); - console.log(`Translation: ${translation}`); + quickStart(); // [END translate_quickstart] } -const args = process.argv.slice(2); -main(...args).catch(console.error); +main(...process.argv.slice(2)); diff --git a/translate/test/quickstart.test.js b/translate/test/quickstart.test.js index 36efd92428..5b75ed5832 100644 --- a/translate/test/quickstart.test.js +++ b/translate/test/quickstart.test.js @@ -1,5 +1,5 @@ /** - * Copyright 2017, Google, Inc. + * Copyright 2017 Google LLC * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/translate/test/translate.test.js b/translate/test/translate.test.js index 07adde1121..733c0d4d43 100644 --- a/translate/test/translate.test.js +++ b/translate/test/translate.test.js @@ -1,5 +1,5 @@ /** - * Copyright 2017, Google, Inc. + * Copyright 2017 Google LLC * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,7 +16,7 @@ 'use strict'; const {assert} = require('chai'); -const {Translate} = require('@google-cloud/translate'); +const {Translate} = require('@google-cloud/translate').v2; const cp = require('child_process'); const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}).trim(); diff --git a/translate/test/v3/translate_batch_translate_text.test.js b/translate/test/v3/translate_batch_translate_text.test.js new file mode 100644 index 0000000000..b9633337e9 --- /dev/null +++ b/translate/test/v3/translate_batch_translate_text.test.js @@ -0,0 +1,77 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {TranslationServiceClient} = require('@google-cloud/translate'); +const {Storage} = require('@google-cloud/storage'); +const cp = require('child_process'); +const uuid = require('uuid'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const REGION_TAG = 'translate_batch_translate_text'; + +describe(REGION_TAG, () => { + const translationClient = new TranslationServiceClient(); + const location = 'us-central1'; + const bucketUuid = uuid.v4(); + const bucketName = `translation-${bucketUuid}/BATCH_TRANSLATION_OUTPUT/`; + const storage = new Storage(); + + before(async () => { + const projectId = await translationClient.getProjectId(); + + //Create bucket if needed + await storage + .createBucket(projectId, { + location: 'US', + storageClass: 'COLDLINE', + }) + .catch(error => { + if (error.code !== 409) { + //if it's not a duplicate bucket error, let the user know + console.error(error); + } + }); + }); + + it('should batch translate the input text', async () => { + const projectId = await translationClient.getProjectId(); + const inputUri = `gs://cloud-samples-data/translation/text.txt`; + + const outputUri = `gs://${projectId}/${bucketName}`; + const output = execSync( + `node v3/${REGION_TAG}.js ${projectId} ${location} ${inputUri} ${outputUri}` + ); + assert.match(output, /Total Characters: 13/); + assert.match(output, /Translated Characters: 13/); + }); + + // Delete the folder from GCS for cleanup + after(async function() { + const projectId = await translationClient.getProjectId(); + const options = { + prefix: `translation-${bucketUuid}`, + }; + + const bucket = await storage.bucket(projectId); + const [files] = await bucket.getFiles(options); + const length = files.length; + if (length > 0) { + await Promise.all(files.map(file => file.delete())); + } + }); +}); diff --git a/translate/test/v3/translate_batch_translate_text_with_glossary.test.js b/translate/test/v3/translate_batch_translate_text_with_glossary.test.js new file mode 100644 index 0000000000..bcb7615f15 --- /dev/null +++ b/translate/test/v3/translate_batch_translate_text_with_glossary.test.js @@ -0,0 +1,110 @@ +/** + * Copyright 2019 Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const {assert} = require('chai'); +const {TranslationServiceClient} = require('@google-cloud/translate'); +const {Storage} = require('@google-cloud/storage'); +const cp = require('child_process'); +const uuid = require('uuid'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const REGION_TAG = 'translate_batch_translate_text_with_glossary'; + +describe(REGION_TAG, () => { + const translationClient = new TranslationServiceClient(); + const location = 'us-central1'; + const glossaryId = 'my-fake_glossary'; + const bucketUuid = uuid.v4(); + const bucketName = `translation-${bucketUuid}/BATCH_TRANSLATION_OUTPUT/`; + const storage = new Storage(); + + before(async () => { + const projectId = await translationClient.getProjectId(); + + //Create bucket if needed + await storage + .createBucket(projectId, { + location: 'US', + storageClass: 'COLDLINE', + }) + .catch(error => { + if (error.code !== 409) { + //if it's not a duplicate bucket error, let the user know + console.error(error); + } + }); + + // Create glossary + const request = { + parent: `projects/${projectId}/locations/${location}`, + glossary: { + languageCodesSet: { + languageCodes: ['en', 'es'], + }, + inputConfig: { + gcsSource: { + inputUri: 'gs://cloud-samples-data/translation/glossary.csv', + }, + }, + name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, + }, + }; + + // Create glossary using a long-running operation. + const [operation] = await translationClient.createGlossary(request); + // Wait for operation to complete. + await operation.promise(); + }); + + it('should batch translate the input text with a glossary', async () => { + const projectId = await translationClient.getProjectId(); + const inputUri = `gs://cloud-samples-data/translation/text.txt`; + + const outputUri = `gs://${projectId}/${bucketName}`; + const output = execSync( + `node v3/${REGION_TAG}.js ${projectId} ${location} ${inputUri} ${outputUri} ${glossaryId}` + ); + assert.match(output, /Total Characters: 13/); + assert.match(output, /Translated Characters: 13/); + }); + + // Delete the folder from GCS for cleanup + after(async function() { + const projectId = await translationClient.getProjectId(); + const options = { + prefix: `translation-${bucketUuid}`, + }; + + const bucket = await storage.bucket(projectId); + const [files] = await bucket.getFiles(options); + const length = files.length; + if (length > 0) { + await Promise.all(files.map(file => file.delete())); + } + + // Delete the Glossary + const request = { + parent: `projects/${projectId}/locations/${location}`, + name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, + }; + // Delete glossary using a long-running operation. + const [operation] = await translationClient.deleteGlossary(request); + // Wait for operation to complete. + await operation.promise(); + }); +}); diff --git a/translate/test/v3/translate_batch_translate_text_with_glossary_and_model.test.js b/translate/test/v3/translate_batch_translate_text_with_glossary_and_model.test.js new file mode 100644 index 0000000000..bbc733c1a5 --- /dev/null +++ b/translate/test/v3/translate_batch_translate_text_with_glossary_and_model.test.js @@ -0,0 +1,111 @@ +/** + * Copyright 2019 Google LLC + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const {assert} = require('chai'); +const {TranslationServiceClient} = require('@google-cloud/translate'); +const {Storage} = require('@google-cloud/storage'); +const cp = require('child_process'); +const uuid = require('uuid'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const REGION_TAG = 'translate_batch_translate_text_with_glossary_and_model'; + +describe(REGION_TAG, () => { + const translationClient = new TranslationServiceClient(); + const location = 'us-central1'; + const glossaryId = 'my-fake_glossary'; + const modelId = 'TRL1218052175389786112'; + const bucketUuid = uuid.v4(); + const bucketName = `translation-${bucketUuid}/BATCH_TRANSLATION_OUTPUT/`; + const storage = new Storage(); + + before(async () => { + const projectId = await translationClient.getProjectId(); + + //Create bucket if needed + await storage + .createBucket(projectId, { + location: 'US', + storageClass: 'COLDLINE', + }) + .catch(error => { + if (error.code !== 409) { + //if it's not a duplicate bucket error, let the user know + console.error(error); + } + }); + + // Create glossary + const request = { + parent: `projects/${projectId}/locations/${location}`, + glossary: { + languageCodesSet: { + languageCodes: ['en', 'ja'], + }, + inputConfig: { + gcsSource: { + inputUri: 'gs://cloud-samples-data/translation/glossary_ja.csv', + }, + }, + name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, + }, + }; + + // Create glossary using a long-running operation. + const [operation] = await translationClient.createGlossary(request); + // Wait for operation to complete. + await operation.promise(); + }); + + it('should batch translate the input text with a glossary', async () => { + const projectId = await translationClient.getProjectId(); + const inputUri = `gs://cloud-samples-data/translation/text_with_custom_model_and_glossary.txt`; + + const outputUri = `gs://${projectId}/${bucketName}`; + const output = execSync( + `node v3/${REGION_TAG}.js ${projectId} ${location} ${inputUri} ${outputUri} ${glossaryId} ${modelId}` + ); + assert.match(output, /Total Characters: 25/); + assert.match(output, /Translated Characters: 25/); + }); + + // Delete the folder from GCS for cleanup + after(async function() { + const projectId = await translationClient.getProjectId(); + const options = { + prefix: `translation-${bucketUuid}`, + }; + + const bucket = await storage.bucket(projectId); + const [files] = await bucket.getFiles(options); + const length = files.length; + if (length > 0) { + await Promise.all(files.map(file => file.delete())); + } + + // Delete the Glossary + const request = { + parent: `projects/${projectId}/locations/${location}`, + name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, + }; + // Delete glossary using a long-running operation. + const [operation] = await translationClient.deleteGlossary(request); + // Wait for operation to complete. + await operation.promise(); + }); +}); diff --git a/translate/test/v3/translate_batch_translate_text_with_model.test.js b/translate/test/v3/translate_batch_translate_text_with_model.test.js new file mode 100644 index 0000000000..a668bb051b --- /dev/null +++ b/translate/test/v3/translate_batch_translate_text_with_model.test.js @@ -0,0 +1,78 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {TranslationServiceClient} = require('@google-cloud/translate'); +const {Storage} = require('@google-cloud/storage'); +const cp = require('child_process'); +const uuid = require('uuid'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const REGION_TAG = 'translate_batch_translate_text_with_model'; + +describe(REGION_TAG, () => { + const translationClient = new TranslationServiceClient(); + const location = 'us-central1'; + const modelId = 'TRL1218052175389786112'; + const bucketUuid = uuid.v4(); + const bucketName = `translation-${bucketUuid}/BATCH_TRANSLATION_OUTPUT/`; + const storage = new Storage(); + + before(async () => { + const projectId = await translationClient.getProjectId(); + + //Create bucket if needed + await storage + .createBucket(projectId, { + location: 'US', + storageClass: 'COLDLINE', + }) + .catch(error => { + if (error.code !== 409) { + //if it's not a duplicate bucket error, let the user know + console.error(error); + } + }); + }); + + it('should batch translate the input text with a model', async () => { + const projectId = await translationClient.getProjectId(); + const inputUri = `gs://cloud-samples-data/translation/custom_model_text.txt`; + + const outputUri = `gs://${projectId}/${bucketName}`; + const output = execSync( + `node v3/${REGION_TAG}.js ${projectId} ${location} ${inputUri} ${outputUri} ${modelId}` + ); + assert.match(output, /Total Characters: 15/); + assert.match(output, /Translated Characters: 15/); + }); + + // Delete the folder from GCS for cleanup + after(async function() { + const projectId = await translationClient.getProjectId(); + const options = { + prefix: `translation-${bucketUuid}`, + }; + + const bucket = await storage.bucket(projectId); + const [files] = await bucket.getFiles(options); + const length = files.length; + if (length > 0) { + await Promise.all(files.map(file => file.delete())); + } + }); +}); diff --git a/translate/test/v3/translate_create_glossary.test.js b/translate/test/v3/translate_create_glossary.test.js new file mode 100644 index 0000000000..44f8ae0a7a --- /dev/null +++ b/translate/test/v3/translate_create_glossary.test.js @@ -0,0 +1,58 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {TranslationServiceClient} = require('@google-cloud/translate'); +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const REGION_TAG = 'translate_create_glossary'; + +describe(REGION_TAG, () => { + const translationClient = new TranslationServiceClient(); + + it('should create a glossary', async function() { + const projectId = await translationClient.getProjectId(); + const location = 'us-central1'; + const glossaryId = 'test-glossary'; + const output = execSync( + `node v3/${REGION_TAG}.js ${projectId} ${location} ${glossaryId}` + ); + assert.match( + output, + /gs:\/\/cloud-samples-data\/translation\/glossary.csv/ + ); + }); + + after('cleanup for glossary create', async function() { + const projectId = await translationClient.getProjectId(); + const location = 'us-central1'; + const glossaryId = 'test-glossary'; + // Delete the glossary to clean up + const request = { + parent: `projects/${projectId}/locations/${location}`, + name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, + }; + + // Delete glossary using a long-running operation. + // You can wait for now, or get results later. + const [operation] = await translationClient.deleteGlossary(request); + + // Wait for operation to complete. + await operation.promise(); + }); +}); diff --git a/translate/test/v3/translate_delete_glossary.test.js b/translate/test/v3/translate_delete_glossary.test.js new file mode 100644 index 0000000000..b5ce3f36cf --- /dev/null +++ b/translate/test/v3/translate_delete_glossary.test.js @@ -0,0 +1,68 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {TranslationServiceClient} = require('@google-cloud/translate'); +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const REGION_TAG = 'translate_delete_glossary'; + +describe(REGION_TAG, () => { + const translationClient = new TranslationServiceClient(); + const location = 'us-central1'; + const glossaryId = 'glossary'; + + before(async function() { + // Add a glossary to be deleted + // const translationClient = new TranslationServiceClient(); + const projectId = await translationClient.getProjectId(); + const glossary = { + languageCodesSet: { + languageCodes: ['en', 'es'], + }, + inputConfig: { + gcsSource: { + inputUri: 'gs://cloud-samples-data/translation/glossary.csv', + }, + }, + name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, + }; + + // Construct request + const request = { + parent: `projects/${projectId}/locations/${location}`, + glossary: glossary, + }; + + // Create glossary using a long-running operation. + // You can wait for now, or get results later. + const [operation] = await translationClient.createGlossary(request); + + // Wait for operation to complete. + await operation.promise(); + }); + + it('should delete a glossary', async () => { + const projectId = await translationClient.getProjectId(); + + const output = execSync( + `node v3/${REGION_TAG}.js ${projectId} ${location} ${glossaryId}` + ); + assert.match(output, /glossary/); + }); +}); diff --git a/translate/test/v3/translate_detect_language.test.js b/translate/test/v3/translate_detect_language.test.js new file mode 100644 index 0000000000..3ebf4be02a --- /dev/null +++ b/translate/test/v3/translate_detect_language.test.js @@ -0,0 +1,37 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {TranslationServiceClient} = require('@google-cloud/translate'); +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const REGION_TAG = 'translate_detect_language'; + +describe(REGION_TAG, () => { + it('should detect the language of the input text', async () => { + const translationClient = new TranslationServiceClient(); + const projectId = await translationClient.getProjectId(); + const location = 'global'; + const text = `'Hæ sæta'`; + const output = execSync( + `node v3/${REGION_TAG}.js ${projectId} ${location} ${text}` + ); + assert.match(output, /Language Code: is/); + assert.match(output, /Confidence: 1/); + }); +}); diff --git a/translate/test/v3/translate_get_glossary.test.js b/translate/test/v3/translate_get_glossary.test.js new file mode 100644 index 0000000000..eb7de3ed89 --- /dev/null +++ b/translate/test/v3/translate_get_glossary.test.js @@ -0,0 +1,83 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {TranslationServiceClient} = require('@google-cloud/translate'); +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const REGION_TAG = 'translate_get_glossary'; + +describe(REGION_TAG, () => { + const translationClient = new TranslationServiceClient(); + const location = 'us-central1'; + const glossaryId = 'test-glossary'; + + before(async function() { + // Add a glossary to get + const projectId = await translationClient.getProjectId(); + const glossary = { + languageCodesSet: { + languageCodes: ['en', 'es'], + }, + inputConfig: { + gcsSource: { + inputUri: 'gs://cloud-samples-data/translation/glossary.csv', + }, + }, + name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, + }; + + // Construct request + const request = { + parent: `projects/${projectId}/locations/${location}`, + glossary: glossary, + }; + + // Create glossary using a long-running operation. + // You can wait for now, or get results later. + const [operation] = await translationClient.createGlossary(request); + + // Wait for operation to complete. + await operation.promise(); + }); + + it('should get a glossary', async () => { + const projectId = await translationClient.getProjectId(); + + const output = execSync( + `node v3/${REGION_TAG}.js ${projectId} ${location} ${glossaryId}` + ); + assert.match(output, /test-glossary/); + }); + + after(async function() { + //delete the glossary we created + const projectId = await translationClient.getProjectId(); + const request = { + parent: `projects/${projectId}/locations/${location}`, + name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, + }; + + // Delete glossary using a long-running operation. + // You can wait for now, or get results later. + const [operation] = await translationClient.deleteGlossary(request); + + // Wait for operation to complete. + await operation.promise(); + }); +}); diff --git a/translate/test/v3/translate_get_supported_languages.test.js b/translate/test/v3/translate_get_supported_languages.test.js new file mode 100644 index 0000000000..990ffbec8b --- /dev/null +++ b/translate/test/v3/translate_get_supported_languages.test.js @@ -0,0 +1,32 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {TranslationServiceClient} = require('@google-cloud/translate'); +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const REGION_TAG = 'translate_get_supported_languages'; + +describe(REGION_TAG, () => { + it('should get supported langauges', async () => { + const translationClient = new TranslationServiceClient(); + const projectId = await translationClient.getProjectId(); + const output = execSync(`node v3/${REGION_TAG}.js ${projectId}`); + assert.match(output, /zh-CN/); + }); +}); diff --git a/translate/test/v3/translate_get_supported_languages_for_targets.test.js b/translate/test/v3/translate_get_supported_languages_for_targets.test.js new file mode 100644 index 0000000000..c16b54139d --- /dev/null +++ b/translate/test/v3/translate_get_supported_languages_for_targets.test.js @@ -0,0 +1,33 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {TranslationServiceClient} = require('@google-cloud/translate'); +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const REGION_TAG = 'translate_get_supported_languages_for_target'; + +describe(REGION_TAG, () => { + it('should get supported langauges for target', async () => { + const translationClient = new TranslationServiceClient(); + const projectId = await translationClient.getProjectId(); + const output = execSync(`node v3/${REGION_TAG}.js ${projectId}`); + assert.match(output, /Language Code: yi/); + assert.match(output, /Display Name: Yiddish/); + }); +}); diff --git a/translate/test/v3/translate_list_codes.test.js b/translate/test/v3/translate_list_codes.test.js new file mode 100644 index 0000000000..725a86fd59 --- /dev/null +++ b/translate/test/v3/translate_list_codes.test.js @@ -0,0 +1,33 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {TranslationServiceClient} = require('@google-cloud/translate'); +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const REGION_TAG = 'translate_list_codes'; + +describe(REGION_TAG, () => { + it('should list available language codes', async () => { + const translationClient = new TranslationServiceClient(); + const projectId = await translationClient.getProjectId(); + const output = execSync(`node v3/${REGION_TAG}.js ${projectId}`); + assert.match(output, /Language Code: en/); + assert.match(output, /Language Code: fr/); + }); +}); diff --git a/translate/test/v3/translate_list_glossary.test.js b/translate/test/v3/translate_list_glossary.test.js new file mode 100644 index 0000000000..eb7d376442 --- /dev/null +++ b/translate/test/v3/translate_list_glossary.test.js @@ -0,0 +1,86 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {TranslationServiceClient} = require('@google-cloud/translate'); +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const REGION_TAG = 'translate_list_glossary'; + +describe(REGION_TAG, () => { + const translationClient = new TranslationServiceClient(); + const location = 'us-central1'; + const glossaryId = 'test-glossary'; + + before(async function() { + // Add a glossary to be deleted + const projectId = await translationClient.getProjectId(); + + const glossary = { + languageCodesSet: { + languageCodes: ['en', 'es'], + }, + inputConfig: { + gcsSource: { + inputUri: 'gs://cloud-samples-data/translation/glossary.csv', + }, + }, + name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, + }; + + // Construct request + const request = { + parent: `projects/${projectId}/locations/${location}`, + glossary: glossary, + }; + + // Create glossary using a long-running operation. + // You can wait for now, or get results later. + const [operation] = await translationClient.createGlossary(request); + + // Wait for operation to complete. + await operation.promise(); + }); + + it('should list glossaries in project', async () => { + const projectId = await translationClient.getProjectId(); + const output = execSync( + `node v3/${REGION_TAG}.js ${projectId} ${location}` + ); + assert.match( + output, + /gs:\/\/cloud-samples-data\/translation\/glossary.csv/ + ); + }); + + after(async function() { + const projectId = await translationClient.getProjectId(); + + const request = { + parent: `projects/${projectId}/locations/${location}`, + name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, + }; + + // Delete glossary using a long-running operation. + // You can wait for now, or get results later. + const [operation] = await translationClient.deleteGlossary(request); + + // Wait for operation to complete. + await operation.promise(); + }); +}); diff --git a/translate/test/v3/translate_list_language_names.test.js b/translate/test/v3/translate_list_language_names.test.js new file mode 100644 index 0000000000..e8343fff4f --- /dev/null +++ b/translate/test/v3/translate_list_language_names.test.js @@ -0,0 +1,35 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {TranslationServiceClient} = require('@google-cloud/translate'); +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const REGION_TAG = 'translate_list_language_names'; + +describe(REGION_TAG, () => { + it('should list available language names', async () => { + const translationClient = new TranslationServiceClient(); + const projectId = await translationClient.getProjectId(); + const output = execSync(`node v3/${REGION_TAG}.js ${projectId}`); + assert.match(output, /Language Code: en/); + assert.match(output, /Display Name: Anglais/); + assert.match(output, /Language Code: fr/); + assert.match(output, /Display Name: Français/); + }); +}); diff --git a/translate/test/v3/translate_translate_text.test.js b/translate/test/v3/translate_translate_text.test.js new file mode 100644 index 0000000000..efc11b4582 --- /dev/null +++ b/translate/test/v3/translate_translate_text.test.js @@ -0,0 +1,36 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {TranslationServiceClient} = require('@google-cloud/translate'); +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const REGION_TAG = 'translate_translate_text'; + +describe(REGION_TAG, () => { + it('should translate the input text', async () => { + const translationClient = new TranslationServiceClient(); + const projectId = await translationClient.getProjectId(); + const location = `global`; + const text = `"Hello world"`; + const output = execSync( + `node v3/${REGION_TAG}.js ${projectId} ${location} ${text}` + ); + assert.match(output, /Translation: Pozdrav svijetu/); + }); +}); diff --git a/translate/test/v3/translate_translate_text_with_glossary.test.js b/translate/test/v3/translate_translate_text_with_glossary.test.js new file mode 100644 index 0000000000..e66eeef0a0 --- /dev/null +++ b/translate/test/v3/translate_translate_text_with_glossary.test.js @@ -0,0 +1,83 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {TranslationServiceClient} = require('@google-cloud/translate'); +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const REGION_TAG = 'translate_translate_text_with_glossary'; + +describe(REGION_TAG, () => { + const translationClient = new TranslationServiceClient(); + const location = 'us-central1'; + const glossaryId = 'test-glossary'; + + before(async function() { + // Add a glossary to be translate with + const projectId = await translationClient.getProjectId(); + + const glossary = { + languageCodesSet: { + languageCodes: ['en', 'es'], + }, + inputConfig: { + gcsSource: { + inputUri: 'gs://cloud-samples-data/translation/glossary.csv', + }, + }, + name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, + }; + + // Construct request + const request = { + parent: `projects/${projectId}/locations/${location}`, + glossary: glossary, + }; + + // Create glossary using a long-running operation. + // You can wait for now, or get results later. + const [operation] = await translationClient.createGlossary(request); + + // Wait for operation to complete. + await operation.promise(); + }); + + it('should translate text with a glossary in project', async () => { + const projectId = await translationClient.getProjectId(); + const input = 'directions'; + const output = execSync( + `node v3/${REGION_TAG}.js ${projectId} ${location} ${glossaryId} ${input}` + ); + assert.match(output, /indicaciones/); + }); + + after(async function() { + const projectId = await translationClient.getProjectId(); + const request = { + parent: `projects/${projectId}/locations/${location}`, + name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, + }; + + // Delete glossary using a long-running operation. + // You can wait for now, or get results later. + const [operation] = await translationClient.deleteGlossary(request); + + // Wait for operation to complete. + await operation.promise(); + }); +}); diff --git a/translate/test/v3/translate_translate_text_with_glossary_and_model.test.js b/translate/test/v3/translate_translate_text_with_glossary_and_model.test.js new file mode 100644 index 0000000000..1f37483149 --- /dev/null +++ b/translate/test/v3/translate_translate_text_with_glossary_and_model.test.js @@ -0,0 +1,84 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {TranslationServiceClient} = require('@google-cloud/translate'); +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const REGION_TAG = 'translate_translate_text_with_glossary_and_model'; + +describe(REGION_TAG, () => { + const translationClient = new TranslationServiceClient(); + const location = 'us-central1'; + const glossaryId = 'test-glossary'; + const modelId = 'TRL1218052175389786112'; + + before(async function() { + // Add a glossary to be translate with + const projectId = await translationClient.getProjectId(); + + const glossary = { + languageCodesSet: { + languageCodes: ['en', 'ja'], + }, + inputConfig: { + gcsSource: { + inputUri: 'gs://cloud-samples-data/translation/glossary_ja.csv', + }, + }, + name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, + }; + + // Construct request + const request = { + parent: `projects/${projectId}/locations/${location}`, + glossary: glossary, + }; + + // Create glossary using a long-running operation. + // You can wait for now, or get results later. + const [operation] = await translationClient.createGlossary(request); + + // Wait for operation to complete. + await operation.promise(); + }); + + it('should translate text with a glossary and Automl model in project', async () => { + const projectId = await translationClient.getProjectId(); + const input = 'directions'; + const output = execSync( + `node v3/${REGION_TAG}.js ${projectId} ${location} ${glossaryId} ${modelId} ${input}` + ); + assert.match(output, /道順/); + }); + + after(async function() { + const projectId = await translationClient.getProjectId(); + const request = { + parent: `projects/${projectId}/locations/${location}`, + name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, + }; + + // Delete glossary using a long-running operation. + // You can wait for now, or get results later. + const [operation] = await translationClient.deleteGlossary(request); + + // Wait for operation to complete. + await operation.promise(); + }); +}); diff --git a/translate/test/v3/translate_translate_text_with_model.test.js b/translate/test/v3/translate_translate_text_with_model.test.js new file mode 100644 index 0000000000..938d039871 --- /dev/null +++ b/translate/test/v3/translate_translate_text_with_model.test.js @@ -0,0 +1,37 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {TranslationServiceClient} = require('@google-cloud/translate'); +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const REGION_TAG = 'translate_translate_text_with_model'; + +describe(REGION_TAG, () => { + const translationClient = new TranslationServiceClient(); + const location = 'us-central1'; + const modelId = 'TRL1218052175389786112'; + const input = 'Tell me how this ends'; + + it('should translate text with an automl model in project', async () => { + const projectId = await translationClient.getProjectId(); + const output = await execSync( + `node v3/${REGION_TAG}.js ${projectId} ${location} ${modelId} ${input}` + ); + assert.match(output, /Translated Content: 教えて/); + }); +}); diff --git a/translate/translate.js b/translate/translate.js index 25f4d4b20b..7ce148f433 100644 --- a/translate/translate.js +++ b/translate/translate.js @@ -1,5 +1,5 @@ /** - * Copyright 2017, Google, Inc. + * Copyright 2017 Google LLC * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -15,10 +15,10 @@ 'use strict'; -async function detectLanguage(text) { +function detectLanguageSample(text) { // [START translate_detect_language] // Imports the Google Cloud client library - const {Translate} = require('@google-cloud/translate'); + const {Translate} = require('@google-cloud/translate').v2; // Creates a client const translate = new Translate(); @@ -31,36 +31,43 @@ async function detectLanguage(text) { // Detects the language. "text" can be a string for detecting the language of // a single piece of text, or an array of strings for detecting the languages // of multiple texts. - let [detections] = await translate.detect(text); - detections = Array.isArray(detections) ? detections : [detections]; - console.log('Detections:'); - detections.forEach(detection => { - console.log(`${detection.input} => ${detection.language}`); - }); - + async function detectLanguage() { + let [detections] = await translate.detect(text); + detections = Array.isArray(detections) ? detections : [detections]; + console.log('Detections:'); + detections.forEach(detection => { + console.log(`${detection.input} => ${detection.language}`); + }); + } + + detectLanguage(); // [END translate_detect_language] } -async function listLanguages() { +function listLanguagesSample() { // [START translate_list_codes] // Imports the Google Cloud client library - const {Translate} = require('@google-cloud/translate'); + const {Translate} = require('@google-cloud/translate').v2; // Creates a client const translate = new Translate(); - // Lists available translation language with their names in English (the default). - const [languages] = await translate.getLanguages(); + async function listLanguages() { + // Lists available translation language with their names in English (the default). + const [languages] = await translate.getLanguages(); + + console.log('Languages:'); + languages.forEach(language => console.log(language)); + } - console.log('Languages:'); - languages.forEach(language => console.log(language)); + listLanguages(); // [END translate_list_codes] } -async function listLanguagesWithTarget(target) { +function listLanguagesWithTargetSample(target) { // [START translate_list_language_names] // Imports the Google Cloud client library - const {Translate} = require('@google-cloud/translate'); + const {Translate} = require('@google-cloud/translate').v2; // Creates a client const translate = new Translate(); @@ -70,19 +77,22 @@ async function listLanguagesWithTarget(target) { */ // const target = 'The target language for language names, e.g. ru'; - // Lists available translation language with their names in a target language - const [languages] = await translate.getLanguages(target); + async function listLanguagesWithTarget() { + // Lists available translation language with their names in a target language + const [languages] = await translate.getLanguages(target); - console.log('Languages:'); - languages.forEach(language => console.log(language)); + console.log('Languages:'); + languages.forEach(language => console.log(language)); + } + listLanguagesWithTarget(); // [END translate_list_language_names] } -async function translateText(text, target) { +function translateTextSample(text, target) { // [START translate_translate_text] // Imports the Google Cloud client library - const {Translate} = require('@google-cloud/translate'); + const {Translate} = require('@google-cloud/translate').v2; // Creates a client const translate = new Translate(); @@ -93,23 +103,26 @@ async function translateText(text, target) { // const text = 'The text to translate, e.g. Hello, world!'; // const target = 'The target language, e.g. ru'; - // Translates the text into the target language. "text" can be a string for - // translating a single piece of text, or an array of strings for translating - // multiple texts. - 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}`); - }); - + async function translateText() { + // Translates the text into the target language. "text" can be a string for + // translating a single piece of text, or an array of strings for translating + // multiple texts. + 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}`); + }); + } + + translateText(); // [END translate_translate_text] } -async function translateTextWithModel(text, target, model) { +function translateTextWithModelSample(text, target, model) { // [START translate_text_with_model] // Imports the Google Cloud client library - const {Translate} = require('@google-cloud/translate'); + const {Translate} = require('@google-cloud/translate').v2; // Creates a client const translate = new Translate(); @@ -121,23 +134,27 @@ async function translateTextWithModel(text, target, model) { // const target = 'The target language, e.g. ru'; // const model = 'The model to use, e.g. nmt'; - const options = { - // The target language, e.g. "ru" - to: target, - // Make sure your project is whitelisted. - // Possible values are "base" and "nmt" - model: model, - }; - - // Translates the text into the target language. "text" can be a string for - // translating a single piece of text, or an array of strings for translating - // multiple texts. - let [translations] = await translate.translate(text, options); - translations = Array.isArray(translations) ? translations : [translations]; - console.log('Translations:'); - translations.forEach((translation, i) => { - console.log(`${text[i]} => (${target}) ${translation}`); - }); + async function translateTextWithModel() { + const options = { + // The target language, e.g. "ru" + to: target, + // Make sure your project is whitelisted. + // Possible values are "base" and "nmt" + model: model, + }; + + // Translates the text into the target language. "text" can be a string for + // translating a single piece of text, or an array of strings for translating + // multiple texts. + let [translations] = await translate.translate(text, options); + translations = Array.isArray(translations) ? translations : [translations]; + console.log('Translations:'); + translations.forEach((translation, i) => { + console.log(`${text[i]} => (${target}) ${translation}`); + }); + } + + translateTextWithModel(); // [END translate_text_with_model] } @@ -147,7 +164,7 @@ require(`yargs`) `detect `, `Detects the language of one or more strings.`, {}, - async opts => await detectLanguage(opts.text).catch(console.error) + async opts => await detectLanguageSample(opts.text) ) .command( `list [target]`, @@ -155,9 +172,9 @@ require(`yargs`) {}, async opts => { if (opts.target) { - await listLanguagesWithTarget(opts.target).catch(console.error); + await listLanguagesWithTargetSample(opts.target); } else { - await listLanguages().catch(console.error); + await listLanguagesSample(); } } ) @@ -165,17 +182,14 @@ require(`yargs`) `translate `, `Translates one or more strings into the target language.`, {}, - async opts => - await translateText(opts.text, opts.toLang).catch(console.error) + async opts => await translateTextSample(opts.text, opts.toLang) ) .command( `translate-with-model `, `Translates one or more strings into the target language using the specified model.`, {}, async opts => - await translateTextWithModel(opts.text, opts.toLang, opts.model).catch( - console.error - ) + await translateTextWithModelSample(opts.text, opts.toLang, opts.model) ) .example(`node $0 detect "Hello world!"`, `Detects the language of a string.`) .example( diff --git a/translate/v3/translate_batch_translate_text.js b/translate/v3/translate_batch_translate_text.js new file mode 100644 index 0000000000..ae904b842d --- /dev/null +++ b/translate/v3/translate_batch_translate_text.js @@ -0,0 +1,73 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + inputUri = 'gs://cloud-samples-data/translation/text.txt', + outputUri = 'gs://YOUR_PROJECT_ID/translation/BATCH_TRANSLATION_OUTPUT/' +) { + // [START translate_v3_batch_translate_text] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const inputUri = 'gs://cloud-samples-data/text.txt'; + // const outputUri = 'gs://YOUR_BUCKET_ID/path_to_store_results/'; + + // Imports the Google Cloud Translation library + const {TranslationServiceClient} = require('@google-cloud/translate'); + + // Instantiates a client + const translationClient = new TranslationServiceClient(); + async function batchTranslateText() { + // Construct request + const request = { + parent: `projects/${projectId}/locations/${location}`, + sourceLanguageCode: 'en', + targetLanguageCodes: ['ja'], + inputConfigs: [ + { + mimeType: 'text/plain', // mime types: text/plain, text/html + gcsSource: { + inputUri: inputUri, + }, + }, + ], + outputConfig: { + gcsDestination: { + outputUriPrefix: outputUri, + }, + }, + }; + + // Batch translate text using a long-running operation. + // You can wait for now, or get results later. + const [operation] = await translationClient.batchTranslateText(request); + + // Wait for operation to complete. + const [response] = await operation.promise(); + + console.log(`Total Characters: ${response.totalCharacters}`); + console.log(`Translated Characters: ${response.translatedCharacters}`); + } + + batchTranslateText(); + // [END translate_v3_batch_translate_text] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3/translate_batch_translate_text_with_glossary.js b/translate/v3/translate_batch_translate_text_with_glossary.js new file mode 100644 index 0000000000..2616ac9693 --- /dev/null +++ b/translate/v3/translate_batch_translate_text_with_glossary.js @@ -0,0 +1,80 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + inputUri = 'gs://cloud-samples-data/translation/text.txt', + outputUri = 'gs://YOUR_PROJECT_ID/translation/BATCH_TRANSLATION_OUTPUT/', + glossaryId = 'YOUR_GLOSSARY_ID' +) { + // [START translate_v3_batch_translate_text_with_glossary] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const inputUri = 'gs://cloud-samples-data/text.txt'; + // const outputUri = 'gs://YOUR_BUCKET_ID/path_to_store_results/'; + // const glossaryId = 'YOUR_GLOSSARY_ID'; + + // Imports the Google Cloud Translation library + const {TranslationServiceClient} = require('@google-cloud/translate'); + + // Instantiates a client + const client = new TranslationServiceClient(); + async function batchTranslateTextWithGlossary() { + // Construct request + const request = { + parent: `projects/${projectId}/locations/${location}`, + sourceLanguageCode: 'en', + targetLanguageCodes: ['es'], + inputConfigs: [ + { + mimeType: 'text/plain', // mime types: text/plain, text/html + gcsSource: { + inputUri: inputUri, + }, + }, + ], + outputConfig: { + gcsDestination: { + outputUriPrefix: outputUri, + }, + }, + glossaries: { + es: { + glossary: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, + }, + }, + }; + + // Create a job whose results you can either wait for now, or get later + const [operation] = await client.batchTranslateText(request); + + // Get a Promise representation of the final result of the job + const [response] = await operation.promise(); + + // Display the translation for each input text provided + console.log(`Total Characters: ${response.totalCharacters}`); + console.log(`Translated Characters: ${response.translatedCharacters}`); + } + + batchTranslateTextWithGlossary(); + // [END translate_v3_batch_translate_text_with_glossary] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3/translate_batch_translate_text_with_glossary_and_model.js b/translate/v3/translate_batch_translate_text_with_glossary_and_model.js new file mode 100644 index 0000000000..e72dc6184c --- /dev/null +++ b/translate/v3/translate_batch_translate_text_with_glossary_and_model.js @@ -0,0 +1,85 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + inputUri = 'gs://cloud-samples-data/translation/text.txt', + outputUri = 'gs://YOUR_PROJECT_ID/translation/BATCH_TRANSLATION_OUTPUT/', + glossaryId = 'YOUR_GLOSSARY_ID', + modelId = 'YOUR_MODEL_ID' +) { + // [START translate_v3_batch_translate_text_with_glossary_and_model] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const inputUri = 'gs://cloud-samples-data/text.txt'; + // const outputUri = 'gs://YOUR_BUCKET_ID/path_to_store_results/'; + // const glossaryId = 'YOUR_GLOSSARY_ID'; + // const modelId = 'YOUR_MODEL_ID'; + + // Imports the Google Cloud Translation library + const {TranslationServiceClient} = require('@google-cloud/translate'); + + // Instantiates a client + const client = new TranslationServiceClient(); + async function batchTranslateTextWithGlossaryAndModel() { + // Construct request + const request = { + parent: `projects/${projectId}/locations/${location}`, + sourceLanguageCode: 'en', + targetLanguageCodes: ['ja'], + inputConfigs: [ + { + mimeType: 'text/plain', // mime types: text/plain, text/html + gcsSource: { + inputUri: inputUri, + }, + }, + ], + outputConfig: { + gcsDestination: { + outputUriPrefix: outputUri, + }, + }, + glossaries: { + ja: { + glossary: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, + }, + }, + models: { + ja: `projects/${projectId}/locations/${location}/models/${modelId}`, + }, + }; + + // Create a job whose results you can either wait for now, or get later + const [operation] = await client.batchTranslateText(request); + + // Get a Promise representation of the final result of the job + const [response] = await operation.promise(); + + // Display the translation for each input text provided + console.log(`Total Characters: ${response.totalCharacters}`); + console.log(`Translated Characters: ${response.translatedCharacters}`); + } + + batchTranslateTextWithGlossaryAndModel(); + // [END translate_v3_batch_translate_text_with_glossary_and_model] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3/translate_batch_translate_text_with_model.js b/translate/v3/translate_batch_translate_text_with_model.js new file mode 100644 index 0000000000..4ec26b02f0 --- /dev/null +++ b/translate/v3/translate_batch_translate_text_with_model.js @@ -0,0 +1,78 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + inputUri = 'gs://cloud-samples-data/translation/text.txt', + outputUri = 'gs://YOUR_PROJECT_ID/translation/BATCH_TRANSLATION_OUTPUT/', + modelId = 'YOUR_MODEL_ID' +) { + // [START translate_v3_batch_translate_text_with_model] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const inputUri = 'gs://cloud-samples-data/text.txt'; + // const outputUri = 'gs://YOUR_BUCKET_ID/path_to_store_results/'; + // const modelId = 'YOUR_MODEL_ID'; + + // Imports the Google Cloud Translation library + const {TranslationServiceClient} = require('@google-cloud/translate'); + + // Instantiates a client + const client = new TranslationServiceClient(); + async function batchTranslateTextWithModel() { + // Construct request + const request = { + parent: `projects/${projectId}/locations/${location}`, + sourceLanguageCode: 'en', + targetLanguageCodes: ['ja'], + inputConfigs: [ + { + mimeType: 'text/plain', // mime types: text/plain, text/html + gcsSource: { + inputUri: inputUri, + }, + }, + ], + outputConfig: { + gcsDestination: { + outputUriPrefix: outputUri, + }, + }, + models: { + ja: `projects/${projectId}/locations/${location}/models/${modelId}`, + }, + }; + + // Create a job whose results you can either wait for now, or get later + const [operation] = await client.batchTranslateText(request); + + // Get a Promise representation of the final result of the job + const [response] = await operation.promise(); + + // Display the translation for each input text provided + console.log(`Total Characters: ${response.totalCharacters}`); + console.log(`Translated Characters: ${response.translatedCharacters}`); + } + + batchTranslateTextWithModel(); + // [END translate_v3_batch_translate_text_with_model] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3/translate_create_glossary.js b/translate/v3/translate_create_glossary.js new file mode 100644 index 0000000000..88b96942eb --- /dev/null +++ b/translate/v3/translate_create_glossary.js @@ -0,0 +1,71 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + glossaryId = 'glossary-id' +) { + // [START translate_v3_create_glossary] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'global'; + // const glossaryId = 'YOUR_GLOSSARY_ID'; + + // Imports the Google Cloud Translation library + const {TranslationServiceClient} = require('@google-cloud/translate'); + + // Instantiates a client + const translationClient = new TranslationServiceClient(); + + async function createGlossary() { + // Construct glossary + const glossary = { + languageCodesSet: { + languageCodes: ['en', 'es'], + }, + inputConfig: { + gcsSource: { + inputUri: 'gs://cloud-samples-data/translation/glossary.csv', + }, + }, + name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, + }; + + // Construct request + const request = { + parent: `projects/${projectId}/locations/${location}`, + glossary: glossary, + }; + + // Create glossary using a long-running operation. + // You can wait for now, or get results later. + const [operation] = await translationClient.createGlossary(request); + + // Wait for operation to complete. + await operation.promise(); + + console.log(`Created glossary:`); + console.log(`InputUri ${request.glossary.inputConfig.gcsSource.inputUri}`); + } + + createGlossary(); + // [END translate_v3_create_glossary] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3/translate_delete_glossary.js b/translate/v3/translate_delete_glossary.js new file mode 100644 index 0000000000..bfe2b7351a --- /dev/null +++ b/translate/v3/translate_delete_glossary.js @@ -0,0 +1,57 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + glossaryId = 'glossary-id' +) { + // [START translate_v3_delete_glossary] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'global'; + // const glossaryId = 'YOUR_GLOSSARY_ID'; + + // Imports the Google Cloud Translation library + const {TranslationServiceClient} = require('@google-cloud/translate'); + + // Instantiates a client + const translationClient = new TranslationServiceClient(); + + async function deleteGlossary() { + // Construct request + const request = { + parent: `projects/${projectId}/locations/${location}`, + name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, + }; + + // Delete glossary using a long-running operation. + // You can wait for now, or get results later. + const [operation] = await translationClient.deleteGlossary(request); + + // Wait for operation to complete. + const [response] = await operation.promise(); + + console.log(`Deleted glossary: ${response.name}`); + } + + deleteGlossary(); + // [END translate_v3_delete_glossary] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3/translate_detect_language.js b/translate/v3/translate_detect_language.js new file mode 100644 index 0000000000..d0bf38223a --- /dev/null +++ b/translate/v3/translate_detect_language.js @@ -0,0 +1,57 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'global', + text = 'text to translate' +) { + // [START translate_v3_detect_language] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'global'; + // const text = 'text to translate'; + + // Imports the Google Cloud Translation library + const {TranslationServiceClient} = require('@google-cloud/translate'); + + // Instantiates a client + const translationClient = new TranslationServiceClient(); + + async function detectLanguage() { + // Construct request + const request = { + parent: `projects/${projectId}/locations/${location}`, + content: text, + }; + + // Run request + const [response] = await translationClient.detectLanguage(request); + + console.log(`Detected Languages:`); + for (const language of response.languages) { + console.log(`Language Code: ${language.languageCode}`); + console.log(`Confidence: ${language.confidence}`); + } + } + + detectLanguage(); + // [END translate_v3_detect_language] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3/translate_get_glossary.js b/translate/v3/translate_get_glossary.js new file mode 100644 index 0000000000..3486e402a6 --- /dev/null +++ b/translate/v3/translate_get_glossary.js @@ -0,0 +1,53 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + glossaryId = 'glossary-id' +) { + // [START translate_v3_get_glossary] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'global'; + // const glossaryId = 'YOUR_GLOSSARY_ID'; + + // Imports the Google Cloud Translation library + const {TranslationServiceClient} = require('@google-cloud/translate'); + + // Instantiates a client + const translationClient = new TranslationServiceClient(); + + async function getGlossary() { + // Construct request + const request = { + parent: `projects/${projectId}/locations/${location}`, + name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, + }; + + // Get glossary + const [response] = await translationClient.getGlossary(request); + + console.log(`Got glossary: ${response.name}`); + } + + getGlossary(); + // [END translate_v3_get_glossary] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3/translate_get_supported_languages.js b/translate/v3/translate_get_supported_languages.js new file mode 100644 index 0000000000..8204d916a2 --- /dev/null +++ b/translate/v3/translate_get_supported_languages.js @@ -0,0 +1,59 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main(projectId = 'YOUR_PROJECT_ID', location = 'global') { + // [START translate_v3_get_supported_languages] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'global'; + + // Imports the Google Cloud Translation library + const {TranslationServiceClient} = require('@google-cloud/translate'); + + // Instantiates a client + const translationClient = new TranslationServiceClient(); + + async function getSupportedLanguages() { + // Construct request + const request = { + parent: `projects/${projectId}/locations/${location}`, + }; + + // Get supported languages + const [response] = await translationClient.getSupportedLanguages(request); + + for (const language of response.languages) { + // Supported language code, generally consisting of its ISO 639-1 identifier, for + // example, 'en', 'ja'. In certain cases, BCP-47 codes including language and + // region identifiers are returned (for example, 'zh-TW' and 'zh-CN') + console.log(`Language - Language Code: ${language.languageCode}`); + // Human readable name of the language localized in the display language specified + // in the request. + console.log(`Language - Display Name: ${language.displayName}`); + // Can be used as source language. + console.log(`Language - Support Source: ${language.supportSource}`); + // Can be used as target language. + console.log(`Language - Support Target: ${language.supportTarget}`); + } + } + + getSupportedLanguages(); + // [END translate_v3_get_supported_languages] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3/translate_get_supported_languages_for_target.js b/translate/v3/translate_get_supported_languages_for_target.js new file mode 100644 index 0000000000..5ea4b9196c --- /dev/null +++ b/translate/v3/translate_get_supported_languages_for_target.js @@ -0,0 +1,60 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main(projectId = 'YOUR_PROJECT_ID', location = 'global') { + // [START translate_v3_get_supported_languages_for_target] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'global'; + + // Imports the Google Cloud Translation library + const {TranslationServiceClient} = require('@google-cloud/translate'); + + // Instantiates a client + const translationClient = new TranslationServiceClient(); + + async function getSupportedLanguages() { + // Construct request + const request = { + parent: `projects/${projectId}/locations/${location}`, + displayLanguageCode: 'en', + }; + + // Get supported languages + const [response] = await translationClient.getSupportedLanguages(request); + + for (const language of response.languages) { + // Supported language code, generally consisting of its ISO 639-1 identifier, for + // example, 'en', 'ja'. In certain cases, BCP-47 codes including language and + // region identifiers are returned (for example, 'zh-TW' and 'zh-CN') + console.log(`Language - Language Code: ${language.languageCode}`); + // Human readable name of the language localized in the display language specified + // in the request. + console.log(`Language - Display Name: ${language.displayName}`); + // Can be used as source language. + console.log(`Language - Support Source: ${language.supportSource}`); + // Can be used as target language. + console.log(`Language - Support Target: ${language.supportTarget}`); + } + } + + getSupportedLanguages(); + // [END translate_v3_get_supported_languages_for_target] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3/translate_list_codes.js b/translate/v3/translate_list_codes.js new file mode 100644 index 0000000000..37c4c73210 --- /dev/null +++ b/translate/v3/translate_list_codes.js @@ -0,0 +1,50 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main(projectId = 'YOUR_PROJECT_ID', location = 'global') { + // [START translate_v3_list_codes] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'global'; + + // Imports the Google Cloud Translation library + const {TranslationServiceClient} = require('@google-cloud/translate'); + + // Instantiates a client + const translationClient = new TranslationServiceClient(); + + async function listLanguages() { + // Construct request + const request = { + parent: `projects/${projectId}/locations/${location}`, + }; + + // Run request + const [response] = await translationClient.getSupportedLanguages(request); + + console.log(`Supported languages:`); + for (const language of response.languages) { + console.log(`Language Code: ${language.languageCode}`); + } + } + + listLanguages(); + // [END translate_v3_list_codes] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3/translate_list_glossary.js b/translate/v3/translate_list_glossary.js new file mode 100644 index 0000000000..f96eb4a132 --- /dev/null +++ b/translate/v3/translate_list_glossary.js @@ -0,0 +1,54 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main(projectId = 'YOUR_PROJECT_ID', location = 'us-central1') { + // [START translate_v3_list_glossary] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'global'; + + // Imports the Google Cloud Translation library + const {TranslationServiceClient} = require('@google-cloud/translate'); + + // Instantiates a client + const translationClient = new TranslationServiceClient(); + + async function listGlossaries() { + // Construct request + const request = { + parent: `projects/${projectId}/locations/${location}`, + }; + + // Run request + const [response] = await translationClient.listGlossaries(request); + + for (const glossary of response) { + console.log(`Name: ${glossary.name}`); + console.log(`Entry count: ${glossary.entryCount}`); + console.log(`Input uri: ${glossary.inputConfig.gcsSource.inputUri}`); + for (const languageCode of glossary.languageCodesSet.languageCodes) { + console.log(`Language code: ${languageCode}`); + } + } + } + + listGlossaries(); + // [END translate_v3_list_glossary] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3/translate_list_language_names.js b/translate/v3/translate_list_language_names.js new file mode 100644 index 0000000000..92aa9ece02 --- /dev/null +++ b/translate/v3/translate_list_language_names.js @@ -0,0 +1,52 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main(projectId = 'YOUR_PROJECT_ID', location = 'global') { + // [START translate_v3_list_language_names] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'global'; + + // Imports the Google Cloud Translation library + const {TranslationServiceClient} = require('@google-cloud/translate'); + + // Instantiates a client + const translationClient = new TranslationServiceClient(); + + async function listLanguages() { + // Construct request + const request = { + parent: `projects/${projectId}/locations/${location}`, + displayLanguageCode: 'fr', + }; + + // Run request + const [response] = await translationClient.getSupportedLanguages(request); + + console.log(`Supported languages:`); + for (const language of response.languages) { + console.log(`Language Code: ${language.languageCode}`); + console.log(`Display Name: ${language.displayName}`); + } + } + + listLanguages(); + // [END translate_v3_list_language_names] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3/translate_translate_text.js b/translate/v3/translate_translate_text.js new file mode 100644 index 0000000000..51a1a750a4 --- /dev/null +++ b/translate/v3/translate_translate_text.js @@ -0,0 +1,57 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'global', + text = 'text to translate' +) { + // [START translate_v3_translate_text] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'global'; + // const text = 'text to translate'; + + // Imports the Google Cloud Translation library + const {TranslationServiceClient} = require('@google-cloud/translate'); + + // Instantiates a client + const translationClient = new TranslationServiceClient(); + async function translateText() { + // Construct request + const request = { + parent: `projects/${projectId}/locations/${location}`, + contents: [text], + mimeType: 'text/plain', // mime types: text/plain, text/html + sourceLanguageCode: 'en', + targetLanguageCode: 'sr-Latn', + }; + + // Run request + const [response] = await translationClient.translateText(request); + + for (const translation of response.translations) { + console.log(`Translation: ${translation.translatedText}`); + } + } + + translateText(); + // [END translate_v3_translate_text] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3/translate_translate_text_with_glossary.js b/translate/v3/translate_translate_text_with_glossary.js new file mode 100644 index 0000000000..e40c6d4c50 --- /dev/null +++ b/translate/v3/translate_translate_text_with_glossary.js @@ -0,0 +1,63 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + glossaryId = 'glossary', + text = 'text to translate' +) { + // [START translate_v3_translate_text_with_glossary] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'global'; + // const glossaryId = 'YOUR_GLOSSARY_ID'; + // const text = 'text to translate'; + + // Imports the Google Cloud Translation library + const {TranslationServiceClient} = require('@google-cloud/translate'); + + // Instantiates a client + const translationClient = new TranslationServiceClient(); + async function translateTextWithGlossary() { + const glossaryConfig = { + glossary: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, + }; + // Construct request + const request = { + parent: `projects/${projectId}/locations/${location}`, + contents: [text], + mimeType: 'text/plain', // mime types: text/plain, text/html + sourceLanguageCode: 'en', + targetLanguageCode: 'es', + glossaryConfig: glossaryConfig, + }; + + // Run request + const [response] = await translationClient.translateText(request); + + for (const translation of response.glossaryTranslations) { + console.log(`Translation: ${translation.translatedText}`); + } + } + + translateTextWithGlossary(); + // [END translate_v3_translate_text_with_glossary] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3/translate_translate_text_with_glossary_and_model.js b/translate/v3/translate_translate_text_with_glossary_and_model.js new file mode 100644 index 0000000000..a09432ac1e --- /dev/null +++ b/translate/v3/translate_translate_text_with_glossary_and_model.js @@ -0,0 +1,66 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + glossaryId = 'glossary', + modelId = 'YOUR_MODEL_ID', + text = 'text to translate' +) { + // [START translate_v3_translate_text_with_glossary_and_model] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const glossaryId = 'YOUR_GLOSSARY_ID'; + // const modelId = 'YOUR_MODEL_ID'; + // const text = 'text to translate'; + + // Imports the Google Cloud Translation library + const {TranslationServiceClient} = require('@google-cloud/translate'); + + // Instantiates a client + const translationClient = new TranslationServiceClient(); + async function translateTextWithGlossaryAndModel() { + const glossaryConfig = { + glossary: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, + }; + // Construct request + const request = { + parent: `projects/${projectId}/locations/${location}`, + contents: [text], + mimeType: 'text/plain', // mime types: text/plain, text/html + sourceLanguageCode: 'en', + targetLanguageCode: 'ja', + glossaryConfig: glossaryConfig, + model: `projects/${projectId}/locations/${location}/models/${modelId}`, + }; + + // Run request + const [response] = await translationClient.translateText(request); + + for (const translation of response.glossaryTranslations) { + console.log(`Translation: ${translation.translatedText}`); + } + } + + translateTextWithGlossaryAndModel(); + // [END translate_v3_translate_text_with_glossary_and_model] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3/translate_translate_text_with_model.js b/translate/v3/translate_translate_text_with_model.js new file mode 100644 index 0000000000..5a1d71d0b3 --- /dev/null +++ b/translate/v3/translate_translate_text_with_model.js @@ -0,0 +1,60 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + modelId = 'model-id', + text = 'text to translate' +) { + // [START translate_v3_text_with_model] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1'; + // const modelId = 'YOUR_MODEL_ID'; + // const text = 'text to translate'; + + // Imports the Google Cloud Translation library + const {TranslationServiceClient} = require('@google-cloud/translate'); + + // Instantiates a client + const translationClient = new TranslationServiceClient(); + async function translateTextWithModel() { + // Construct request + const request = { + parent: `projects/${projectId}/locations/${location}`, + contents: [text], + mimeType: 'text/plain', // mime types: text/plain, text/html + sourceLanguageCode: 'en', + targetLanguageCode: 'ja', + model: `projects/${projectId}/locations/${location}/models/${modelId}`, + }; + + // Run request + const [response] = await translationClient.translateText(request); + + for (const translation of response.translations) { + console.log(`Translated Content: ${translation.translatedText}`); + } + } + + translateTextWithModel(); + // [END translate_v3_text_with_model] +} + +main(...process.argv.slice(2));