OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Google translator (Python) for Linux & Windows

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
covar
General User
General User


Joined: 17 Feb 2007
Posts: 25

PostPosted: Sat Sep 10, 2011 5:21 am    Post subject: Google translator (Python) for Linux & Windows Reply with quote

An example script in Python and macro to work with Google Translate, the translation direction English-> Russian (en | ru).

Script in Python gt.py:

Code:
 # -*- coding: utf-8 -*-

import re, urllib

import json

class UrlOpener(urllib.FancyURLopener):

    version = "py-gtranslate/1.0"

base_uri = "http://ajax.googleapis.com/ajax/services/language/translate"

def translate(phrase):

    src="en"

    to="ru"

    data = urllib.urlencode({'v': '1.0', 'langpair': '%s|%s' % (src, to), 'q': phrase.encode('utf-8')})

    resp = json.load(UrlOpener().open('%s?%s' % (base_uri, data)))

    return resp['responseData']['translatedText']


Macro gt for translation (Calc):

Code:
Sub gt
oDoc=ThisComponent

oSheet=oDoc.CurrentController.getActiveSheet()

oCell1 = oDoc.CurrentSelection

ncol=oCell1.CellAddress.Column
nrow=oCell1.cellAddress.Row

TextEN=oCell1.getString()

script="gt.py"
   
script = createUnoService("com.sun.star.script.provider.MasterScriptProviderFactory")._
   createScriptProvider("")._
    getScript("vnd.sun.star.script:" & script & "$translate?language=Python&location=share")
   
stret = script.invoke(array(TextEN), array(), array())
   
oCell=oSheet.getCellByposition(ncol+1,nrow)
oCell.setString(stret)
End Sub


Screenshots and examples (in Russian):
http://libreoffice.3nx.ru/viewtopic.php?t=19


Moderation probe1: moved to CODE SNIPPETS section, although untested
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group