Skip to content

Commit

Permalink
feat: add show_billed_characters option to translate-text function
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-jones-dev committed Sep 13, 2024
1 parent b7fd098 commit 573845a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
* Add supported glossary languages: Danish (`'da'`), Norwegian (bokmål)
(`'nb'`), and Swedish (`'sv'`).
* Add billed characters to translate-text function: (`show_billed_characters`
request parameter, and `billed_characters` response parameter).


## [1.12.0] - 2024-04-08
Expand Down
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ async function handleTranslate(req, res) {
getParamFormality(req, targetLang);
getParam(req, 'tag_handling', { default: 'xml', allowedValues: ['html', 'xml'] });
getParam(req, 'outline_detection', { default: '1', allowedValues: ['0', '1', true, false] });
const showBilledCharacters = getParam(req, 'show_billed_characters', { default: false, allowedValues: ['0', '1', true, false] });

// Calculate the character count of the requested translation
const totalCharacters = textArray.reduce((total, text) => (total + text.length), 0);
Expand All @@ -193,8 +194,13 @@ async function handleTranslate(req, res) {
res.status(456).send({ message: 'Quota for this billing period has been exceeded.' });
} else {
const body = {
translations: textArray.map((text) => languages.translate(text, targetLang, sourceLang,
glossary)),
translations: textArray.map((text) => {
const result = languages.translate(text, targetLang, sourceLang, glossary);
if (showBilledCharacters) {
result.billed_characters = text.length;
}
return result;
}),
};
res.status(200).send(body);
}
Expand Down

0 comments on commit 573845a

Please sign in to comment.