Skip to content

Commit

Permalink
Handle empty glossary files (#111)
Browse files Browse the repository at this point in the history
* add glossaries

* Install packages

* Improve DeepL glossaries handling

* Add postinstall script

* Install before building

* Fixes for PR

* Fix error when no glossariesDir is passed

* Handle empty glossary files

---------

Co-authored-by: idaapayo <idaapayo@gmail.com>
  • Loading branch information
steventilator and Idaapayo committed Apr 16, 2024
1 parent 819415d commit 1719478
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/services/deepl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ export class DeepL implements TranslationService {
)) {
entries += `${sourceEntry}\t${targetEntry}\n`;
}
// Abort if the glossary JSON is empty.
if (!entries.length) {
console.log('Cannot use glossary because it is empty.');
return;
}
// Create the request body:
const body = {
name: this.appName,
Expand Down Expand Up @@ -255,7 +260,7 @@ export class DeepL implements TranslationService {
g.target_lang === to.toLowerCase(),
);
if (!glossary) {
throw new Error(`Could not find matching glossary for ${from} -> ${to}`);
return null;
}
if (!glossary.ready) {
throw new Error(`${from} -> ${to} glossary is not ready yet.`);
Expand Down Expand Up @@ -289,8 +294,10 @@ export class DeepL implements TranslationService {
if (this.glossariesDir) {
// Find the glossary that matches the source and target language:
const glossary = await this.getGlossary(from, to);
// Add it to the options body:
body['glossary_id'] = glossary.glossary_id;
if (glossary) {
// Add it to the options body:
body['glossary_id'] = glossary.glossary_id;
}
}

if (this.supportsFormality(to)) {
Expand Down

0 comments on commit 1719478

Please sign in to comment.