Skip to content

Commit

Permalink
feat!: v3 is now the default API surface (#355)
Browse files Browse the repository at this point in the history
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
bcoe authored and Ace Nassri committed Nov 17, 2022
1 parent 3b54382 commit c86b9e7
Show file tree
Hide file tree
Showing 39 changed files with 2,256 additions and 78 deletions.
2 changes: 1 addition & 1 deletion translate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"node": ">=8"
},
"scripts": {
"test": "mocha --recursive --timeout 90000"
"test": "mocha --recursive --timeout 150000"
},
"dependencies": {
"@google-cloud/automl": "^1.0.0",
Expand Down
34 changes: 21 additions & 13 deletions translate/quickstart.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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));
2 changes: 1 addition & 1 deletion translate/test/quickstart.test.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions translate/test/translate.test.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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();
Expand Down
77 changes: 77 additions & 0 deletions translate/test/v3/translate_batch_translate_text.test.js
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 translate/test/v3/translate_batch_translate_text_with_glossary.test.js
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();
});
});
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();
});
});
Loading

0 comments on commit c86b9e7

Please sign in to comment.