Skip to content

Commit c616c13

Browse files
bcoeAce Nassri
authored and
Ace Nassri
committed
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.
1 parent 27fbe30 commit c616c13

39 files changed

+2256
-78
lines changed

translate/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"node": ">=8"
1212
},
1313
"scripts": {
14-
"test": "mocha --recursive --timeout 90000"
14+
"test": "mocha --recursive --timeout 150000"
1515
},
1616
"dependencies": {
1717
"@google-cloud/automl": "^1.0.0",

translate/quickstart.js

+21-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2017, Google, Inc.
2+
* Copyright 2017 Google LLC
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -15,28 +15,36 @@
1515

1616
'use strict';
1717

18-
async function main(
18+
function main(
1919
projectId = 'YOUR_PROJECT_ID' // Your GCP Project Id
2020
) {
2121
// [START translate_quickstart]
22+
/**
23+
* TODO(developer): Uncomment the following line before running the sample.
24+
*/
25+
// const projectId = 'YOUR_PROJECT_ID';
26+
2227
// Imports the Google Cloud client library
23-
const {Translate} = require('@google-cloud/translate');
28+
const {Translate} = require('@google-cloud/translate').v2;
2429

2530
// Instantiates a client
2631
const translate = new Translate({projectId});
2732

28-
// The text to translate
29-
const text = 'Hello, world!';
33+
async function quickStart() {
34+
// The text to translate
35+
const text = 'Hello, world!';
36+
37+
// The target language
38+
const target = 'ru';
3039

31-
// The target language
32-
const target = 'ru';
40+
// Translates some text into Russian
41+
const [translation] = await translate.translate(text, target);
42+
console.log(`Text: ${text}`);
43+
console.log(`Translation: ${translation}`);
44+
}
3345

34-
// Translates some text into Russian
35-
const [translation] = await translate.translate(text, target);
36-
console.log(`Text: ${text}`);
37-
console.log(`Translation: ${translation}`);
46+
quickStart();
3847
// [END translate_quickstart]
3948
}
4049

41-
const args = process.argv.slice(2);
42-
main(...args).catch(console.error);
50+
main(...process.argv.slice(2));

translate/test/quickstart.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2017, Google, Inc.
2+
* Copyright 2017 Google LLC
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at

translate/test/translate.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2017, Google, Inc.
2+
* Copyright 2017 Google LLC
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -16,7 +16,7 @@
1616
'use strict';
1717

1818
const {assert} = require('chai');
19-
const {Translate} = require('@google-cloud/translate');
19+
const {Translate} = require('@google-cloud/translate').v2;
2020
const cp = require('child_process');
2121

2222
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}).trim();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {TranslationServiceClient} = require('@google-cloud/translate');
19+
const {Storage} = require('@google-cloud/storage');
20+
const cp = require('child_process');
21+
const uuid = require('uuid');
22+
23+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
24+
25+
const REGION_TAG = 'translate_batch_translate_text';
26+
27+
describe(REGION_TAG, () => {
28+
const translationClient = new TranslationServiceClient();
29+
const location = 'us-central1';
30+
const bucketUuid = uuid.v4();
31+
const bucketName = `translation-${bucketUuid}/BATCH_TRANSLATION_OUTPUT/`;
32+
const storage = new Storage();
33+
34+
before(async () => {
35+
const projectId = await translationClient.getProjectId();
36+
37+
//Create bucket if needed
38+
await storage
39+
.createBucket(projectId, {
40+
location: 'US',
41+
storageClass: 'COLDLINE',
42+
})
43+
.catch(error => {
44+
if (error.code !== 409) {
45+
//if it's not a duplicate bucket error, let the user know
46+
console.error(error);
47+
}
48+
});
49+
});
50+
51+
it('should batch translate the input text', async () => {
52+
const projectId = await translationClient.getProjectId();
53+
const inputUri = `gs://cloud-samples-data/translation/text.txt`;
54+
55+
const outputUri = `gs://${projectId}/${bucketName}`;
56+
const output = execSync(
57+
`node v3/${REGION_TAG}.js ${projectId} ${location} ${inputUri} ${outputUri}`
58+
);
59+
assert.match(output, /Total Characters: 13/);
60+
assert.match(output, /Translated Characters: 13/);
61+
});
62+
63+
// Delete the folder from GCS for cleanup
64+
after(async function() {
65+
const projectId = await translationClient.getProjectId();
66+
const options = {
67+
prefix: `translation-${bucketUuid}`,
68+
};
69+
70+
const bucket = await storage.bucket(projectId);
71+
const [files] = await bucket.getFiles(options);
72+
const length = files.length;
73+
if (length > 0) {
74+
await Promise.all(files.map(file => file.delete()));
75+
}
76+
});
77+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* Copyright 2019 Google LLC
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
const {assert} = require('chai');
19+
const {TranslationServiceClient} = require('@google-cloud/translate');
20+
const {Storage} = require('@google-cloud/storage');
21+
const cp = require('child_process');
22+
const uuid = require('uuid');
23+
24+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
25+
26+
const REGION_TAG = 'translate_batch_translate_text_with_glossary';
27+
28+
describe(REGION_TAG, () => {
29+
const translationClient = new TranslationServiceClient();
30+
const location = 'us-central1';
31+
const glossaryId = 'my-fake_glossary';
32+
const bucketUuid = uuid.v4();
33+
const bucketName = `translation-${bucketUuid}/BATCH_TRANSLATION_OUTPUT/`;
34+
const storage = new Storage();
35+
36+
before(async () => {
37+
const projectId = await translationClient.getProjectId();
38+
39+
//Create bucket if needed
40+
await storage
41+
.createBucket(projectId, {
42+
location: 'US',
43+
storageClass: 'COLDLINE',
44+
})
45+
.catch(error => {
46+
if (error.code !== 409) {
47+
//if it's not a duplicate bucket error, let the user know
48+
console.error(error);
49+
}
50+
});
51+
52+
// Create glossary
53+
const request = {
54+
parent: `projects/${projectId}/locations/${location}`,
55+
glossary: {
56+
languageCodesSet: {
57+
languageCodes: ['en', 'es'],
58+
},
59+
inputConfig: {
60+
gcsSource: {
61+
inputUri: 'gs://cloud-samples-data/translation/glossary.csv',
62+
},
63+
},
64+
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
65+
},
66+
};
67+
68+
// Create glossary using a long-running operation.
69+
const [operation] = await translationClient.createGlossary(request);
70+
// Wait for operation to complete.
71+
await operation.promise();
72+
});
73+
74+
it('should batch translate the input text with a glossary', async () => {
75+
const projectId = await translationClient.getProjectId();
76+
const inputUri = `gs://cloud-samples-data/translation/text.txt`;
77+
78+
const outputUri = `gs://${projectId}/${bucketName}`;
79+
const output = execSync(
80+
`node v3/${REGION_TAG}.js ${projectId} ${location} ${inputUri} ${outputUri} ${glossaryId}`
81+
);
82+
assert.match(output, /Total Characters: 13/);
83+
assert.match(output, /Translated Characters: 13/);
84+
});
85+
86+
// Delete the folder from GCS for cleanup
87+
after(async function() {
88+
const projectId = await translationClient.getProjectId();
89+
const options = {
90+
prefix: `translation-${bucketUuid}`,
91+
};
92+
93+
const bucket = await storage.bucket(projectId);
94+
const [files] = await bucket.getFiles(options);
95+
const length = files.length;
96+
if (length > 0) {
97+
await Promise.all(files.map(file => file.delete()));
98+
}
99+
100+
// Delete the Glossary
101+
const request = {
102+
parent: `projects/${projectId}/locations/${location}`,
103+
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
104+
};
105+
// Delete glossary using a long-running operation.
106+
const [operation] = await translationClient.deleteGlossary(request);
107+
// Wait for operation to complete.
108+
await operation.promise();
109+
});
110+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* Copyright 2019 Google LLC
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
const {assert} = require('chai');
19+
const {TranslationServiceClient} = require('@google-cloud/translate');
20+
const {Storage} = require('@google-cloud/storage');
21+
const cp = require('child_process');
22+
const uuid = require('uuid');
23+
24+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
25+
26+
const REGION_TAG = 'translate_batch_translate_text_with_glossary_and_model';
27+
28+
describe(REGION_TAG, () => {
29+
const translationClient = new TranslationServiceClient();
30+
const location = 'us-central1';
31+
const glossaryId = 'my-fake_glossary';
32+
const modelId = 'TRL1218052175389786112';
33+
const bucketUuid = uuid.v4();
34+
const bucketName = `translation-${bucketUuid}/BATCH_TRANSLATION_OUTPUT/`;
35+
const storage = new Storage();
36+
37+
before(async () => {
38+
const projectId = await translationClient.getProjectId();
39+
40+
//Create bucket if needed
41+
await storage
42+
.createBucket(projectId, {
43+
location: 'US',
44+
storageClass: 'COLDLINE',
45+
})
46+
.catch(error => {
47+
if (error.code !== 409) {
48+
//if it's not a duplicate bucket error, let the user know
49+
console.error(error);
50+
}
51+
});
52+
53+
// Create glossary
54+
const request = {
55+
parent: `projects/${projectId}/locations/${location}`,
56+
glossary: {
57+
languageCodesSet: {
58+
languageCodes: ['en', 'ja'],
59+
},
60+
inputConfig: {
61+
gcsSource: {
62+
inputUri: 'gs://cloud-samples-data/translation/glossary_ja.csv',
63+
},
64+
},
65+
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
66+
},
67+
};
68+
69+
// Create glossary using a long-running operation.
70+
const [operation] = await translationClient.createGlossary(request);
71+
// Wait for operation to complete.
72+
await operation.promise();
73+
});
74+
75+
it('should batch translate the input text with a glossary', async () => {
76+
const projectId = await translationClient.getProjectId();
77+
const inputUri = `gs://cloud-samples-data/translation/text_with_custom_model_and_glossary.txt`;
78+
79+
const outputUri = `gs://${projectId}/${bucketName}`;
80+
const output = execSync(
81+
`node v3/${REGION_TAG}.js ${projectId} ${location} ${inputUri} ${outputUri} ${glossaryId} ${modelId}`
82+
);
83+
assert.match(output, /Total Characters: 25/);
84+
assert.match(output, /Translated Characters: 25/);
85+
});
86+
87+
// Delete the folder from GCS for cleanup
88+
after(async function() {
89+
const projectId = await translationClient.getProjectId();
90+
const options = {
91+
prefix: `translation-${bucketUuid}`,
92+
};
93+
94+
const bucket = await storage.bucket(projectId);
95+
const [files] = await bucket.getFiles(options);
96+
const length = files.length;
97+
if (length > 0) {
98+
await Promise.all(files.map(file => file.delete()));
99+
}
100+
101+
// Delete the Glossary
102+
const request = {
103+
parent: `projects/${projectId}/locations/${location}`,
104+
name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
105+
};
106+
// Delete glossary using a long-running operation.
107+
const [operation] = await translationClient.deleteGlossary(request);
108+
// Wait for operation to complete.
109+
await operation.promise();
110+
});
111+
});

0 commit comments

Comments
 (0)