This repository was archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathtranslate.ts
More file actions
222 lines (207 loc) · 7.28 KB
/
translate.ts
File metadata and controls
222 lines (207 loc) · 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright 2015 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.
import * as assert from 'assert';
import {describe, it} from 'mocha';
import {TranslationServiceClient} from '../src';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const http2spy = require('http2spy');
describe('translate', () => {
const translate = new TranslationServiceClient();
describe('detecting language from input', () => {
const INPUT = [
{
content: 'Hello!',
expectedLanguage: 'en',
},
{
content: 'Esto es una prueba.',
expectedLanguage: 'es',
},
];
it('should detect a language', async () => {
const projectId = await translate.getProjectId();
for (const input of INPUT) {
const [result] = await translate.detectLanguage({
content: input.content,
parent: `projects/${projectId}`,
});
assert.strictEqual(
result.languages![0].languageCode,
input.expectedLanguage
);
}
});
});
describe('translations', () => {
const INPUT = [
{
content: 'Hello!',
expectedTranslation: 'Hola',
},
{
content: 'How are you today?',
expectedTranslation: 'Cómo estás hoy',
},
];
function removeSymbols(input: string) {
// Remove the leading and trailing ! or ? symbols. The API has been known
// to switch back and forth between returning "Cómo estás hoy" and
// "¿Cómo estás hoy?", so let's just not depend on that.
return input.replace(/^\W|\W*$/g, '');
}
it('should translate input', async () => {
const projectId = await translate.getProjectId();
const [results] = await translate.translateText({
contents: INPUT.map(intput => intput.content),
sourceLanguageCode: 'en',
targetLanguageCode: 'es',
parent: `projects/${projectId}`,
});
const translations = results.translations!.map(translation =>
removeSymbols(translation.translatedText as string)
);
assert.strictEqual(translations[0], INPUT[0].expectedTranslation);
assert.strictEqual(translations[1], INPUT[1].expectedTranslation);
});
it('should autodetect HTML', async () => {
const input = '<body>' + INPUT[0].content + '</body>';
const projectId = await translate.getProjectId();
const [results] = await translate.translateText({
contents: [input],
sourceLanguageCode: 'en',
targetLanguageCode: 'es',
parent: `projects/${projectId}`,
});
const translation = (results.translations![0].translatedText as string)
.split(/<\/*body>/g)[1]
.trim();
assert.strictEqual(
removeSymbols(translation),
INPUT[0].expectedTranslation
);
});
});
describe('supported languages', () => {
it('should get a list of supported languages', async () => {
const projectId = await translate.getProjectId();
const [result] = await translate.getSupportedLanguages({
parent: `projects/${projectId}`,
});
const englishResult = result.languages!.filter(
l => l.languageCode === 'en'
)[0];
assert.deepStrictEqual(englishResult, {
languageCode: 'en',
displayName: '',
supportSource: true,
supportTarget: true,
});
});
it('should accept displayLanguageCode, and show appropriate displayName', async () => {
const projectId = await translate.getProjectId();
const [result] = await translate.getSupportedLanguages({
parent: `projects/${projectId}`,
displayLanguageCode: 'es',
});
const englishResult = result.languages!.filter(
l => l.languageCode === 'en'
)[0];
assert.deepStrictEqual(englishResult, {
languageCode: 'en',
displayName: 'inglés',
supportSource: true,
supportTarget: true,
});
});
it('should populate x-goog-user-project header, and succeed if valid project', async () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const {GoogleAuth} = require('google-auth-library');
const auth = new GoogleAuth({
credentials: Object.assign(
// eslint-disable-next-line @typescript-eslint/no-var-requires
require(process.env.GOOGLE_APPLICATION_CREDENTIALS || ''),
{
quota_project_id: process.env.GCLOUD_PROJECT,
}
),
});
const {TranslationServiceClient} = http2spy.require(
require.resolve('../src')
);
const translate = new TranslationServiceClient({
auth,
});
// We run the same test as "list of supported languages", but with an
// alternate "quota_project_id" set; Given that GCLOUD_PROJECT
// references a valid project, we expect success:
const projectId = await translate.getProjectId();
// This should not hrow an exception:
await translate.getSupportedLanguages({
parent: `projects/${projectId}`,
});
// Ensure we actually populated the header:
assert.strictEqual(
process.env.GCLOUD_PROJECT,
http2spy.requests[http2spy.requests.length - 1][
'x-goog-user-project'
][0]
);
});
it('should populate x-goog-user-project header, and fail if invalid project', async () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const {GoogleAuth} = require('google-auth-library');
const auth = new GoogleAuth({
credentials: Object.assign(
// eslint-disable-next-line @typescript-eslint/no-var-requires
require(process.env.GOOGLE_APPLICATION_CREDENTIALS || ''),
{
quota_project_id: 'my-fake-billing-project',
}
),
});
const {TranslationServiceClient} = http2spy.require(
require.resolve('../src')
);
const translate = new TranslationServiceClient({
auth,
});
// We set a quota project "my-fake-billing-project" that does not exist,
// this should result in an error.
let err: Error | null = null;
try {
const projectId = await translate.getProjectId();
await translate.getSupportedLanguages({
parent: `projects/${projectId}`,
});
} catch (_err) {
err = _err as Error;
}
assert(err);
assert(
err!.message.includes(
// make sure the error included our fake project name, we shouldn't
// be too specific about the error incase it changes upstream.
'my-fake-billing-project'
),
err!.message
);
assert.strictEqual(
'my-fake-billing-project',
http2spy.requests[http2spy.requests.length - 1][
'x-goog-user-project'
][0]
);
});
});
});