Skip to content
This repository has been archived by the owner on Oct 17, 2023. It is now read-only.

Commit

Permalink
feat!: v3 is now the default API surface
Browse files Browse the repository at this point in the history
BREAKING CHANGE: this signficantly 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 committed Oct 25, 2019
1 parent 310cf60 commit 1b3400c
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 133 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"lint": "npm run check && eslint '**/*.js'",
"samples-test": "cd samples/ && npm link ../ && npm test && cd ../",
"system-test": "mocha build/system-test --timeout 600000",
"test": "nyc mocha build/test",
"test": "c8 mocha build/test",
"check": "gts check",
"clean": "gts clean",
"compile": "tsc -p . && npm run copy-js",
Expand Down Expand Up @@ -62,6 +62,7 @@
"@types/node": "^10.5.7",
"@types/proxyquire": "^1.3.28",
"@types/request": "^2.47.1",
"c8": "^6.0.0",
"codecov": "^3.0.2",
"eslint": "^6.0.0",
"eslint-config-prettier": "^6.0.0",
Expand All @@ -73,7 +74,6 @@
"jsdoc-fresh": "^1.0.1",
"linkinator": "^1.5.0",
"mocha": "^6.1.4",
"nyc": "^14.0.0",
"power-assert": "^1.6.0",
"prettier": "^1.13.5",
"proxyquire": "^2.0.1",
Expand Down
13 changes: 9 additions & 4 deletions samples/quickstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ async function main(
) {
// [START translate_quickstart]
// Imports the Google Cloud client library
const {Translate} = require('@google-cloud/translate');
const {TranslationServiceClient} = require('@google-cloud/translate');

// Instantiates a client
const translate = new Translate({projectId});
const translate = new TranslationServiceClient({projectId});

// The text to translate
const text = 'Hello, world!';
Expand All @@ -32,9 +32,14 @@ async function main(
const target = 'ru';

// Translates some text into Russian
const [translation] = await translate.translate(text, target);
const [result] = await translate.translateText({
contents: [text],
sourceLanguageCode: 'en',
targetLanguageCode: target,
parent: `projects/${projectId}`,
});
console.log(`Text: ${text}`);
console.log(`Translation: ${translation}`);
console.log(`Translation: ${result.translations[0].translatedText}`);
// [END translate_quickstart]
}

Expand Down
30 changes: 8 additions & 22 deletions samples/test/translate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
'use strict';

const {assert} = require('chai');
const {Translate} = require('@google-cloud/translate');
const cp = require('child_process');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}).trim();
const translate = new Translate();
const cmd = 'node translate.js';
const text = 'Hello world!';
const text2 = 'Goodbye!';
Expand All @@ -30,67 +28,55 @@ const toLang = 'ru';
describe('translate sample tests', () => {
it('should detect language of a single string', async () => {
const output = execSync(`${cmd} detect "${text}"`);
const [detection] = await translate.detect(text);
const expected = new RegExp(
`Detections:\n${text} => ${detection.language}`
);
const expected = new RegExp(`Detections:\n\t${text} => en`);
assert.match(output, expected);
});

it('should detect language of multiple strings', async () => {
const output = execSync(`${cmd} detect "${text}" "${text2}"`);
const [detections] = await translate.detect([text, text2]);
const expected = new RegExp(
`Detections:\n${text} => ${detections[0].language}\n${text2} => ${detections[1].language}`
`Detections:\n\t${text} => en\n\t${text2} => en`
);
assert.match(output, expected);
});

it('should list languages', () => {
const output = execSync(`${cmd} list`);
assert.match(output, /Languages:/);
assert.match(output, new RegExp(`{ code: 'af', name: 'Afrikaans' }`));
assert.match(output, new RegExp('languageCode = af'));
});

it('should list languages with a target', () => {
const output = execSync(`${cmd} list es`);
assert.match(output, /Languages:/);
assert.match(output, new RegExp(`{ code: 'af', name: 'afrikáans' }`));
assert.include(output, 'languageCode = af\tdisplayName = afrikáans');
});

it('should translate a single string', async () => {
const output = execSync(`${cmd} translate ${toLang} "${text}"`);
const [translation] = await translate.translate(text, toLang);
const expected = `Translations:\n${text} => (${toLang}) ${translation}`;
const expected = `Translations:\n\t${text} => (${toLang}) Привет, мир!`;
assert.strictEqual(output, expected);
});

it('should translate multiple strings', async () => {
const output = execSync(`${cmd} translate ${toLang} "${text}" "${text2}"`);
const [translations] = await translate.translate([text, text2], toLang);
const expected = `Translations:\n${text} => (${toLang}) ${
translations[0]
}\n${text2} => (${toLang}) ${translations[1]}`;
const expected = `Translations:\n\t${text} => (${toLang}) Привет, мир!\n\t${text2} => (${toLang}) До свидания!`;
assert.strictEqual(output, expected);
});

it('should translate a single string with a model', async () => {
const output = execSync(
`${cmd} translate-with-model ${toLang} ${model} "${text}"`
);
const [translation] = await translate.translate(text, toLang);
const expected = `Translations:\n${text} => (${toLang}) ${translation}`;
const expected = `Translations:\n\t${text} => (${toLang}) Привет, мир!`;
assert.strictEqual(output, expected);
});

it('should translate multiple strings with a model', async () => {
const output = execSync(
`${cmd} translate-with-model ${toLang} ${model} "${text}" "${text2}"`
);
const [translations] = await translate.translate([text, text2], toLang);
const expected = `Translations:\n${text} => (${toLang}) ${
translations[0]
}\n${text2} => (${toLang}) ${translations[1]}`;
const expected = `Translations:\n\t${text} => (${toLang}) Привет, мир!\n\t${text2} => (${toLang}) До свидания!`;
assert.strictEqual(output, expected);
});
});
97 changes: 59 additions & 38 deletions samples/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,101 +15,122 @@

