2 # -*- coding: utf-8 -*-
4 # Copyright 2006 Zuza Software Foundation
6 # This file is part of translate.
8 # translate is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # translate is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with translate; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 """Small example program for querying an XML-RPC lookup service"""
25 from translate
.storage
import tbx
26 from xml
.dom
import minidom
30 server_url
= 'http://localhost:1234/'
31 server
= xmlrpclib
.Server(server_url
)
32 UnitClass
= tbx
.tbxunit
34 text
= sys
.stdin
.readline()
36 text
= text
.strip().decode("utf-8")
38 source
= server
.lookup(text
)
41 #Lets assume life is simple:
42 if "<termEntry>" in source
:
44 base
= minidom
.parseString(source
)
45 unit
= UnitClass
.createfromxmlElement(base
.documentElement
, None)
46 #Do something interesting with unit
47 elif "<tu><tuv>" in source
:
49 base
= minidom
.parseString
50 unit
= tmx
.createfromxmlElement(base
.documentElement
, None)
51 target
= server
.translate(text
)
52 print "%s -> %s".decode('utf-8') % (text
, target
)
55 candidates
= server
.matches(text
)
56 #alternate example, slightly faster:
57 #candidates = server.matches(text, 5, 70)
59 print "Likely matches:"
60 columnwidth
= min(int(len(text
)*1.3)+5, 35)
61 for score
, original
, translation
in candidates
:
62 print "%s %-*s | %s".encode('utf-8') % (score
, columnwidth
, original
, translation
)
64 print "No likely matches found"
65 text
= sys
.stdin
.readline()