Skip to content

Commit a128c50

Browse files
leahecoleAce Nassri
authored andcommitted
docs(samples): Add samples for translate v3 beta (#234)
1 parent bf6be46 commit a128c50

21 files changed

+1222
-1
lines changed

translate/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"node": ">=8"
99
},
1010
"scripts": {
11-
"test": "mocha system-test"
11+
"test": "mocha system-test --recursive --timeout 90000"
1212
},
1313
"dependencies": {
1414
"@google-cloud/translate": "^3.0.0",
@@ -18,6 +18,7 @@
1818
"devDependencies": {
1919
"chai": "^4.2.0",
2020
"execa": "^1.0.0",
21+
"@google-cloud/storage": "^2.4.3",
2122
"mocha": "^6.0.0",
2223
"uuid": "^3.3.2"
2324
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* Copyright 2019, Google, Inc.
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').v3beta1;
20+
const {Storage} = require('@google-cloud/storage');
21+
const execa = require('execa');
22+
const uuid = require('uuid');
23+
const exec = async cmd => (await execa.shell(cmd)).stdout;
24+
25+
const REGION_TAG = 'translate_batch_translate_text_beta';
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 = await exec(
57+
`node v3beta1/${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+
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Copyright 2019, Google, Inc.
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').v3beta1;
20+
const execa = require('execa');
21+
const exec = async cmd => (await execa.shell(cmd)).stdout;
22+
23+
const REGION_TAG = 'translate_create_glossary_beta';
24+
25+
describe(REGION_TAG, () => {
26+
const translationClient = new TranslationServiceClient();
27+
28+
it('should create a glossary', async function() {
29+
const projectId = await translationClient.getProjectId();
30+
const location = 'us-central1';
31+
const glossaryId = 'test-glossary';
32+
const output = await exec(
33+
`node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${glossaryId}`
34+
);
35+
assert.match(
36+
output,
37+
/gs:\/\/cloud-samples-data\/translation\/glossary.csv/
38+
);
39+
});
40+
41+
after('cleanup for glossary create', async function() {
42+
const projectId = await translationClient.getProjectId();
43+
const location = 'us-central1';
44+
const glossaryId = 'test-glossary';
45+
// Delete the glossary to clean up
46+
const name = translationClient.glossaryPath(
47+
projectId,
48+
location,
49+
glossaryId
50+
);
51+
const request = {
52+
parent: translationClient.locationPath(projectId, location),
53+
name: name,
54+
};
55+
56+
// Delete glossary using a long-running operation.
57+
// You can wait for now, or get results later.
58+
const [operation] = await translationClient.deleteGlossary(request);
59+
60+
// Wait for operation to complete.
61+
await operation.promise();
62+
});
63+
});
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Copyright 2019, Google, Inc.
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').v3beta1;
20+
const execa = require('execa');
21+
const exec = async cmd => (await execa.shell(cmd)).stdout;
22+
23+
const REGION_TAG = 'translate_delete_glossary_beta';
24+
25+
describe(REGION_TAG, () => {
26+
const translationClient = new TranslationServiceClient();
27+
const location = 'us-central1';
28+
const glossaryId = 'glossary';
29+
30+
before(async function() {
31+
// Add a glossary to be deleted
32+
// const translationClient = new TranslationServiceClient();
33+
const projectId = await translationClient.getProjectId();
34+
const glossary = {
35+
languageCodesSet: {
36+
languageCodes: ['en', 'es'],
37+
},
38+
inputConfig: {
39+
gcsSource: {
40+
inputUri: 'gs://cloud-samples-data/translation/glossary.csv',
41+
},
42+
},
43+
name: translationClient.glossaryPath(projectId, location, glossaryId),
44+
};
45+
46+
// Construct request
47+
const request = {
48+
parent: translationClient.locationPath(projectId, location),
49+
glossary: glossary,
50+
};
51+
52+
// Create glossary using a long-running operation.
53+
// You can wait for now, or get results later.
54+
const [operation] = await translationClient.createGlossary(request);
55+
56+
// Wait for operation to complete.
57+
await operation.promise();
58+
});
59+
60+
it('should delete a glossary', async () => {
61+
const projectId = await translationClient.getProjectId();
62+
63+
const output = await exec(
64+
`node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${glossaryId}`
65+
);
66+
assert.match(output, /glossary/);
67+
});
68+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright 2019, Google, Inc.
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').v3beta1;
20+
const execa = require('execa');
21+
const exec = async cmd => (await execa.shell(cmd)).stdout;
22+
23+
const REGION_TAG = 'translate_detect_language_beta';
24+
25+
describe(REGION_TAG, () => {
26+
it('should detect the language of the input text', async () => {
27+
const translationClient = new TranslationServiceClient();
28+
const projectId = await translationClient.getProjectId();
29+
const location = 'global';
30+
const text = `'Hæ sæta'`;
31+
const output = await exec(
32+
`node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${text}`
33+
);
34+
assert.match(output, /Language Code: is/);
35+
assert.match(output, /Confidence: 1/);
36+
});
37+
});
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* Copyright 2019, Google, Inc.
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').v3beta1;
20+
const execa = require('execa');
21+
const exec = async cmd => (await execa.shell(cmd)).stdout;
22+
23+
const REGION_TAG = 'translate_get_glossary_beta';
24+
25+
describe(REGION_TAG, () => {
26+
const translationClient = new TranslationServiceClient();
27+
const location = 'us-central1';
28+
const glossaryId = 'test-glossary';
29+
30+
before(async function() {
31+
// Add a glossary to get
32+
const projectId = await translationClient.getProjectId();
33+
const glossary = {
34+
languageCodesSet: {
35+
languageCodes: ['en', 'es'],
36+
},
37+
inputConfig: {
38+
gcsSource: {
39+
inputUri: 'gs://cloud-samples-data/translation/glossary.csv',
40+
},
41+
},
42+
name: translationClient.glossaryPath(projectId, location, glossaryId),
43+
};
44+
45+
// Construct request
46+
const request = {
47+
parent: translationClient.locationPath(projectId, location),
48+
glossary: glossary,
49+
};
50+
51+
// Create glossary using a long-running operation.
52+
// You can wait for now, or get results later.
53+
const [operation] = await translationClient.createGlossary(request);
54+
55+
// Wait for operation to complete.
56+
await operation.promise();
57+
});
58+
59+
it('should get a glossary', async () => {
60+
const projectId = await translationClient.getProjectId();
61+
62+
const output = await exec(
63+
`node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${glossaryId}`
64+
);
65+
assert.match(output, /test-glossary/);
66+
});
67+
68+
after(async function() {
69+
//delete the glossary we created
70+
const projectId = await translationClient.getProjectId();
71+
const name = translationClient.glossaryPath(
72+
projectId,
73+
location,
74+
glossaryId
75+
);
76+
const request = {
77+
parent: translationClient.locationPath(projectId, location),
78+
name: name,
79+
};
80+
81+
// Delete glossary using a long-running operation.
82+
// You can wait for now, or get results later.
83+
const [operation] = await translationClient.deleteGlossary(request);
84+
85+
// Wait for operation to complete.
86+
await operation.promise();
87+
});
88+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright 2019, Google, Inc.
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').v3beta1;
20+
const execa = require('execa');
21+
const exec = async cmd => (await execa.shell(cmd)).stdout;
22+
23+
const REGION_TAG = 'translate_list_codes_beta';
24+
25+
describe(REGION_TAG, () => {
26+
it('should list available language codes', async () => {
27+
const translationClient = new TranslationServiceClient();
28+
const projectId = await translationClient.getProjectId();
29+
const output = await exec(`node v3beta1/${REGION_TAG}.js ${projectId}`);
30+
assert.match(output, /Language Code: en/);
31+
assert.match(output, /Language Code: fr/);
32+
});
33+
});

0 commit comments

Comments
 (0)