From c265b115a5fb37fc45becde9614078d17e33afa6 Mon Sep 17 00:00:00 2001 From: "Leah E. Cole" <6719667+leahecole@users.noreply.github.com> Date: Wed, 3 Apr 2019 14:54:28 -0700 Subject: [PATCH] docs(samples): Add samples for translate v3 beta (#234) --- translate/package.json | 3 +- ...ranslate_batch_translate_text_beta.test.js | 77 ++++++++++++++++ .../translate_create_glossary_beta.test.js | 63 +++++++++++++ .../translate_delete_glossary_beta.test.js | 68 ++++++++++++++ .../translate_detect_language_beta.test.js | 37 ++++++++ .../translate_get_glossary_beta.test.js | 88 ++++++++++++++++++ .../v3beta1/translate_list_codes_beta.test.js | 33 +++++++ .../translate_list_glossary_beta.test.js | 90 +++++++++++++++++++ ...translate_list_language_names_beta.test.js | 35 ++++++++ .../translate_translate_text_beta.test.js | 36 ++++++++ ..._translate_text_with_glossary_beta.test.js | 88 ++++++++++++++++++ .../translate_batch_translate_text_beta.js | 73 +++++++++++++++ .../v3beta1/translate_create_glossary_beta.js | 71 +++++++++++++++ .../v3beta1/translate_delete_glossary_beta.js | 62 +++++++++++++ .../v3beta1/translate_detect_language_beta.js | 57 ++++++++++++ .../v3beta1/translate_get_glossary_beta.js | 58 ++++++++++++ .../v3beta1/translate_list_codes_beta.js | 51 +++++++++++ .../v3beta1/translate_list_glossary_beta.js | 55 ++++++++++++ .../translate_list_language_names_beta.js | 53 +++++++++++ .../v3beta1/translate_translate_text_beta.js | 58 ++++++++++++ ...slate_translate_text_with_glossary_beta.js | 67 ++++++++++++++ 21 files changed, 1222 insertions(+), 1 deletion(-) create mode 100644 translate/system-test/v3beta1/translate_batch_translate_text_beta.test.js create mode 100644 translate/system-test/v3beta1/translate_create_glossary_beta.test.js create mode 100644 translate/system-test/v3beta1/translate_delete_glossary_beta.test.js create mode 100644 translate/system-test/v3beta1/translate_detect_language_beta.test.js create mode 100644 translate/system-test/v3beta1/translate_get_glossary_beta.test.js create mode 100644 translate/system-test/v3beta1/translate_list_codes_beta.test.js create mode 100644 translate/system-test/v3beta1/translate_list_glossary_beta.test.js create mode 100644 translate/system-test/v3beta1/translate_list_language_names_beta.test.js create mode 100644 translate/system-test/v3beta1/translate_translate_text_beta.test.js create mode 100644 translate/system-test/v3beta1/translate_translate_text_with_glossary_beta.test.js create mode 100644 translate/v3beta1/translate_batch_translate_text_beta.js create mode 100644 translate/v3beta1/translate_create_glossary_beta.js create mode 100644 translate/v3beta1/translate_delete_glossary_beta.js create mode 100644 translate/v3beta1/translate_detect_language_beta.js create mode 100644 translate/v3beta1/translate_get_glossary_beta.js create mode 100644 translate/v3beta1/translate_list_codes_beta.js create mode 100644 translate/v3beta1/translate_list_glossary_beta.js create mode 100644 translate/v3beta1/translate_list_language_names_beta.js create mode 100644 translate/v3beta1/translate_translate_text_beta.js create mode 100644 translate/v3beta1/translate_translate_text_with_glossary_beta.js diff --git a/translate/package.json b/translate/package.json index d6290d50f31..b390ebcc5f4 100644 --- a/translate/package.json +++ b/translate/package.json @@ -8,7 +8,7 @@ "node": ">=8" }, "scripts": { - "test": "mocha system-test" + "test": "mocha system-test --recursive --timeout 90000" }, "dependencies": { "@google-cloud/translate": "^3.0.0", @@ -18,6 +18,7 @@ "devDependencies": { "chai": "^4.2.0", "execa": "^1.0.0", + "@google-cloud/storage": "^2.4.3", "mocha": "^6.0.0", "uuid": "^3.3.2" } diff --git a/translate/system-test/v3beta1/translate_batch_translate_text_beta.test.js b/translate/system-test/v3beta1/translate_batch_translate_text_beta.test.js new file mode 100644 index 00000000000..de545c0b427 --- /dev/null +++ b/translate/system-test/v3beta1/translate_batch_translate_text_beta.test.js @@ -0,0 +1,77 @@ +/** + * Copyright 2019, Google, Inc. + * 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').v3beta1; +const {Storage} = require('@google-cloud/storage'); +const execa = require('execa'); +const uuid = require('uuid'); +const exec = async cmd => (await execa.shell(cmd)).stdout; + +const REGION_TAG = 'translate_batch_translate_text_beta'; + +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 = await exec( + `node v3beta1/${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/system-test/v3beta1/translate_create_glossary_beta.test.js b/translate/system-test/v3beta1/translate_create_glossary_beta.test.js new file mode 100644 index 00000000000..d8008760550 --- /dev/null +++ b/translate/system-test/v3beta1/translate_create_glossary_beta.test.js @@ -0,0 +1,63 @@ +/** + * Copyright 2019, Google, Inc. + * 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').v3beta1; +const execa = require('execa'); +const exec = async cmd => (await execa.shell(cmd)).stdout; + +const REGION_TAG = 'translate_create_glossary_beta'; + +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 = await exec( + `node v3beta1/${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 name = translationClient.glossaryPath( + projectId, + location, + glossaryId + ); + const request = { + parent: translationClient.locationPath(projectId, location), + name: name, + }; + + // 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/system-test/v3beta1/translate_delete_glossary_beta.test.js b/translate/system-test/v3beta1/translate_delete_glossary_beta.test.js new file mode 100644 index 00000000000..82c93eca0ed --- /dev/null +++ b/translate/system-test/v3beta1/translate_delete_glossary_beta.test.js @@ -0,0 +1,68 @@ +/** + * Copyright 2019, Google, Inc. + * 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').v3beta1; +const execa = require('execa'); +const exec = async cmd => (await execa.shell(cmd)).stdout; + +const REGION_TAG = 'translate_delete_glossary_beta'; + +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: translationClient.glossaryPath(projectId, location, glossaryId), + }; + + // Construct request + const request = { + parent: translationClient.locationPath(projectId, 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 = await exec( + `node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${glossaryId}` + ); + assert.match(output, /glossary/); + }); +}); diff --git a/translate/system-test/v3beta1/translate_detect_language_beta.test.js b/translate/system-test/v3beta1/translate_detect_language_beta.test.js new file mode 100644 index 00000000000..34a8adb4ff7 --- /dev/null +++ b/translate/system-test/v3beta1/translate_detect_language_beta.test.js @@ -0,0 +1,37 @@ +/** + * Copyright 2019, Google, Inc. + * 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').v3beta1; +const execa = require('execa'); +const exec = async cmd => (await execa.shell(cmd)).stdout; + +const REGION_TAG = 'translate_detect_language_beta'; + +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 = await exec( + `node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${text}` + ); + assert.match(output, /Language Code: is/); + assert.match(output, /Confidence: 1/); + }); +}); diff --git a/translate/system-test/v3beta1/translate_get_glossary_beta.test.js b/translate/system-test/v3beta1/translate_get_glossary_beta.test.js new file mode 100644 index 00000000000..decc19e4473 --- /dev/null +++ b/translate/system-test/v3beta1/translate_get_glossary_beta.test.js @@ -0,0 +1,88 @@ +/** + * Copyright 2019, Google, Inc. + * 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').v3beta1; +const execa = require('execa'); +const exec = async cmd => (await execa.shell(cmd)).stdout; + +const REGION_TAG = 'translate_get_glossary_beta'; + +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: translationClient.glossaryPath(projectId, location, glossaryId), + }; + + // Construct request + const request = { + parent: translationClient.locationPath(projectId, 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 = await exec( + `node v3beta1/${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 name = translationClient.glossaryPath( + projectId, + location, + glossaryId + ); + const request = { + parent: translationClient.locationPath(projectId, location), + name: name, + }; + + // 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/system-test/v3beta1/translate_list_codes_beta.test.js b/translate/system-test/v3beta1/translate_list_codes_beta.test.js new file mode 100644 index 00000000000..dab4f2cc69c --- /dev/null +++ b/translate/system-test/v3beta1/translate_list_codes_beta.test.js @@ -0,0 +1,33 @@ +/** + * Copyright 2019, Google, Inc. + * 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').v3beta1; +const execa = require('execa'); +const exec = async cmd => (await execa.shell(cmd)).stdout; + +const REGION_TAG = 'translate_list_codes_beta'; + +describe(REGION_TAG, () => { + it('should list available language codes', async () => { + const translationClient = new TranslationServiceClient(); + const projectId = await translationClient.getProjectId(); + const output = await exec(`node v3beta1/${REGION_TAG}.js ${projectId}`); + assert.match(output, /Language Code: en/); + assert.match(output, /Language Code: fr/); + }); +}); diff --git a/translate/system-test/v3beta1/translate_list_glossary_beta.test.js b/translate/system-test/v3beta1/translate_list_glossary_beta.test.js new file mode 100644 index 00000000000..f3d738e4220 --- /dev/null +++ b/translate/system-test/v3beta1/translate_list_glossary_beta.test.js @@ -0,0 +1,90 @@ +/** + * Copyright 2019, Google, Inc. + * 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').v3beta1; +const execa = require('execa'); +const exec = async cmd => (await execa.shell(cmd)).stdout; + +const REGION_TAG = 'translate_list_glossary_beta'; + +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: translationClient.glossaryPath(projectId, location, glossaryId), + }; + + // Construct request + const request = { + parent: translationClient.locationPath(projectId, 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 = await exec( + `node v3beta1/${REGION_TAG}.js ${projectId} ${location}` + ); + assert.match( + output, + /gs:\/\/cloud-samples-data\/translation\/glossary.csv/ + ); + }); + + after(async function() { + const projectId = await translationClient.getProjectId(); + const name = translationClient.glossaryPath( + projectId, + location, + glossaryId + ); + const request = { + parent: translationClient.locationPath(projectId, location), + name: name, + }; + + // 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/system-test/v3beta1/translate_list_language_names_beta.test.js b/translate/system-test/v3beta1/translate_list_language_names_beta.test.js new file mode 100644 index 00000000000..b81d6ee87b8 --- /dev/null +++ b/translate/system-test/v3beta1/translate_list_language_names_beta.test.js @@ -0,0 +1,35 @@ +/** + * Copyright 2019, Google, Inc. + * 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').v3beta1; +const execa = require('execa'); +const exec = async cmd => (await execa.shell(cmd)).stdout; + +const REGION_TAG = 'translate_list_language_names_beta'; + +describe(REGION_TAG, () => { + it('should list available language names', async () => { + const translationClient = new TranslationServiceClient(); + const projectId = await translationClient.getProjectId(); + const output = await exec(`node v3beta1/${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/system-test/v3beta1/translate_translate_text_beta.test.js b/translate/system-test/v3beta1/translate_translate_text_beta.test.js new file mode 100644 index 00000000000..3b5b3ece817 --- /dev/null +++ b/translate/system-test/v3beta1/translate_translate_text_beta.test.js @@ -0,0 +1,36 @@ +/** + * Copyright 2019, Google, Inc. + * 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').v3beta1; +const execa = require('execa'); +const exec = async cmd => (await execa.shell(cmd)).stdout; + +const REGION_TAG = 'translate_translate_text_beta'; + +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 = await exec( + `node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${text}` + ); + assert.match(output, /Translation: Zdravo svet/); + }); +}); diff --git a/translate/system-test/v3beta1/translate_translate_text_with_glossary_beta.test.js b/translate/system-test/v3beta1/translate_translate_text_with_glossary_beta.test.js new file mode 100644 index 00000000000..451932a4f62 --- /dev/null +++ b/translate/system-test/v3beta1/translate_translate_text_with_glossary_beta.test.js @@ -0,0 +1,88 @@ +/** + * Copyright 2019, Google, Inc. + * 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').v3beta1; +const execa = require('execa'); +const exec = async cmd => (await execa.shell(cmd)).stdout; + +const REGION_TAG = 'translate_translate_text_with_glossary_beta'; + +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: translationClient.glossaryPath(projectId, location, glossaryId), + }; + + // Construct request + const request = { + parent: translationClient.locationPath(projectId, 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 = await exec( + `node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${glossaryId} ${input}` + ); + assert.match(output, /direcciones/); + }); + + after(async function() { + const projectId = await translationClient.getProjectId(); + const name = translationClient.glossaryPath( + projectId, + location, + glossaryId + ); + const request = { + parent: translationClient.locationPath(projectId, location), + name: name, + }; + + // 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/v3beta1/translate_batch_translate_text_beta.js b/translate/v3beta1/translate_batch_translate_text_beta.js new file mode 100644 index 00000000000..e918332ed51 --- /dev/null +++ b/translate/v3beta1/translate_batch_translate_text_beta.js @@ -0,0 +1,73 @@ +/** + * Copyright 2019, Google, Inc. + * 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'; + +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_batch_translate_text_beta] + /** + * 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').v3beta1; + + // Instantiates a client + const translationClient = new TranslationServiceClient(); + async function batchTranslateText() { + // Construct request + const request = { + parent: translationClient.locationPath(projectId, location), + sourceLanguageCode: 'en-US', + targetLanguageCodes: ['sr-Latn'], + 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_batch_translate_text_beta] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3beta1/translate_create_glossary_beta.js b/translate/v3beta1/translate_create_glossary_beta.js new file mode 100644 index 00000000000..9c109104d63 --- /dev/null +++ b/translate/v3beta1/translate_create_glossary_beta.js @@ -0,0 +1,71 @@ +/** + * Copyright 2019, Google, Inc. + * 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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + glossaryId = 'glossary-id' +) { + // [START translate_create_glossary_beta] + /** + * 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').v3beta1; + + // 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: translationClient.glossaryPath(projectId, location, glossaryId), + }; + + // Construct request + const request = { + parent: translationClient.locationPath(projectId, 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_create_glossary_beta] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3beta1/translate_delete_glossary_beta.js b/translate/v3beta1/translate_delete_glossary_beta.js new file mode 100644 index 00000000000..93f7bf87375 --- /dev/null +++ b/translate/v3beta1/translate_delete_glossary_beta.js @@ -0,0 +1,62 @@ +/** + * Copyright 2019, Google, Inc. + * 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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + glossaryId = 'glossary-id' +) { + // [START translate_delete_glossary_beta] + /** + * 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').v3beta1; + + // Instantiates a client + const translationClient = new TranslationServiceClient(); + + async function deleteGlossary() { + // Construct request + const name = translationClient.glossaryPath( + projectId, + location, + glossaryId + ); + const request = { + parent: translationClient.locationPath(projectId, location), + name: name, + }; + + // 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_delete_glossary_beta] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3beta1/translate_detect_language_beta.js b/translate/v3beta1/translate_detect_language_beta.js new file mode 100644 index 00000000000..f437a7bbc13 --- /dev/null +++ b/translate/v3beta1/translate_detect_language_beta.js @@ -0,0 +1,57 @@ +/** + * Copyright 2019, Google, Inc. + * 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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'global', + text = 'text to translate' +) { + // [START translate_detect_language_beta] + /** + * 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').v3beta1; + + // Instantiates a client + const translationClient = new TranslationServiceClient(); + + async function detectLanguage() { + // Construct request + const request = { + parent: translationClient.locationPath(projectId, 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_detect_language_beta] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3beta1/translate_get_glossary_beta.js b/translate/v3beta1/translate_get_glossary_beta.js new file mode 100644 index 00000000000..0f0c77a31bc --- /dev/null +++ b/translate/v3beta1/translate_get_glossary_beta.js @@ -0,0 +1,58 @@ +/** + * Copyright 2019, Google, Inc. + * 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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + glossaryId = 'glossary-id' +) { + // [START translate_get_glossary_beta] + /** + * 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').v3beta1; + + // Instantiates a client + const translationClient = new TranslationServiceClient(); + + async function getGlossary() { + // Construct request + const name = translationClient.glossaryPath( + projectId, + location, + glossaryId + ); + const request = { + parent: translationClient.locationPath(projectId, location), + name: name, + }; + + // Get glossary + const [response] = await translationClient.getGlossary(request); + + console.log(`Got glossary: ${response.name}`); + } + + getGlossary(); + // [END translate_get_glossary_beta] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3beta1/translate_list_codes_beta.js b/translate/v3beta1/translate_list_codes_beta.js new file mode 100644 index 00000000000..5992040aa79 --- /dev/null +++ b/translate/v3beta1/translate_list_codes_beta.js @@ -0,0 +1,51 @@ +/** + * Copyright 2019, Google, Inc. + * 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'; + +function main(projectId = 'YOUR_PROJECT_ID', location = 'global') { + // [START translate_list_codes_beta] + /** + * 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').v3beta1; + + // Instantiates a client + const translationClient = new TranslationServiceClient(); + + async function listLanguages() { + // Construct request + const request = { + parent: translationClient.locationPath(projectId, 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_list_codes_beta] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3beta1/translate_list_glossary_beta.js b/translate/v3beta1/translate_list_glossary_beta.js new file mode 100644 index 00000000000..60410858b4e --- /dev/null +++ b/translate/v3beta1/translate_list_glossary_beta.js @@ -0,0 +1,55 @@ +/** + * Copyright 2019, Google, Inc. + * 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'; + +function main(projectId = 'YOUR_PROJECT_ID', location = 'us-central1') { + // [START translate_list_glossary_beta] + /** + * 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').v3beta1; + + // Instantiates a client + const translationClient = new TranslationServiceClient(); + + async function listGlossaries() { + // Construct request + const request = { + parent: translationClient.locationPath(projectId, 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_list_glossary_beta] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3beta1/translate_list_language_names_beta.js b/translate/v3beta1/translate_list_language_names_beta.js new file mode 100644 index 00000000000..4bdfeb2d037 --- /dev/null +++ b/translate/v3beta1/translate_list_language_names_beta.js @@ -0,0 +1,53 @@ +/** + * Copyright 2019, Google, Inc. + * 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'; + +function main(projectId = 'YOUR_PROJECT_ID', location = 'global') { + // [START translate_list_language_names_beta] + /** + * 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').v3beta1; + + // Instantiates a client + const translationClient = new TranslationServiceClient(); + + async function listLanguages() { + // Construct request + const request = { + parent: translationClient.locationPath(projectId, 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_list_language_names_beta] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3beta1/translate_translate_text_beta.js b/translate/v3beta1/translate_translate_text_beta.js new file mode 100644 index 00000000000..d3a31963bf6 --- /dev/null +++ b/translate/v3beta1/translate_translate_text_beta.js @@ -0,0 +1,58 @@ +/** + * Copyright 2019, Google, Inc. + * 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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'global', + text = 'text to translate' +) { + // [START translate_translate_text_beta] + /** + * 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').v3beta1; + + // Instantiates a client + const translationClient = new TranslationServiceClient(); + async function translateText() { + // Construct request + const request = { + parent: translationClient.locationPath(projectId, location), + contents: [text], + mimeType: 'text/plain', // mime types: text/plain, text/html + sourceLanguageCode: 'en-US', + 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_translate_text_beta] +} + +main(...process.argv.slice(2)); diff --git a/translate/v3beta1/translate_translate_text_with_glossary_beta.js b/translate/v3beta1/translate_translate_text_with_glossary_beta.js new file mode 100644 index 00000000000..51bbbf1e285 --- /dev/null +++ b/translate/v3beta1/translate_translate_text_with_glossary_beta.js @@ -0,0 +1,67 @@ +/** + * Copyright 2019, Google, Inc. + * 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'; + +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'us-central1', + glossaryId = 'glossary', + text = 'text to translate' +) { + // [START translate_text_with_glossary_beta] + /** + * 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').v3beta1; + + // Instantiates a client + const translationClient = new TranslationServiceClient(); + async function translateTextWithGlossary() { + const glossary = translationClient.glossaryPath( + projectId, + location, + glossaryId + ); + const glossaryConfig = { + glossary: glossary, + }; + // Construct request + const request = { + parent: translationClient.locationPath(projectId, location), + contents: [text], + mimeType: 'text/plain', // mime types: text/plain, text/html + sourceLanguageCode: 'en-US', + targetLanguageCode: 'es', + glossaryConfig: glossaryConfig, + }; + + // Run request + const [response] = await translationClient.translateText(request); + + for (const translation of response.translations) { + console.log(`Translation: ${translation.translatedText}`); + } + } + + translateTextWithGlossary(); + // [END translate_text_with_glossary_beta] +} + +main(...process.argv.slice(2));