Skip to content

Instantly share code, notes, and snippets.

@takawitter
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takawitter/e419c570bd08c8fe6c3a to your computer and use it in GitHub Desktop.
Save takawitter/e419c570bd08c8fe6c3a to your computer and use it in GitHub Desktop.
Codes to invoke Translation service on Language Grid.
/*
Copyright 2015 Takao Nakaguchi.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
function example(){
var url = "http://langrid.org/service_manager/invoker/SERVICEID";
var id = "YOURID";
var password = "YOURPASS";
var srcLang = "en";
var tgtLang = "ja";
var sheet = SpreadsheetApp.getActive().getSheetByName('SHEETNAME');
var srcCol = 1;
var tgtCol = 1;
var startRow = 1;
var maxRow = 50;
for(var i = startRow; i < maxRow; i++){
var ref = sheet.getRange(i, srcCol);
var tgt = sheet.getRange(i, tgtCol);
if(tgt.isBlank()){
tgt.setValue(
invokeTranslation(url, id, password, srcLang, tgtLang, ref.getValue())
);
}
}
}
function invokeTranslation(url, id, password, sourceLang, targetLang, source){
var xmlNs = XmlService.getXmlNamespace();
var xsiNs = XmlService.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
var soapenvNs = XmlService.getNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
var serviceNs = XmlService.getNamespace("service", "servicegrid:servicetype:nict.nlp:Translation");
var requestHeaders = {
"Authorization" : "Basic " + Utilities.base64Encode(id + ":" + password),
"SOAPAction": ""
};
var requestXml = XmlService.getRawFormat().format(XmlService.createDocument(
XmlService.createElement('Envelope', soapenvNs)
.setAttribute("xsi", "http://www.w3.org/2001/XMLSchema-instance", xmlNs)
.setAttribute("xsd", "http://www.w3.org/2001/XMLSchema", xmlNs)
.setAttribute("soapenv", "http://schemas.xmlsoap.org/soap/envelope/", xmlNs)
.setAttribute("ser", "servicegrid:servicetype:nict.nlp:Translation", xmlNs)
.addContent(XmlService.createElement("Header", soapenvNs))
.addContent(XmlService.createElement("Body", soapenvNs)
.addContent(XmlService.createElement("translate", serviceNs)
.setAttribute("encodingStyle", "http://schemas.xmlsoap.org/soap/encoding/", soapenvNs)
.addContent(XmlService.createElement("sourceLang")
.setAttribute("type", "xsd:string", xsiNs)
.addContent(XmlService.createText(sourceLang))
).addContent(XmlService.createElement("targetLang")
.setAttribute("type", "xsd:string", xsiNs)
.addContent(XmlService.createText(targetLang))
).addContent(XmlService.createElement("text")
.setAttribute("type", "xsd:string", xsiNs)
.addContent(XmlService.createText(source))
)
)
)
));
var response = UrlFetchApp.fetch(url, {
"method":"POST",
"headers":requestHeaders,
"payload":requestXml
});
return XmlService.parse(response.getContentText("UTF-8"))
.getRootElement()
.getChild("Body", soapenvNs)
.getChild("translateResponse", serviceNs)
.getChildText("translateReturn")
;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment