Korean translation from Chang Hyun Park
[zxing.git] / android / utils / translate.py
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2010 ZXing authors
4 #
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
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Translate a string from English to all locales used in the Barcode
18 # Scanner Android project
19 #
20 # Author: Neha Pandey
21
22 from urllib2 import urlopen
23 from urllib import urlencode
24 import sys
25
26 def translate (in_lang, out_lang, input):
27     """Translate the input from in_lang to out_lang using Google Translate"""
28     # Create the URL
29     langpair = '%s|%s' % (in_lang, out_lang)
30     base = 'http://ajax.googleapis.com/ajax/services/language/translate?'
31     params = urlencode ((('v',1.0),
32                          ('q',input),
33                          ('langpair',langpair),) )
34     url = base + params
35     # Call translation
36     content = urlopen(url).read()
37
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]
43     return output
44
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',
49                  'zh-TW']
50
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]
54     import sys
55     sys.exit (-1);
56
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:])
61
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
68