@@ -14,21 +14,27 @@ const MAX_TRANSLATE_BATCH_SIZE = 25;
1414const MAX_CHAR_IN_REQUEST = 4990 ;
1515
1616const translateHelpers = {
17+ translationSettings : class {
18+ subscriptionKey = '' ;
19+ to_lang = '' ;
20+ src_lang = '' ;
21+ translate_comments = false ;
22+ translate_link_text = false ;
23+ log = false ;
24+ batch_translate = MAX_TRANSLATE_BATCH_SIZE ;
25+ region = '' ;
26+ } ,
1727 /**
1828 * Helper function to parseAndTranslate lu file content
1929 * @param {string } fileContent file content
20- * @param {string } subscriptionKey translate text API key
21- * @param {string } to_lang language code to translate content to
22- * @param {string } src_lang language code for source content
23- * @param {boolean } translate_comments translate comments in .lu files if this is set to true
24- * @param {boolean } translate_link_text translate URL or LU reference link text in .lu files if this is set to true
25- * @param {boolean } log indicates if this function should write verbose messages to process.stdout
26- * @param {number } batch_translate indicates number of input lines to batch up before calling translation API
30+ * @param {translationSettings } ts translation settings
2731 * @returns {string } Localized file content
2832 * @throws {exception } Throws on errors. exception object includes errCode and text.
2933 */
30- parseAndTranslate : async function ( fileContent , subscriptionKey , to_lang , src_lang , translate_comments , translate_link_text , log , batch_translate ) {
31- let batch_translate_size = batch_translate ? parseInt ( batch_translate ) : MAX_TRANSLATE_BATCH_SIZE ;
34+ parseAndTranslate : async function ( fileContent , ts ) {
35+ const initializeTS = new this . translationSettings ( ) ;
36+ const translationSettings = { ...initializeTS , ...ts } ;
37+ let batch_translate_size = translationSettings . batch_translate ;
3238 fileContent = helpers . sanitizeNewLines ( fileContent ) ;
3339 let linesInFile = fileContent . split ( NEWLINE ) ;
3440 let linesToTranslate = [ ] ;
@@ -46,7 +52,7 @@ const translateHelpers = {
4652 addSegment ( linesToTranslate , NEWLINE , false ) ;
4753 continue ;
4854 }
49- if ( translate_comments ) {
55+ if ( translationSettings . translate_comments ) {
5056 addSegment ( linesToTranslate , currentLine . charAt ( 0 ) , false ) ;
5157 addSegment ( linesToTranslate , currentLine . substring ( 1 ) , true ) ;
5258 } else {
@@ -186,7 +192,7 @@ const translateHelpers = {
186192 continue ;
187193 }
188194 currentSectionType = PARSERCONSTS . URLORFILEREF ;
189- if ( translate_link_text ) {
195+ if ( translationSettings . translate_link_text ) {
190196 const linkValueRegEx = new RegExp ( / \( .* ?\) / g) ;
191197 let linkValueList = currentLine . trim ( ) . match ( linkValueRegEx ) ;
192198 let linkValue = linkValueList [ 0 ] . replace ( '(' , '' ) . replace ( ')' , '' ) ;
@@ -222,7 +228,7 @@ const translateHelpers = {
222228 // do we have any payload to localize? and have we hit the batch size limit?
223229 if ( ( linesToTranslate . length !== 0 ) && ( lineCtr % batch_translate_size === 0 ) ) {
224230 try {
225- localizedContent += await batchTranslateText ( linesToTranslate , subscriptionKey , to_lang , src_lang , log ) ;
231+ localizedContent += await batchTranslateText ( linesToTranslate , translationSettings ) ;
226232 linesToTranslate = [ ] ;
227233 } catch ( err ) {
228234 throw ( err )
@@ -231,7 +237,7 @@ const translateHelpers = {
231237 }
232238 if ( linesToTranslate . length !== 0 ) {
233239 try {
234- localizedContent += await batchTranslateText ( linesToTranslate , subscriptionKey , to_lang , src_lang , log ) ;
240+ localizedContent += await batchTranslateText ( linesToTranslate , translationSettings ) ;
235241 linesToTranslate = [ ] ;
236242 } catch ( err ) {
237243 throw ( err )
@@ -245,25 +251,28 @@ const translateHelpers = {
245251 /**
246252 * Helper function to call MT rest API to translate content
247253 * @param {string } text Text to translate
248- * @param {string } subscriptionKey user provided subscription to text translation API
249- * @param {string } to_lang target language to localize to
250- * @param {string } from_lang source language of text
254+ * @param {translationSettings } translationSettings translation settings
251255 * @returns {object } response from MT call.
252256 * @throws {exception } Throws on errors. exception object includes errCode and text.
253257 */
254- translateText : async function ( text , subscriptionKey , to_lang , from_lang ) {
258+ translateText : async function ( text , translationSettings ) {
255259 let payload = Array . isArray ( text ) ? text : [ { 'Text' : text } ] ;
256- let tUri = 'https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=' + to_lang + '&includeAlignment=true' ;
257- if ( from_lang ) tUri += '&from=' + from_lang ;
260+ let tUri = 'https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=' + translationSettings . to_lang + '&includeAlignment=true' ;
261+ if ( translationSettings . src_lang ) tUri += '&from=' + translationSettings . src_lang ;
258262 const options = {
259263 method : 'POST' ,
260264 body : JSON . stringify ( payload ) ,
261265 headers : {
262266 'Content-Type' : 'application/json' ,
263- 'Ocp-Apim-Subscription-Key' : subscriptionKey ,
267+ 'Ocp-Apim-Subscription-Key' : translationSettings . subscriptionKey ,
264268 'X-ClientTraceId' : get_guid ( ) ,
265269 }
266270 } ;
271+
272+ if ( translationSettings . region ) {
273+ options . headers [ 'Ocp-Apim-Subscription-Region' ] = translationSettings . region
274+ }
275+
267276 const res = await fetch ( tUri , options ) ;
268277 if ( ! res . ok ) {
269278 throw ( new exception ( retCode . errorCode . TRANSLATE_SERVICE_FAIL , 'Text translator service call failed with [' + res . status + '] : ' + res . statusText + '.\nPlease check key & language code validity' ) ) ;
@@ -294,14 +303,11 @@ const addSegment = function (linesToTranslate, text, localize) {
294303/**
295304 * Helper function to batch calls to translate API
296305 * @param {translateLine [] } linesToTranslate Array of translateLine objects
297- * @param {string } subscriptionKey translate text API key
298- * @param {string } to_lang language code to translate content to
299- * @param {string } src_lang language code for source content
300- * @param {boolean } log indicates if this function should write verbose messages to process.stdout
306+ * @param {translationSettings } translationSettings translation settings
301307 * @returns {string } translated content
302308 * @throws {exception } Throws on errors. exception object includes errCode and text.
303309 */
304- const batchTranslateText = async function ( linesToTranslate , subscriptionKey , to_lang , src_lang , log ) {
310+ const batchTranslateText = async function ( linesToTranslate , translationSettings ) {
305311 // responsible for breaking localizable text into chunks that are
306312 // - not more than 5000 characters in combined length
307313 // - not more than 25 segments in one chunk
@@ -312,13 +318,13 @@ const batchTranslateText = async function(linesToTranslate, subscriptionKey, to_
312318 for ( var idx in linesToTranslate ) {
313319 let item = linesToTranslate [ idx ] ;
314320 if ( item . text . length + charCountInChunk >= MAX_CHAR_IN_REQUEST ) {
315- await translateAndMap ( batchTranslate , subscriptionKey , to_lang , src_lang , linesToTranslate ) ;
321+ await translateAndMap ( batchTranslate , linesToTranslate , translationSettings ) ;
316322 batchTranslate = [ ] ;
317323 charCountInChunk = 0 ;
318324 }
319325 let currentBatchSize = batchTranslate . length > 0 ? batchTranslate . length : 1 ;
320326 if ( currentBatchSize % MAX_TRANSLATE_BATCH_SIZE === 0 ) {
321- await translateAndMap ( batchTranslate , subscriptionKey , to_lang , src_lang , linesToTranslate ) ;
327+ await translateAndMap ( batchTranslate , linesToTranslate , translationSettings ) ;
322328 batchTranslate = [ ] ;
323329 charCountInChunk = 0 ;
324330 }
@@ -329,28 +335,26 @@ const batchTranslateText = async function(linesToTranslate, subscriptionKey, to_
329335 }
330336 }
331337 if ( batchTranslate . length !== 0 ) {
332- await translateAndMap ( batchTranslate , subscriptionKey , to_lang , src_lang , linesToTranslate ) ;
338+ await translateAndMap ( batchTranslate , linesToTranslate , translationSettings ) ;
333339 batchTranslate = [ ] ;
334340 charCountInChunk = 0 ;
335341 }
336342 linesToTranslate . forEach ( item => retValue += item . text ) ;
337- if ( log ) process . stdout . write ( chalk . default . gray ( retValue ) ) ;
343+ if ( translationSettings . log ) process . stdout . write ( chalk . default . gray ( retValue ) ) ;
338344 return retValue ;
339345} ;
340346
341347/**
342348 * Helper function to call translate and update text with localized result
343349 * @param {object [] } batchRequest Array of {'Text':'value'} objects
344- * @param {string } subscriptionKey translate text API key
345- * @param {string } to_lang language code to translate content to
346- * @param {string } src_lang language code for source content
350+ * @param {translationSettings } translationSettings translation settings
347351 * @param {translateLine [] } linesToTranslateCopy Array of translateLine objects
348352 * @returns {void }
349353 */
350- const translateAndMap = async function ( batchRequest , subscriptionKey , to_lang , src_lang , linesToTranslateCopy ) {
354+ const translateAndMap = async function ( batchRequest , linesToTranslateCopy , translationSettings ) {
351355 if ( batchRequest . length === 0 ) return ;
352356 let data ;
353- data = await translateHelpers . translateText ( batchRequest , subscriptionKey , to_lang , src_lang ) ;
357+ data = await translateHelpers . translateText ( batchRequest , translationSettings ) ;
354358 data . forEach ( ( item , idx ) => {
355359 // find the correponding item in linesToTranslate
356360 let itemInLine = linesToTranslateCopy . find ( item => item . idx === idx ) ;
0 commit comments