-
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.
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.
- Loading branch information
Showing
39 changed files
with
2,256 additions
and
78 deletions.
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
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
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
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
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 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())); | ||
} | ||
}); | ||
}); |
110 changes: 110 additions & 0 deletions
110
translate/test/v3/translate_batch_translate_text_with_glossary.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,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(); | ||
}); | ||
}); |
111 changes: 111 additions & 0 deletions
111
translate/test/v3/translate_batch_translate_text_with_glossary_and_model.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,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(); | ||
}); | ||
}); |
Oops, something went wrong.