这是indexloc提供的服务,不要输入任何密码
Skip to content

Switch to cell-by-cell translation in all cases to avoid #65 #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2023
Merged
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
49 changes: 17 additions & 32 deletions src/sheetsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const UP_KEY_TARGET_LOCALE = 'targetLocale'; // User property key for saving the
const DEEPL_API_VERSION = 'v2'; // DeepL API version
const DEEPL_API_BASE_URL_FREE = `https://api-free.deepl.com/${DEEPL_API_VERSION}/`;
const DEEPL_API_BASE_URL_PRO = `https://api.deepl.com/${DEEPL_API_VERSION}/`;
const ROW_SEPARATOR = '|||';

// Threshold value of the length of the text to translate, in bytes. See https://developers.google.com/apps-script/guides/services/quotas#current_limitations
const THRESHOLD_BYTES = 1900;
Expand Down Expand Up @@ -270,37 +269,23 @@ export function translateRange(): void {
const sourceTextArr = selectedRange.getValues();
// console.log(`sourceTextArr: ${JSON.stringify(sourceTextArr)}`);

const translatedText = sourceTextArr.map((row) => {
const joinedRowText = row.join(ROW_SEPARATOR);
if (getBlobBytes(encodeURIComponent(joinedRowText)) > THRESHOLD_BYTES) {
console.info(
`[${ADDON_NAME}] ${joinedRowText} is longer than ${THRESHOLD_BYTES} bytes. Switching to per cell translation.`
);
return row.map((cellValue: string | number | boolean) => {
if (getBlobBytes(encodeURIComponent(cellValue)) > THRESHOLD_BYTES) {
throw new Error(
`[${ADDON_NAME}] Cell content is too long. Please consider splitting the content into multiple cells:\n${cellValue}`
);
} else {
Utilities.sleep(1000); // Interval to avoid concentrated access to API
// Cell-based translation
return deepLTranslate(
cellValue.toString(),
userProperties[UP_KEY_SOURCE_LOCALE],
userProperties[UP_KEY_TARGET_LOCALE]
)[0];
}
});
} else {
Utilities.sleep(1000); // Interval to avoid concentrated access to API
// Row-based translation
return deepLTranslate(
joinedRowText,
userProperties[UP_KEY_SOURCE_LOCALE],
userProperties[UP_KEY_TARGET_LOCALE]
)[0].split(ROW_SEPARATOR);
}
});
const translatedText = sourceTextArr.map((row) =>
row.map((cellValue: string | number | boolean) => {
if (getBlobBytes(encodeURIComponent(cellValue)) > THRESHOLD_BYTES) {
throw new Error(
`[${ADDON_NAME}] Cell content is too long. Please consider splitting the content into multiple cells:\n${cellValue}`
);
} else {
Utilities.sleep(1000); // Interval to avoid concentrated access to API
// Cell-based translation
return deepLTranslate(
cellValue.toString(),
userProperties[UP_KEY_SOURCE_LOCALE],
userProperties[UP_KEY_TARGET_LOCALE]
)[0];
}
})
);
// console.log(`translatedText: ${JSON.stringify(translatedText)}`);

// Set translated text in target range
Expand Down