Google Translation API
This is another example of how to use an API, but this time a commercially available API.
The only difference with the previous exampel is that you will need to obtain a commercial or a test key
from Google before you can actually run this example (check out the Google API console on how to obtain a key)
The purpose of the example is to show how simple it is to add a translation service to an application.
Google Translation API.celsrc
use Windows, Math, Strings, Editor, System
use HTTPTypes
design GoogleApi is
constructor is
cell Window = create MenuWindow("Test of a Google API")
system <- stdout.Set(Window)
create ClientDesign
end
end
design ClientDesign is
cell ConxMgr
ansi Server ="translation.googleapis.com", Port="443"
constructor is
system <- Service.Get("ConxMgr")
end
on Service.Provider(ansi Service, cell Provider) do
Service is "ConxMgr" ? ConxMgr = Provider : yield
ConxMgr <- TCPIP.SSL.Connect(Server,Port)
end
on TCPIP.SSL.Connected( ConxClass Conx) do
ConxMgr <- Set.Protocol(Conx, "HTTP", self)
end
on HTTP.Ready( cell Avatar) do
Header RequestHeaders[]
system <- println("*** HTTPS *** Client [self]: Connection with server [Server] successful. Cell: [Avatar] ")
var GoogleApiKey = "your key here"
RequestHeaders = [ [ "Host", Server],
[ "User-Agent", "CellSpeak 1.0"],
[ "Accept", "*/*"] ]
ansi URI = "/language/translate/v2?key=[GoogleApiKey]&source=en&target=de&q=Hello%20world&q=My%20name%20is%20Jeff"
Avatar <- GET(URI, "HTTP/1.1", RequestHeaders, null)
end
interface HTTP is
on GET(ansi URI, ansi Version, Header[] Headers, byte[] Content) do
Header ResponseHeaders[]
system <- println("*** Client [self]: Server [sender] GET request URI=[URI] Version=[Version]")
for each Header in Headers do
system <- println("Header [Header.Name]: [Header.Value]")
end
end
on RESPONSE(ansi Version, ansi Status, ansi ReasonPhrase, Header[] Headers, byte[] Content) do
Header ResponseHeaders[]
system <- println("[Version] [Status] [ReasonPhrase]")
for each Header in Headers do
system <- println("Header [Header.Name]: [Header.Value]")
end
system <- print(Content)
end
interface end
end