Hi all,
I’m new to XOJO and just an amateur developer. During the xmass holidays I would like to use XOJO to build a small app to automate something I need to do frequently: translate a plain text file and store the source and translated lines in an XLIFF file. I would like to use Google Translate (or Microsoft Translator) for the translation work.
I see I can send a translation to google like this: https://translate.google.com/?sl=&en=0&tl=nl&text=This%20is%20a%20small%20test
But I don’t know how I can capture the reply.
Has anyone done this before OR is there a smarter way to do this? I’m hoping someone can share some code to get me started.
thanks & Merry Christmas!
gert
In the mean time I learned that I should do an API call and get a JSON object that needs to be parsed as a dictionary.
{ "data": { "translations": [ { "translatedText": "This is what I need to retrieve."
}
]
}
}
I tried to use the Lookup method, but I’m not sure how to use this. Could someone help me to retrieve “This is what I need to retrieve.”?
thanks
gert
You may want to have a look at https://forum.xojo.com/10694-web-robot
Alain Bailleul posted an interesting example.
Michel, I think that what Alain is doing is not the right approach…
This is what I got so far:
[code] Dim r As New JSONItem
Dim x As New JSONItem
Dim y As New JSONItem
Dim z As String
r.Load(result)
if r.IsArray ( ) then
MsgBox “JSONItem IsArray”
else
r.Compact = True
x=r.Lookup("data", "test1")
y=x.Lookup("translations", "test2")
z=y.Lookup("translatedText", "test3")
MsgBox(z)
End[/code]
All is OK till I do the lookup in y. Then I get a JSONexception error.
y ToString contains:
[{"translatedText":"ce est un petit test."}]
so I don’t understand why I cannot get the translatedText out of it via a lookup…
Alain project may not be to your liking, but it does work…
I’m sure it work for the regular Google Translate. I want to use the paid variant.
[quote=154976:@Gert Van Assche]Michel, I think that what Alain is doing is not the right approach…
This is what I got so far:
[code] Dim r As New JSONItem
Dim x As New JSONItem
Dim y As New JSONItem
Dim z As String
r.Load(result)
if r.IsArray ( ) then
MsgBox “JSONItem IsArray”
else
r.Compact = True
x=r.Lookup("data", "test1")
y=x.Lookup("translations", "test2")
z=y.Lookup("translatedText", "test3")
MsgBox(z)
End[/code]
All is OK till I do the lookup in y. Then I get a JSONexception error.
y ToString contains:
[{"translatedText":"ce est un petit test."}]
so I don’t understand why I cannot get the translatedText out of it via a lookup…[/quote]
The reason this last one fails is that it is an array, not simply a key/value pair.
You will need code that looks like this:
z = y.Child(0).Lookup("translatedText", "test3")
Greg, that is indeed what I needed. Thanks a lot!