Skip to content

Commit 71dc451

Browse files
committed
feat: omit placeholders plugin option
1 parent 15aaa61 commit 71dc451

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

providers/deepl/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ module.exports = {
2121
// use uppercase here!
2222
EN: 'EN-US',
2323
},
24+
// controls if placeholder text inside double curly brackets should be omitted from translation
25+
omitPlaceholders: false,
2426
apiOptions: {
2527
// see <https://github.com/DeepLcom/deepl-node#text-translation-options> for supported options.
2628
// note that tagHandling Mode cannot be set this way.

providers/deepl/lib/__tests__/deepl.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,27 @@ describe('deepl provider', () => {
395395
await expect(deeplProvider.usage()).resolves.toBeTruthy()
396396
})
397397

398+
it('omits placeholders when option used', async () => {
399+
server.use(http.post(`${DEEPL_PAID_API}/translate`, translateHandler))
400+
401+
const deeplProvider = provider.init({
402+
apiKey: authKey,
403+
omitPlaceholders: true,
404+
})
405+
406+
// given
407+
const params = {
408+
sourceLocale: 'en',
409+
targetLocale: 'de',
410+
text: 'Some text with a placeholder {{placeholder}} in it and an {{other}} one',
411+
}
412+
// when
413+
const result = await deeplProvider.translate(params)
414+
415+
// then
416+
expect(result).toEqual([params.text])
417+
})
418+
398419
describe('api options used', () => {
399420
const registerHandlerEnforceParams = (
400421
requiredParams,

providers/deepl/lib/index.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module.exports = {
3131
typeof providerOptions.apiOptions === 'object'
3232
? providerOptions.apiOptions
3333
: {}
34+
const omitPlaceholders = providerOptions.omitPlaceholders || false
3435

3536
const client = new deepl.Translator(apiKey, {
3637
serverUrl: apiUrl,
@@ -74,12 +75,23 @@ module.exports = {
7475
textArray = formatService.markdownToHtml(textArray)
7576
}
7677

78+
let placeholderTexts = [];
79+
80+
if (omitPlaceholders) {
81+
textArray = textArray.map((text) =>
82+
text.replace(/{{(.*?)}}/g, (match) => {
83+
placeholderTexts.push(match)
84+
return `<m id=${placeholderTexts.length - 1} />`
85+
})
86+
)
87+
}
88+
7789
const { chunks, reduceFunction } = chunksService.split(textArray, {
7890
maxLength: DEEPL_API_MAX_TEXTS,
7991
maxByteSize: DEEPL_API_ROUGH_MAX_REQUEST_SIZE,
8092
})
8193

82-
const result = reduceFunction(
94+
let result = reduceFunction(
8395
await Promise.all(
8496
chunks.map(async (texts) => {
8597
const result = await rateLimitedTranslate.withOptions(
@@ -99,6 +111,12 @@ module.exports = {
99111
)
100112
)
101113

114+
if (omitPlaceholders) {
115+
result = result.map((text) =>
116+
text.replace(/<m id=(.*?) \/>/g, (_, id) => placeholderTexts[id])
117+
)
118+
}
119+
102120
if (format === 'markdown') {
103121
return formatService.htmlToMarkdown(result)
104122
}

0 commit comments

Comments
 (0)