Skip to content

Commit

Permalink
feat: omit placeholders plugin option
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrixounez committed Feb 3, 2025
1 parent 059fc2c commit 09c09e9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
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

0 comments on commit 09c09e9

Please sign in to comment.