Merge lp:~eduardo-mucelli/cairo-dock-plug-ins-extras/Translator into lp:~cairo-dock-team/cairo-dock-plug-ins-extras/third-party

Proposed by Eduardo Mucelli Rezende Oliveira
Status: Merged
Merged at revision: 123
Proposed branch: lp:~eduardo-mucelli/cairo-dock-plug-ins-extras/Translator
Merge into: lp:~cairo-dock-team/cairo-dock-plug-ins-extras/third-party
Diff against target: 151 lines (+57/-10)
4 files modified
Translator/Changelog.txt (+1/-0)
Translator/Translator (+54/-8)
Translator/Translator.conf (+1/-1)
Translator/auto-load.conf (+1/-1)
To merge this branch: bzr merge lp:~eduardo-mucelli/cairo-dock-plug-ins-extras/Translator
Reviewer Review Type Date Requested Status
Cairo-Dock Devs Pending
Review via email: mp+46251@code.launchpad.net

Commit message

Added the dictionary functionality. If there is only one word to be translated, show the dictionary with the possible translations in categories.

Description of the change

Added the dictionary functionality.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Translator/Changelog.txt'
2--- Translator/Changelog.txt 2010-10-27 20:39:26 +0000
3+++ Translator/Changelog.txt 2011-01-14 11:46:09 +0000
4@@ -1,3 +1,4 @@
5+0.1.6:(January/14/2011): Added the dictionary functionality. If there is only one word to be translated, show the dictionary with the possible translations in categories.
6 0.1.5:(October/27/2010): Added a string pre-processing to remove the possible line breaks in the text to be translated. Changed the shortcuts, both the X-server, and the Dock one were set to Ctrl+Alt[W|R]
7 0.1.4:(August/24/2010): For Cairo-dock 2.2.0 or higher, the user can move from the translation result to a new translation, or edit the recently translated text.
8 0.1.3:(August/23/2010): Adding the dock-level shortcut Ctrl + F9, requires Cairo-Dock 2.2.0 or higher. Removing glib import since it was redundant and causing problems in Debian Lenny.
9
10=== modified file 'Translator/Translator'
11--- Translator/Translator 2010-10-27 20:39:26 +0000
12+++ Translator/Translator 2011-01-14 11:46:09 +0000
13@@ -151,9 +151,17 @@
14 class TranslatorParser(SGMLParser):
15 def reset(self):
16 SGMLParser.reset(self)
17+
18+ # sentences
19 self.translated_content = ""
20 self.inside_a_element = 0 # indicates if the parser is between <span></span> tag
21+
22+ # dictionary
23+ self.dictionary = {}
24+ self.inside_div = 0
25+ self.current_type = ""
26
27+ # sentences
28 def start_span(self, attrs):
29 for name, value in attrs:
30 if name == "id" and value == "result_box": # <span id="result_box">translated text</span>
31@@ -162,10 +170,38 @@
32 def end_span(self):
33 self.inside_a_element = 0
34
35+ # dictionary
36+ def start_div(self, attrs):
37+ for name, value in attrs:
38+ if name == "id" and value == "gt-res-dict": # <div id="gt-res-dict">translated text</span>
39+ self.inside_div = 1
40+
41+ def end_div(self):
42+ self.inside_div = 0
43+
44 def handle_data(self, text):
45+ # sentences
46 if self.inside_a_element: # we're inside the tag ...
47 self.translated_content = text # ... grab the text!
48
49+ # dictionary
50+ if self.inside_div:
51+ if text == 'noun': # set, and start in dictionary the category that is being read
52+ self.current_type = 'noun'
53+ self.dictionary[self.current_type] = []
54+ elif text == 'verb':
55+ self.current_type = 'verb'
56+ self.dictionary[self.current_type] = []
57+ elif text == 'adjective':
58+ self.current_type = 'adjective'
59+ self.dictionary[self.current_type] = []
60+ elif text == 'interjection':
61+ self.current_type = 'interjection'
62+ self.dictionary[self.current_type] = []
63+
64+ if self.current_type: # any category found, append in the dictionary
65+ self.dictionary[self.current_type].append(text)
66+
67 def parse(self, page):
68 self.feed(page) # feed the parser with the page's html
69 self.close() # cya soon!
70@@ -176,6 +212,8 @@
71 access the parser to get the context of the Google Translator for this text"""
72 def __init__(self, text_to_be_translated):
73 self.text_to_be_translated = text_to_be_translated
74+ self.dictionary = {}
75+ self.interjections = []
76
77 def translate_it(self, source, destiny):
78 parser = TranslatorParser() # create the parser
79@@ -188,8 +226,8 @@
80 else:
81 parser.parse(page.read()) # feed the parser to get the specific content: translated text
82 page.close() # lets close the page connection
83- self.text_to_be_translated = parser.translated_content # from the parser, we get the translated content
84- return self.text_to_be_translated
85+ self.text_to_be_translated, self.dictionary = parser.translated_content, parser.dictionary # from the parser, we get the translated content
86+ return self.text_to_be_translated, self.dictionary
87
88 def adjust(self, text):
89 """Ajusta o texto removendo espacos, quebras de linha, codifica tudo em utf-8"""
90@@ -214,7 +252,7 @@
91 self.sources = [] # list of possible source languages
92 self.destiny = None # get it from configuration file
93 self.destinies = [] # list of possible resulting languages
94- # self.favorites = [] # TODO lista de linguas favoritas
95+ # self.favorites = [] # TODO lista de linguas favoritas
96 self.scroll_destiny_language = 0
97 self.dialog_active_time = 10 # time in seconds that the dialog window will be active
98 self.new_translation_key = 1 # there are 3 buttons, cancel = 0, new = 1 ...
99@@ -278,19 +316,27 @@
100 self.inform_start_of_waiting_process()
101
102 interface = Interface(sentence) # alimenta o tradutor com o sentence a ser traduzida
103- translated = interface.translate_it(source, destiny) # o resultado, que eh texto traduzido
104+ translated, dictionary = interface.translate_it(source, destiny) # texto traduzido e o resultado do dicionario
105+
106+ if len(sentence.split()) == 1: # Just one word, dictionary was returned
107+ message = ""
108+ for category, words in dictionary.iteritems():
109+ message += "%s\n\n" % category
110+ message += "%s\n\n" % "\n".join(words[1:]) # array of words com with the category name in first position
111+ else: # Sentence, only the translation was returned
112+ message = translated
113 try:
114- self.icon.PopupDialog({'message':translated, "buttons":"cancel;stock_new;stock_edit"}, {})
115+ self.icon.PopupDialog({'message':message, "buttons":"cancel;stock_new;stock_edit"}, {})
116 except Exception:
117 log("Error caused PopupDialog not be shown, ShowDialog was used instead")
118- self.icon.ShowDialog(translated, self.dialog_active_time)
119+ self.icon.ShowDialog(message, self.dialog_active_time)
120 else:
121 log("PopupDialog succesfully shown")
122 self.set_to_clipboard(translated) # coloca o resultado na area de transferencia
123-
124+ # apenas a primeira palavra no caso do dicionario
125 self.inform_end_of_waiting_process()
126 self.inform_current_destiny_language()
127- log ("Translated: %s" % translated)
128+ log ("Translated: %s" % message)
129
130 def ask_text(self, default=""):
131 label = "Translate from %s to %s:" % (self.source.name, self.destiny.name)
132
133=== modified file 'Translator/Translator.conf'
134--- Translator/Translator.conf 2010-10-27 20:39:26 +0000
135+++ Translator/Translator.conf 2011-01-14 11:46:09 +0000
136@@ -1,4 +1,4 @@
137-#!en;0.1.5
138+#!en;0.1.6
139
140 #[gtk-about]
141 [Icon]
142
143=== modified file 'Translator/auto-load.conf'
144--- Translator/auto-load.conf 2010-10-27 20:39:26 +0000
145+++ Translator/auto-load.conf 2011-01-14 11:46:09 +0000
146@@ -10,4 +10,4 @@
147 category = 5
148
149 # Version of the applet; change it everytime you change something in the config file. Don't forget to update the version both in this file and in the config file.
150-version = 0.1.5
151+version = 0.1.6

Subscribers

People subscribed via source and target branches