I use LibreOffice/JOD library to generate PDF files based on an ODT template I manually created.
These templates use jooscript to populate the template with values from the database. Two (2) of these database values are translated to Chinese.
One of these values is a string which translates correctly, however as of the 19th of May 2017 the other value, a datetime fails to convert.
I receive a HTTP 400 Bad Request error message coming back from the API.
The translator page looks as below:
package com.anguillafsd.acorn.pam.conf;
import wslite.rest.*
import wslite.http.auth.*
import java.text.SimpleDateFormat
import java.util.Date;
import javax.management.monitor.Monitor.NumericalType;
import com.anguillafsd.acorn.pam.conf.NumericTranslator
import com.anguillafsd.acorn.pam.util.Translatable
class Translator implements Translatable{
def id = *********
def secret = *********
def getToken = {
def client = new RESTClient("https://datamarket.accesscontrol.windows.net/v2/OAuth2-13")
try{
client.post(){
type "application/x-www-form-urlencoded"
urlenc client_id:id , client_secret:secret , scope: "http://api.microsofttranslator.com", grant_type: "client_credentials"
}.json.access_token
}
catch(e){ e.printStackTrace(); return null;}
}
public String translate(String text, String from, String to){
def client = new RESTClient("http://api.microsofttranslator.com/v2/Http.svc/Translate")
client.get(path:'', query:[text:"$text", from:"$from", to:"$to"], headers:["Authorization":"Bearer ${getToken()}"]).xml
}
public String translateDate(Date date,String to){
def numericTranslator = new NumericTranslator()
def df = new SimpleDateFormat("dd MMMM yyyy")
def txt = translate(df.format(date), 'en', to)
def yearMatch = (txt =~ /(\d{4})/)
yearMatch.each {key,value -> key.each { txt = txt.replaceFirst(it, numericTranslator.translate(Long.parseLong(it),to)) }}
def otherMatch = (txt =~ /\d{1,2}/)
otherMatch.each{value -> txt = txt.replaceFirst(value,numericTranslator.translate(Long.parseLong(value),to))}
return txt
}
public String translateNumber(BigInteger number, String to) {
def numericTranslator = new NumericTranslator()
numericTranslator.translate(number,to)
}
}
The jooscript used to translate the datetime is as follows with certificate.issueDate
being the database variable.
${translate(certificate.issueDate?datetime,"en","zh-CHT" )}
The format of certificate.issueDate
is Date: dd-MON-yy
Any assistance is appreciated since this code remains unchanged and has been working for the last few years.
Updated:
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: http://api.microsofttranslator.com/v2/Http.svc/Translate?text=06+December+2017&from=en&to=zh-CHT
Request : Translate?text=06+December+2017&from=en&to=zh-CHT
Found that when we change the text to anything it gives the same response, i.e. translating "Hello" would give the same error.
Expected response: 六 日 十二 月 二零一七年,i.e. 6 day, 12 month,2017 year. although the format is changed using SimpleDateFormat to dd MMM yyyy.