Skip to content

Commit 4ea6dab

Browse files
authored
Merge pull request #12 from samchon/feat/pair
Avoid bad language pair problem.
2 parents 805f92c + 516e3b3 commit 4ea6dab

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@samchon/json-translator",
3-
"version": "2.1.2",
3+
"version": "2.1.3",
44
"description": "JSON Translator via Google Translation API with optimization strategies",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/JsonTranslator.ts

+21-11
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,26 @@ export class JsonTranslator {
5858
let bytes: number = 0;
5959
const execute = async () => {
6060
if (queue.length) {
61-
const [response] = await this.service_.translate(
62-
queue.map((p) => p.text),
63-
{
64-
from,
65-
to: props.target,
66-
format: "html",
67-
},
68-
);
61+
const complete = (texts: string[]) => {
62+
translated.push(...texts);
63+
queue = [];
64+
bytes = 0;
65+
};
66+
let response: string[] = [];
67+
try {
68+
[response] = await this.service_.translate(
69+
queue.map((p) => p.text),
70+
{
71+
from,
72+
to: props.target,
73+
format: "html",
74+
},
75+
);
76+
} catch (exp) {
77+
if (exp instanceof Error && exp.message.includes("Bad language pair"))
78+
return complete(queue.map((p) => p.text));
79+
throw exp;
80+
}
6981
const expected: number = queue
7082
.map((p) => p.text.split(SEPARATOR).length)
7183
.reduce((x, y) => x + y, 0);
@@ -88,10 +100,8 @@ export class JsonTranslator {
88100
`Mismatched translation count. Delivered ${expected} count string values to the Google Translate API, but received number is ${actual}.`,
89101
);
90102
}
91-
translated.push(...response);
103+
return complete(response);
92104
}
93-
queue = [];
94-
bytes = 0;
95105
};
96106

97107
for (const text of collection.raw) {
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { TestValidator } from "@nestia/e2e";
2+
import { JsonTranslator } from "@samchon/json-translator";
3+
import typia, { tags } from "typia";
4+
5+
import input from "../../assets/input/bbs.article.json";
6+
7+
export const test_bad_language_pair = async (
8+
translator: JsonTranslator,
9+
): Promise<void> => {
10+
typia.assertGuard<IBbsArticle>(input);
11+
12+
const output: IBbsArticle = await translator.translate({
13+
input,
14+
target: "en",
15+
filter: ({ key }) => key === "title" || key === "body",
16+
});
17+
TestValidator.equals("english to english")(input)(output);
18+
};
19+
20+
interface IBbsArticle {
21+
id: string & tags.Format<"uuid">;
22+
title: string;
23+
body: string;
24+
created_at: string & tags.Format<"date-time">;
25+
updated_at: string & tags.Format<"date-time">;
26+
}

0 commit comments

Comments
 (0)