-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranslate.ts
136 lines (118 loc) · 3.84 KB
/
translate.ts
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
//Interface
interface Options {
from: string;
}
type TextObject = Record<string, string>;
// Response For Batch Translate
export interface BatchResponse {
data: {
source: {
lan: string;
text: string;
},
target: {
lan: string;
text: string;
}[];
}
}
//Response For Single Translate
export interface SingleResponse {
data: {
source: {
lan: string;
text: string;
},
target: {
lan: string;
text: string;
};
}
}
//Response For multi
type TranslatedText<T extends TextObject, U extends string[]> = {
languageCode: U[number];
} & {
[K in keyof T]: string;
};
export type MultiResponse<T extends TextObject, U extends string[]> = {
data: {
source: {
lan: string;
};
target: TranslatedText<T, U>[];
};
};
interface Translate {
batch: (text: string, targets: string[], options?: Options) => Promise<BatchResponse>;
single: (text: string, targets: string, options?: Options) => Promise<SingleResponse>;
multi: <T extends TextObject>(text: T, targets: string[], options?: Options) => Promise<MultiResponse<T, typeof targets>>
}
//Constructing translate
const translate: Translate = {} as Translate;
//Translating Batch
translate.batch = async (text: string, targets: string[], options?: Options): Promise<BatchResponse> => {
const translationPromises = targets.map(async (target) => {
const response = await fetch(`https://translate.googleapis.com/translate_a/single?client=gtx&sl=${options?.from ?? "auto"}&tl=${target}&dt=t&q=${encodeURIComponent(text)}`);
const data = await response.json();
return {
lan: target,
text: data[0][0][0]
};
});
const translations = await Promise.all(translationPromises);
return {
data: {
source: {
lan: options?.from ?? "auto",
text: text
},
target: translations
}
};
}
//Translating Single
translate.single = async (text: string, target: string, options?: Options): Promise<SingleResponse> => {
const response = await fetch(`https://translate.googleapis.com/translate_a/single?client=gtx&sl=${options?.from ?? "auto"}&tl=${target}&dt=t&q=${encodeURIComponent(text)}`);
const data = await response.json();
return {
data: {
source: {
lan: options?.from ?? "auto",
text: text
},
target: {
lan: target,
text: data[0][0][0]
}
}
};
}
//Translating Multiple
translate.multi = async <T extends TextObject>(text: T, targets: string[], options?: Options): Promise<MultiResponse<T, typeof targets>> => {
const translationPromises = targets.map(async (target) => {
const translatedFieldsPromises = Object.entries(text).map(async ([fieldName, fieldValue]) => {
const response = await fetch(`https://translate.googleapis.com/translate_a/single?client=gtx&sl=${"auto"}&tl=${options?.from ?? "auto"}&dt=t&q=${encodeURIComponent(fieldValue)}`);
const data = await response.json();
return {
[fieldName]: data[0][0][0]
};
});
const translatedFields = await Promise.all(translatedFieldsPromises);
const translatedObject = Object.assign({}, ...translatedFields);
return {
languageCode: target,
...translatedObject
} as TranslatedText<T, typeof targets>;
});
const translations = await Promise.all(translationPromises);
return {
data: {
source: {
lan: options?.from ?? "auto"
},
target: translations
}
};
}
export default translate;