Skip to content
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

feat(provider-deepl): omit placeholders plugin option #487

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 2 additions & 0 deletions providers/deepl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ module.exports = {
// use uppercase here!
EN: 'EN-US',
},
// controls if placeholder text inside double curly brackets should be omitted from translation
omitPlaceholders: false,
apiOptions: {
// see <https://github.com/DeepLcom/deepl-node#text-translation-options> for supported options.
// note that tagHandling Mode cannot be set this way.
Expand Down
21 changes: 21 additions & 0 deletions providers/deepl/lib/__tests__/deepl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,27 @@ describe('deepl provider', () => {
await expect(deeplProvider.usage()).resolves.toBeTruthy()
})

it('omits placeholders when option used', async () => {
server.use(http.post(`${DEEPL_PAID_API}/translate`, translateHandler))

const deeplProvider = provider.init({
apiKey: authKey,
omitPlaceholders: true,
})

// given
const params = {
sourceLocale: 'en',
targetLocale: 'de',
text: 'Some text with a placeholder {{placeholder}} in it and an {{other}} one',
}
// when
const result = await deeplProvider.translate(params)

// then
expect(result).toEqual([params.text])
})

describe('api options used', () => {
const registerHandlerEnforceParams = (
requiredParams,
Expand Down
22 changes: 20 additions & 2 deletions providers/deepl/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = {
typeof providerOptions.apiOptions === 'object'
? providerOptions.apiOptions
: {}
const omitPlaceholders = providerOptions.omitPlaceholders || false

const client = new deepl.Translator(apiKey, {
serverUrl: apiUrl,
Expand Down Expand Up @@ -77,12 +78,23 @@ module.exports = {

let textArray = Array.isArray(input) ? input : [input]

let placeholderTexts = [];

if (omitPlaceholders) {
textArray = textArray.map((text) =>
text.replace(/{{(.*?)}}/g, (match) => {
placeholderTexts.push(match)
return `<m id=${placeholderTexts.length - 1} />`
})
)
}

const { chunks, reduceFunction } = chunksService.split(textArray, {
maxLength: DEEPL_API_MAX_TEXTS,
maxByteSize: DEEPL_API_ROUGH_MAX_REQUEST_SIZE,
})

const result = reduceFunction(
let result = reduceFunction(
await Promise.all(
chunks.map(async (texts) => {
const result = await rateLimitedTranslate.withOptions(
Expand All @@ -101,7 +113,13 @@ module.exports = {
})
)
)


if (omitPlaceholders) {
result = result.map((text) =>
text.replace(/<m id=(.*?) \/>/g, (_, id) => placeholderTexts[id])
)
}

if (format === 'jsonb') {
return formatService.htmlToBlock(result)
}
Expand Down