@@ -778,7 +778,7 @@ export class Translator {
778778 }
779779
780780 /**
781- * Creates a new glossary on DeepL server with given name, languages, and entries.
781+ * Creates a new glossary on the DeepL server with given name, languages, and entries.
782782 * @param name User-defined name to assign to the glossary.
783783 * @param sourceLang Language code of the glossary source terms.
784784 * @param targetLang Language code of the glossary target terms.
@@ -791,33 +791,43 @@ export class Translator {
791791 targetLang : LanguageCode ,
792792 entries : GlossaryEntries ,
793793 ) : Promise < GlossaryInfo > {
794- // Glossaries are only supported for base language types
795- sourceLang = nonRegionalLanguageCode ( sourceLang ) ;
796- targetLang = nonRegionalLanguageCode ( targetLang ) ;
797-
798- if ( ! isString ( name ) || name . length === 0 ) {
799- throw new DeepLError ( 'glossary name must be a non-empty string' ) ;
800- } else if ( Object . keys ( entries . entries ( ) ) . length === 0 ) {
794+ if ( Object . keys ( entries . entries ( ) ) . length === 0 ) {
801795 throw new DeepLError ( 'glossary entries must not be empty' ) ;
802796 }
803797
804798 const tsv = entries . toTsv ( ) ;
799+ return this . internalCreateGlossary ( name , sourceLang , targetLang , 'tsv' , tsv ) ;
800+ }
805801
806- const data = new URLSearchParams ( {
807- name : name ,
808- source_lang : sourceLang ,
809- target_lang : targetLang ,
810- entries_format : 'tsv' ,
811- entries : tsv ,
812- } ) ;
802+ /**
803+ * Creates a new glossary on DeepL server with given name, languages, and CSV data.
804+ * @param name User-defined name to assign to the glossary.
805+ * @param sourceLang Language code of the glossary source terms.
806+ * @param targetLang Language code of the glossary target terms.
807+ * @param csvFile String containing the path of the CSV file to be translated, or a Stream,
808+ * Buffer, or a FileHandle containing CSV file content.
809+ * @return Fulfills with a GlossaryInfo containing details about the created glossary.
810+ */
811+ async createGlossaryWithCsv (
812+ name : string ,
813+ sourceLang : LanguageCode ,
814+ targetLang : LanguageCode ,
815+ csvFile : string | Buffer | fs . ReadStream | fs . promises . FileHandle ,
816+ ) : Promise < GlossaryInfo > {
817+ let csvContent ;
818+ if ( isString ( csvFile ) ) {
819+ csvContent = ( await fs . promises . readFile ( csvFile ) ) . toString ( ) ;
820+ } else if ( csvFile instanceof fs . ReadStream ) {
821+ csvContent = ( await streamToBuffer ( csvFile ) ) . toString ( ) ;
822+ } else if ( csvFile instanceof Buffer ) {
823+ csvContent = csvFile . toString ( ) ;
824+ } else {
825+ // FileHandle
826+ csvContent = ( await csvFile . readFile ( ) ) . toString ( ) ;
827+ await csvFile . close ( ) ;
828+ }
813829
814- const { statusCode, content } = await this . httpClient . sendRequestWithBackoff < string > (
815- 'POST' ,
816- '/v2/glossaries' ,
817- { data } ,
818- ) ;
819- await checkStatusCode ( statusCode , content , true ) ;
820- return parseGlossaryInfo ( content ) ;
830+ return this . internalCreateGlossary ( name , sourceLang , targetLang , 'csv' , csvContent ) ;
821831 }
822832
823833 /**
@@ -932,6 +942,42 @@ export class Translator {
932942 } ) ;
933943 }
934944
945+ /**
946+ * Create glossary with given details.
947+ * @private
948+ */
949+ private async internalCreateGlossary (
950+ name : string ,
951+ sourceLang : LanguageCode ,
952+ targetLang : LanguageCode ,
953+ entriesFormat : string ,
954+ entries : string ,
955+ ) : Promise < GlossaryInfo > {
956+ // Glossaries are only supported for base language types
957+ sourceLang = nonRegionalLanguageCode ( sourceLang ) ;
958+ targetLang = nonRegionalLanguageCode ( targetLang ) ;
959+
960+ if ( ! isString ( name ) || name . length === 0 ) {
961+ throw new DeepLError ( 'glossary name must be a non-empty string' ) ;
962+ }
963+
964+ const data = new URLSearchParams ( {
965+ name : name ,
966+ source_lang : sourceLang ,
967+ target_lang : targetLang ,
968+ entries_format : entriesFormat ,
969+ entries : entries ,
970+ } ) ;
971+
972+ const { statusCode, content } = await this . httpClient . sendRequestWithBackoff < string > (
973+ 'POST' ,
974+ '/v2/glossaries' ,
975+ { data } ,
976+ ) ;
977+ await checkStatusCode ( statusCode , content , true ) ;
978+ return parseGlossaryInfo ( content ) ;
979+ }
980+
935981 /**
936982 * HttpClient implements all HTTP requests and retries.
937983 * @private
0 commit comments