Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@samchon/json-translator",
"version": "2.1.2",
"version": "2.1.3",
"description": "JSON Translator via Google Translation API with optimization strategies",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
32 changes: 21 additions & 11 deletions src/JsonTranslator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,26 @@ export class JsonTranslator {
let bytes: number = 0;
const execute = async () => {
if (queue.length) {
const [response] = await this.service_.translate(
queue.map((p) => p.text),
{
from,
to: props.target,
format: "html",
},
);
const complete = (texts: string[]) => {
translated.push(...texts);
queue = [];
bytes = 0;
};
let response: string[] = [];
try {
[response] = await this.service_.translate(
queue.map((p) => p.text),
{
from,
to: props.target,
format: "html",
},
);
} catch (exp) {
if (exp instanceof Error && exp.message.includes("Bad language pair"))
return complete(queue.map((p) => p.text));
throw exp;
}
const expected: number = queue
.map((p) => p.text.split(SEPARATOR).length)
.reduce((x, y) => x + y, 0);
Expand All @@ -88,10 +100,8 @@ export class JsonTranslator {
`Mismatched translation count. Delivered ${expected} count string values to the Google Translate API, but received number is ${actual}.`,
);
}
translated.push(...response);
return complete(response);
}
queue = [];
bytes = 0;
};

for (const text of collection.raw) {
Expand Down
26 changes: 26 additions & 0 deletions test/features/test_bad_language_pair.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { TestValidator } from "@nestia/e2e";
import { JsonTranslator } from "@samchon/json-translator";
import typia, { tags } from "typia";

import input from "../../assets/input/bbs.article.json";

export const test_bad_language_pair = async (
translator: JsonTranslator,
): Promise<void> => {
typia.assertGuard<IBbsArticle>(input);

const output: IBbsArticle = await translator.translate({
input,
target: "en",
filter: ({ key }) => key === "title" || key === "body",
});
TestValidator.equals("english to english")(input)(output);
};

interface IBbsArticle {
id: string & tags.Format<"uuid">;
title: string;
body: string;
created_at: string & tags.Format<"date-time">;
updated_at: string & tags.Format<"date-time">;
}