'use strict';

async function detectLanguage(text) {
async function detectLanguage(texts) {
// [START translate_detect_language]
// Imports the Google Cloud client library
const {Translate} = require('@google-cloud/translate');
const {TranslationServiceClient} = require('@google-cloud/translate');

// Creates a client
const translate = new Translate();
const translate = new TranslationServiceClient();

/**
* TODO(developer): Uncomment the following line before running the sample.
*/
// const text = 'The text for which to detect language, e.g. Hello, world!';
// const texts = ['The text for which to detect language, e.g. Hello, world!'];

// Detects the language. "text" can be a string for detecting the language of
// a single piece of text, or an array of strings for detecting the languages
// of multiple texts.
let [detections] = await translate.detect(text);
detections = Array.isArray(detections) ? detections : [detections];
// Detects the language. "content" represents the string that the language
// should be detected for.
console.log('Detections:');
detections.forEach(detection => {
console.log(`${detection.input} => ${detection.language}`);
});

const projectId = await translate.getProjectId();
for (const text of texts) {
const [result] = await translate.detectLanguage({
content: text,
parent: `projects/${projectId}`,
});
result.languages.forEach(detection => {
console.log(`\t${text} => ${detection.languageCode}`);
});
}
// [END translate_detect_language]
}

async function listLanguages() {
// [START translate_list_codes]
// Imports the Google Cloud client library
const {Translate} = require('@google-cloud/translate');
const {TranslationServiceClient} = require('@google-cloud/translate');

// Creates a client
const translate = new Translate();
const translate = new TranslationServiceClient();

// Lists available translation language with their names in English (the default).
const [languages] = await translate.getLanguages();
// Lists available translations (language codes only).
const projectId = await translate.getProjectId();
const [result] = await translate.getSupportedLanguages({
parent: `projects/${projectId}`,
});

console.log('Languages:');
languages.forEach(language => console.log(language));
result.languages.forEach(language => {
console.log(
`\tlanguageCode = ${language.languageCode}\tdisplayName = ${language.displayName}`
);
});
// [END translate_list_codes]
}

async function listLanguagesWithTarget(target) {
// [START translate_list_language_names]
// Imports the Google Cloud client library
const {Translate} = require('@google-cloud/translate');
const {TranslationServiceClient} = require('@google-cloud/translate');

// Creates a client
const translate = new Translate();
const translate = new TranslationServiceClient();

/**
* TODO(developer): Uncomment the following line before running the sample.
*/
// const target = 'The target language for language names, e.g. ru';

// Lists available translation language with their names in a target language
const [languages] = await translate.getLanguages(target);
// Lists available translation language with their names in a target language.
const projectId = await translate.getProjectId();
const [result] = await translate.getSupportedLanguages({
parent: `projects/${projectId}`,
displayLanguageCode: target,
});

console.log('Languages:');
languages.forEach(language => console.log(language));

result.languages.forEach(language => {
console.log(
`\tlanguageCode = ${language.languageCode}\tdisplayName = ${language.displayName}`
);
});
// [END translate_list_language_names]
}

async function translateText(text, target) {
async function translateText(texts, target) {
// [START translate_translate_text]
// Imports the Google Cloud client library
const {Translate} = require('@google-cloud/translate');
const {TranslationServiceClient} = require('@google-cloud/translate');

// Creates a client
const translate = new Translate();
const translate = new TranslationServiceClient();

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const text = 'The text to translate, e.g. Hello, world!';
// const texts = ['The text to translate, e.g. Hello, world!'];
// const target = 'The target language, e.g. ru';

// Translates the text into the target language. "text" can be a string for
// translating a single piece of text, or an array of strings for translating
// multiple texts.
let [translations] = await translate.translate(text, target);
translations = Array.isArray(translations) ? translations : [translations];
console.log('Translations:');
translations.forEach((translation, i) => {
console.log(`${text[i]} => (${target}) ${translation}`);
// Translates the text into the target language. "contents" is an array of
// one or more strings for translating.
const projectId = await translate.getProjectId();
const [result] = await translate.translateText({
contents: texts,
sourceLanguageCode: 'en',
targetLanguageCode: target,
parent: `projects/${projectId}`,
});

console.log('Translations:');
result.translations.forEach((translation, i) => {
console.log(`\t${texts[i]} => (${target}) ${translation.translatedText}`);
});
// [END translate_translate_text]
}

async function translateTextWithModel(text, target, model) {
// [START translate_text_with_model]
// Imports the Google Cloud client library
const {Translate} = require('@google-cloud/translate');
const {Translate} = require('@google-cloud/translate').v2;

// Creates a client
const translate = new Translate();
Expand All @@ -136,7 +157,7 @@ async function translateTextWithModel(text, target, model) {
translations = Array.isArray(translations) ? translations : [translations];
console.log('Translations:');
translations.forEach((translation, i) => {
console.log(`${text[i]} => (${target}) ${translation}`);
console.log(`\t${text[i]} => (${target}) ${translation}`);
});
// [END translate_text_with_model]
}
Expand Down
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* @namespace google.protobuf
*/
const v3beta1 = require('./v3beta1');
import * as v3 from './v3';
import * as v2 from './v2';

/**
* The `@google-cloud/translate` package has the following named exports:
Expand Down Expand Up @@ -74,11 +74,12 @@ import * as v3 from './v3';
* const {TranslationServiceClient} =
* require('@google-cloud/translate').v3beta1;
*/
export * from './v2';
import * as v3 from './v3';
export * from './v3';

/**
* @type {object}
* @property {constructor} TranslationServiceClient
* Reference to {@link v3beta1.TranslationServiceClient}
*/
export {v3beta1, v3};
export {v2, v3beta1, v3};
8 changes: 5 additions & 3 deletions src/v3/translation_service_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ import * as gapicConfig from './translation_service_client_config.json';
const version = packageJson.version;

export interface ClientOptions extends gax.GrpcClientOptions,
gax.GoogleAuthOptions,
gax.ClientStubOptions {
gax.GoogleAuthOptions {
libName?: string;
libVersion?: string;
clientConfig?: gax.ClientConfig;
fallback?: boolean;
apiEndpoint?: string;
port?: number;
servicePath?: string;
sslCreds?: object;
}

interface Descriptors {
Expand Down Expand Up @@ -225,7 +227,7 @@ export class TranslationServiceClient {
'google.cloud.translation.v3.TranslationService') :
// @ts-ignore Do not check types for loaded protos
protos.google.cloud.translation.v3.TranslationService,
opts) as Promise<{[method: string]: Function}>;
opts as gax.ClientStubOptions) as Promise<{[method: string]: Function}>;

const translationServiceStubMethods = [
'translateText', 'detectLanguage', 'getSupportedLanguages',
Expand Down
Loading

0 comments on commit 1b3400c

Please sign in to comment.