-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(samples): Add samples for translate v3 beta (#234)
- Loading branch information
Showing
21 changed files
with
1,222 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
translate/system-test/v3beta1/translate_batch_translate_text_beta.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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())); | ||
} | ||
}); | ||
}); |
63 changes: 63 additions & 0 deletions
63
translate/system-test/v3beta1/translate_create_glossary_beta.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}); |
68 changes: 68 additions & 0 deletions
68
translate/system-test/v3beta1/translate_delete_glossary_beta.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/); | ||
}); | ||
}); |
37 changes: 37 additions & 0 deletions
37
translate/system-test/v3beta1/translate_detect_language_beta.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/); | ||
}); | ||
}); |
88 changes: 88 additions & 0 deletions
88
translate/system-test/v3beta1/translate_get_glossary_beta.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}); |
33 changes: 33 additions & 0 deletions
33
translate/system-test/v3beta1/translate_list_codes_beta.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/); | ||
}); | ||
}); |
Oops, something went wrong.