-
Notifications
You must be signed in to change notification settings - Fork 0
/
TranslateXLIFF.gs
30 lines (26 loc) · 1.27 KB
/
TranslateXLIFF.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function translateRawFile(language,blob){
const xml = blob.getDataAsString()
const filename = blob.getName()
const document = XmlService.parse(xml)
const root = document.getRootElement()
const xliff = XmlService.getNamespace('urn:oasis:names:tc:xliff:document:1.2');
const files = root.getChildren("file",xliff)
const regex = RegExp('(<\/?)source[^>]*(>)','gi')
files.forEach((file)=>{
const bodies = file.getChildren("body",xliff)
bodies.forEach((body)=>{
const transUnits = body.getChildren("trans-unit",xliff)
transUnits.forEach((transUnit)=>{
const source = transUnit.getChild("source",xliff)
//convert to text and replace <source> with <target>, keep the rest the same
const toTranslate = XmlService.getPrettyFormat().format(source).replace(regex,'$1target$2')
console.log(toTranslate)
const targetText = LanguageApp.translate(toTranslate.replace('xmlns="urn:oasis:names:tc:xliff:document:1.2"',''),'en', language, {contentType: 'html'});
const targetXml = XmlService.parse(targetText)
transUnit.addContent(targetXml.getRootElement().detach())
})
})
})
let newXml = XmlService.getPrettyFormat().format(document);
return {filename:`translated-${language}-${filename}`,xml:newXml}
}