Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
gajim
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
John Smith
gajim
Commits
65db6c5e
Commit
65db6c5e
authored
13 years ago
by
Yann Leboulanger
Browse files
Options
Downloads
Patches
Plain Diff
make google translator plugin work. Still much work to do about configuration
parent
a07829c0
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
plugins/google_translation/plugin.py
+21
-23
21 additions, 23 deletions
plugins/google_translation/plugin.py
with
21 additions
and
23 deletions
plugins/google_translation/plugin.py
+
21
−
23
View file @
65db6c5e
...
@@ -28,6 +28,7 @@ Translates (currently only incoming) messages using Google Translate.
...
@@ -28,6 +28,7 @@ Translates (currently only incoming) messages using Google Translate.
import
re
import
re
import
urllib2
import
urllib2
import
HTMLParser
import
new
import
new
from
pprint
import
pformat
from
pprint
import
pformat
from
sys
import
getfilesystemencoding
from
sys
import
getfilesystemencoding
...
@@ -47,7 +48,7 @@ class GoogleTranslationPlugin(GajimPlugin):
...
@@ -47,7 +48,7 @@ class GoogleTranslationPlugin(GajimPlugin):
self
.
description
=
_
(
'
Translates (currently only incoming)
'
self
.
description
=
_
(
'
Translates (currently only incoming)
'
'
messages using Google Translate.
'
)
'
messages using Google Translate.
'
)
self
.
config_dialog
=
None
self
.
config_dialog
=
None
#self.gui_extension_points = {}
self
.
config_default_values
=
{
self
.
config_default_values
=
{
'
from_lang
'
:
'
from_lang
'
:
(
u
'
en
'
,
u
'
Language of text to be translated
'
),
(
u
'
en
'
,
u
'
Language of text to be translated
'
),
...
@@ -59,12 +60,11 @@ class GoogleTranslationPlugin(GajimPlugin):
...
@@ -59,12 +60,11 @@ class GoogleTranslationPlugin(GajimPlugin):
u
'
User Agent data to be used with urllib2
'
u
'
User Agent data to be used with urllib2
'
'
when connecting to Google Translate service
'
)}
'
when connecting to Google Translate service
'
)}
#self.events_handlers = {}
self
.
events_handlers
=
{
'
decrypted-message-received
'
:
(
ged
.
PREGUI
,
self
.
_nec_decrypted_message_received
)}
self
.
events
=
[
GoogleTranslateMessageReceivedEvent
]
self
.
translated_text_re
=
re
.
compile
(
self
.
translated_text_re
=
re
.
compile
(
r
'
google.language.callbacks.id100\(\'22\',
'
r
'
google.language.callbacks.id100\(\'22\',
'
'
{
"
translatedText
"
:
"
(?P<text>[^
"
]*)
"
}, 200, null, 200\)
'
)
'
{
"
translatedText
"
:
"
(?P<text>[^
"
]*)
"
}, 200, null, 200\)
'
)
@log_calls
(
'
GoogleTranslationPlugin
'
)
@log_calls
(
'
GoogleTranslationPlugin
'
)
...
@@ -90,9 +90,25 @@ class GoogleTranslationPlugin(GajimPlugin):
...
@@ -90,9 +90,25 @@ class GoogleTranslationPlugin(GajimPlugin):
translated_text
=
self
.
translated_text_re
.
search
(
results
).
group
(
'
text
'
)
translated_text
=
self
.
translated_text_re
.
search
(
results
).
group
(
'
text
'
)
if
translated_text
:
if
translated_text
:
try
:
translated_text
=
unicode
(
translated_text
,
'
unicode_escape
'
)
htmlparser
=
HTMLParser
.
HTMLParser
()
translated_text
=
htmlparser
.
unescape
(
translated_text
)
except
Exception
:
pass
return
translated_text
return
translated_text
return
text
return
text
@log_calls
(
'
GoogleTranslationPlugin
'
)
def
_nec_decrypted_message_received
(
self
,
obj
):
if
not
obj
.
msgtxt
:
return
from_lang
=
self
.
config
[
'
from_lang
'
]
to_lang
=
self
.
config
[
'
to_lang
'
]
translated_text
=
self
.
translate_text
(
obj
.
msgtxt
,
from_lang
,
to_lang
)
if
translated_text
:
obj
.
msgtxt
=
translated_text
@log_calls
(
'
GoogleTranslationPlugin
'
)
@log_calls
(
'
GoogleTranslationPlugin
'
)
def
activate
(
self
):
def
activate
(
self
):
pass
pass
...
@@ -100,21 +116,3 @@ class GoogleTranslationPlugin(GajimPlugin):
...
@@ -100,21 +116,3 @@ class GoogleTranslationPlugin(GajimPlugin):
@log_calls
(
'
GoogleTranslationPlugin
'
)
@log_calls
(
'
GoogleTranslationPlugin
'
)
def
deactivate
(
self
):
def
deactivate
(
self
):
pass
pass
class
GoogleTranslateMessageReceivedEvent
(
nec
.
NetworkIncomingEvent
):
name
=
'
google-translate-message-received
'
base_network_events
=
[
'
raw-message-received
'
]
def
generate
(
self
):
msg_type
=
self
.
base_event
.
stanza
.
attrs
.
get
(
'
type
'
,
None
)
if
msg_type
==
u
'
chat
'
:
msg_text
=
""
.
join
(
self
.
base_event
.
stanza
.
kids
[
0
].
data
)
if
msg_text
:
from_lang
=
self
.
plugin
.
config
[
'
from_lang
'
]
to_lang
=
self
.
plugin
.
config
[
'
to_lang
'
]
self
.
base_event
.
stanza
.
kids
[
0
].
setData
(
self
.
plugin
.
translate_text
(
msg_text
,
from_lang
,
to_lang
))
# We only want to modify old event, not emit another, so we return False
# here.
return
False
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment