Skip to content

Commit e70cc9a

Browse files
committed
feat(core): add microsoft translate engine
1 parent 9448d8a commit e70cc9a

3 files changed

Lines changed: 56 additions & 13 deletions

File tree

src/pipeline/after.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type { TranslationOption } from '@kabeep/node-translate';
2-
import type { ArgumentVector } from '../shared';
2+
import type { ArgumentVector, MicrosoftTranslateOption } from '../shared';
33
import {
44
getColumns,
55
getLanguageName,
66
getNativeName,
7+
gray,
78
Polysemy,
89
Sentence,
910
Source,
@@ -12,15 +13,38 @@ import {
1213
} from '../utils/index.js';
1314

1415
function after(
15-
result: TranslationOption,
16-
options: Pick<
16+
result?: TranslationOption | MicrosoftTranslateOption[],
17+
options?: Pick<
1718
ArgumentVector,
18-
'from' | 'to' | 'showPhonetics' | 'showSource' | 'showDetail'
19+
'from' | 'to' | 'showPhonetics' | 'showSource' | 'showDetail' | 'engine'
1920
>,
2021
) {
21-
const { from, to, showPhonetics, showSource, showDetail } = options;
22+
const { from, to, showPhonetics, showSource, showDetail, engine } =
23+
options || {};
2224
const columns: number = Math.max(getColumns(), 32) - 32;
2325

26+
if (!result) {
27+
console.log('No response data');
28+
return;
29+
}
30+
31+
if (engine === 'microsoft') {
32+
console.log(
33+
(result as MicrosoftTranslateOption[])[0].translations[0].text,
34+
);
35+
36+
if (showSource || showPhonetics || showDetail) {
37+
console.log(
38+
gray(
39+
'* `--engine microsoft` does not currently support the `--show-*` arguments',
40+
),
41+
);
42+
}
43+
44+
return;
45+
}
46+
47+
result = result as TranslationOption;
2448
if (showSource) {
2549
let sourceText = result.from.text.value;
2650
if (showPhonetics && result.from.text.phonetics)

src/pipeline/index.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ import before from './before';
88
import run from './run';
99

1010
async function pipeline(argv: ArgumentVector) {
11+
const {
12+
from,
13+
to,
14+
engine,
15+
timeout,
16+
retry,
17+
showPhonetics,
18+
showSource,
19+
showDetail,
20+
} = argv;
21+
1122
const text = await boundary(before)(argv);
1223
if (text === undefined) return;
1324

@@ -27,9 +38,13 @@ async function pipeline(argv: ArgumentVector) {
2738
spinner.color = 'red';
2839
}, 10_000);
2940
}, 5000);
30-
31-
const { from, to, timeout, retry } = argv;
32-
const result = await boundary(run)(text, { from, to, timeout, retry });
41+
const result = await boundary(run)(text, {
42+
from,
43+
to,
44+
engine,
45+
timeout,
46+
retry,
47+
});
3348

3449
clearTimeout(timer);
3550
spinner.stop();
@@ -38,8 +53,7 @@ async function pipeline(argv: ArgumentVector) {
3853
throw result;
3954
}
4055

41-
const { showPhonetics, showSource, showDetail } = argv;
42-
after(result, { from, to, showPhonetics, showSource, showDetail });
56+
after(result, { from, to, engine, showPhonetics, showSource, showDetail });
4357
}
4458

4559
export default pipeline;

src/pipeline/run.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
import translate, { type TranslateOptions } from '@kabeep/node-translate';
1+
import type { TranslateOptions } from '@kabeep/node-translate';
2+
import { translator } from '../helpers';
23
import locale from '../locale';
4+
import type { ArgumentVector } from '../shared';
35

4-
async function run(text: string, options: Omit<TranslateOptions, 'raw'>) {
6+
async function run(
7+
text: string,
8+
options: Omit<TranslateOptions, 'raw'> & Pick<ArgumentVector, 'engine'>,
9+
) {
510
try {
6-
return await translate(text, options);
11+
return await translator(text, options);
712
} catch (error: unknown) {
813
const { message } = error as Error;
914
switch (message) {

0 commit comments

Comments
 (0)