3 # Copyright (C) 2010 ZXing authors
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Translate a string from English to all locales used in the Barcode
18 # Scanner Android project
22 from urllib2 import urlopen
23 from urllib import urlencode
26 def translate (in_lang, out_lang, input):
27 """Translate the input from in_lang to out_lang using Google Translate"""
29 langpair = '%s|%s' % (in_lang, out_lang)
30 base = 'http://ajax.googleapis.com/ajax/services/language/translate?'
31 params = urlencode ((('v',1.0),
33 ('langpair',langpair),) )
36 content = urlopen(url).read()
38 # Snip out unwanted fluff from the translation
39 start_index = content.find('"translatedText":"') + 18
40 translation = content [start_index:]
41 end_index = translation.find('"}, "')
42 output = translation[:end_index]
45 # All the languages to translate to
46 language_list = ['en', 'ar', 'cs', 'da', 'de', 'es',
47 'fi', 'fr', 'hu', 'it', 'ja', 'nl',
48 'pl', 'pt', 'ru', 'sv', 'zh-CN',
51 if (len(sys.argv) < 3):
52 print "Usage: %s name String to translate" % sys.argv[0]
53 print "Sample: %s ask-banana Give me a banana" % sys.argv[0]
57 # First argument is the name of the string
58 string_name = sys.argv[1]
59 # Remaining arguments is the string to be translated in English
60 input_string =' '.join(sys.argv[2:])
62 # Translate all languages
63 for i in range(len(language_list)) :
64 translation = translate ('en', language_list[i], input_string)
65 xml_string = '<string name="' + string_name + '">' + \
66 translation + '</string>'
67 print language_list[i], xml_string