Skip to content

Commit 0019c43

Browse files
Merge pull request #21 from AgrI-Mitra/fix/link-translation
Fixed urls getting translated across languages
2 parents 02ce5fc + 5f94617 commit 0019c43

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/modules/aiTools/ai-tools.service.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ export class AiToolsService {
8181
text: string
8282
) {
8383
try {
84+
const urlRegex = /(https?:\/\/[^\s]+)|(www\.[^\s]+)/g;
85+
const urls = text.match(urlRegex) || [];
86+
87+
const placeHolder = "9kBjf325" //placeholder which stays the same across languages after translation
88+
const textWithoutUrls = text.replace(urlRegex, placeHolder)
8489
let config = {
8590
"language": {
8691
"sourceLanguage": source,
@@ -89,7 +94,7 @@ export class AiToolsService {
8994
}
9095
let bhashiniConfig: any = await this.getBhashiniConfig('translation',config)
9196

92-
let textArray = text.split("\n")
97+
let textArray = textWithoutUrls.split("\n")
9398
for(let i=0;i<textArray.length;i++){
9499
let response: any = await this.computeBhashini(
95100
bhashiniConfig?.pipelineInferenceAPIEndPoint?.inferenceApiKey?.value,
@@ -112,8 +117,9 @@ export class AiToolsService {
112117
}
113118
textArray[i]=response?.pipelineResponse[0]?.output[0]?.target
114119
}
120+
const translatedText = textArray.join('\n').replace(new RegExp(placeHolder, 'g'), () => urls.shift() || '');
115121
return {
116-
text: textArray.join('\n'),
122+
text: translatedText,
117123
error: null
118124
}
119125
} catch(error){

0 commit comments

Comments
 (0)