LibreOffice plugin to pipe whole Writer documents through Google Translate, that ought to keep most of the page formatting.

⌈⌋ branch:  PageTranslate


Check-in [b469e6efc0]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:prepare for language list updates (seems shorter than default list however)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | trunk
Files: files | file ages | folders
SHA1: b469e6efc0ffa0d4bdc47ed5b119b3d329c97dab
User & Date: mario 2022-11-15 17:47:11
Context
2022-11-15
17:47
prepare for language list updates (seems shorter than default list however) Leaf check-in: b469e6efc0 user: mario tags: trunk
2022-10-31
09:08
release as 2.1 check-in: e93c2ef456 user: mario tags: trunk, 2.1
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to pagetranslate.py.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/python
# encoding: utf-8
# api: uno
##type: callback
# category: language
# title: PageTranslate
# description: Action button to get whole Writer document translated
# version: 2.1
# state: stable
# author: mario
# url: https://fossil.include-once.org/pagetranslate/
# depends: python:requests (>= 2.5), python:uno
# pack: *.py, pythonpath/*.py, META-INF/*, pkg-desc, *.x*, icons/*
# config:
#    { name: frames, type: bool, value: 0, description: traverse TextFrames }







|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/python
# encoding: utf-8
# api: uno
##type: callback
# category: language
# title: PageTranslate
# description: Action button to get whole Writer document translated
# version: 2.1.0
# state: stable
# author: mario
# url: https://fossil.include-once.org/pagetranslate/
# depends: python:requests (>= 2.5), python:uno
# pack: *.py, pythonpath/*.py, META-INF/*, pkg-desc, *.x*, icons/*
# config:
#    { name: frames, type: bool, value: 0, description: traverse TextFrames }

Changes to pythonpath/pt_dialogs.py.

20
21
22
23
24
25
26



27
28
29
30
31
32
33

from com.sun.star.awt.MessageBoxButtons import BUTTONS_OK, BUTTONS_OK_CANCEL, BUTTONS_YES_NO, BUTTONS_YES_NO_CANCEL, BUTTONS_RETRY_CANCEL, BUTTONS_ABORT_IGNORE_RETRY
from com.sun.star.awt.MessageBoxButtons import DEFAULT_BUTTON_OK, DEFAULT_BUTTON_CANCEL, DEFAULT_BUTTON_RETRY, DEFAULT_BUTTON_YES, DEFAULT_BUTTON_NO, DEFAULT_BUTTON_IGNORE
from com.sun.star.awt.MessageBoxType import MESSAGEBOX, INFOBOX, WARNINGBOX, ERRORBOX, QUERYBOX
from com.sun.star.awt import FontDescriptor

langs = ("auto", "acf", "af", "aig", "am", "ar", "az", "bah", "be", "bem", "bg", "bi", "bjs", "bn", "bod", "br", "bs", "ca", "cb", "cha", "cop", "crs", "cs", "cy", "da", "de", "div", "dzo", "el", "en", "eo", "es", "et", "eu", "fa", "fi", "fn", "fo", "fr", "ga", "gcl", "gd", "gl", "grc", "gu", "gv", "gyn", "ha", "haw", "he", "hi", "hr", "ht", "hu", "hy", "id", "is", "it", "ja", "jam", "jv", "ka", "kab", "kal", "kea", "kk", "km", "kn", "ko", "ku", "ku", "ky", "la", "lb", "lo", "lt", "lv", "men", "mfe", "mg", "mh", "mi", "mk", "mn", "ms", "mt", "my", "ne", "niu", "nl", "no", "nso", "ny", "pa", "pap", "pau", "pis", "pl", "pot", "pov", "ppk", "ps", "pt", "qu", "rm", "ro", "ru", "run", "rw", "sg", "si", "sk", "sl", "smo", "sna", "so", "sq", "sr", "srn", "sv", "svc", "sw", "syc", "ta", "te", "tet", "tg", "th", "ti", "tk", "tkl", "tl", "tmh", "tn", "ton", "tpi", "tr", "tvl", "uk", "ur", "uz", "vi", "vic", "wls", "wo", "xh", "yi", "zdj", "zh-CN", "zh-TW", "zu")




class ExcApp(unohelper.Base, XActionListener, XJobExecutor):
    """
    Class documentation...
    """
    def __init__(self, ctx=uno.getComponentContext(), err="error msg", exc="exception/traceback"):
        self.LocalContext = ctx







>
>
>







20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

from com.sun.star.awt.MessageBoxButtons import BUTTONS_OK, BUTTONS_OK_CANCEL, BUTTONS_YES_NO, BUTTONS_YES_NO_CANCEL, BUTTONS_RETRY_CANCEL, BUTTONS_ABORT_IGNORE_RETRY
from com.sun.star.awt.MessageBoxButtons import DEFAULT_BUTTON_OK, DEFAULT_BUTTON_CANCEL, DEFAULT_BUTTON_RETRY, DEFAULT_BUTTON_YES, DEFAULT_BUTTON_NO, DEFAULT_BUTTON_IGNORE
from com.sun.star.awt.MessageBoxType import MESSAGEBOX, INFOBOX, WARNINGBOX, ERRORBOX, QUERYBOX
from com.sun.star.awt import FontDescriptor

langs = ("auto", "acf", "af", "aig", "am", "ar", "az", "bah", "be", "bem", "bg", "bi", "bjs", "bn", "bod", "br", "bs", "ca", "cb", "cha", "cop", "crs", "cs", "cy", "da", "de", "div", "dzo", "el", "en", "eo", "es", "et", "eu", "fa", "fi", "fn", "fo", "fr", "ga", "gcl", "gd", "gl", "grc", "gu", "gv", "gyn", "ha", "haw", "he", "hi", "hr", "ht", "hu", "hy", "id", "is", "it", "ja", "jam", "jv", "ka", "kab", "kal", "kea", "kk", "km", "kn", "ko", "ku", "ku", "ky", "la", "lb", "lo", "lt", "lv", "men", "mfe", "mg", "mh", "mi", "mk", "mn", "ms", "mt", "my", "ne", "niu", "nl", "no", "nso", "ny", "pa", "pap", "pau", "pis", "pl", "pot", "pov", "ppk", "ps", "pt", "qu", "rm", "ro", "ru", "run", "rw", "sg", "si", "sk", "sl", "smo", "sna", "so", "sq", "sr", "srn", "sv", "svc", "sw", "syc", "ta", "te", "tet", "tg", "th", "ti", "tk", "tkl", "tl", "tmh", "tn", "ton", "tpi", "tr", "tvl", "uk", "ur", "uz", "vi", "vic", "wls", "wo", "xh", "yi", "zdj", "zh-CN", "zh-TW", "zu")

def google_get_langs():
    langs = langs

class ExcApp(unohelper.Base, XActionListener, XJobExecutor):
    """
    Class documentation...
    """
    def __init__(self, ctx=uno.getComponentContext(), err="error msg", exc="exception/traceback"):
        self.LocalContext = ctx
343
344
345
346
347
348
349

350
351
352
353
def langselect(*args):

    try:
        ctx = remote_ctx                    # IDE
    except:
        ctx = uno.getComponentContext()     # UI


    app = LangSelection(ctx=ctx)
    app.showDialog()
    return app.fr, app.to








>




346
347
348
349
350
351
352
353
354
355
356
357
def langselect(*args):

    try:
        ctx = remote_ctx                    # IDE
    except:
        ctx = uno.getComponentContext()     # UI

    #google_get_langs()
    app = LangSelection(ctx=ctx)
    app.showDialog()
    return app.fr, app.to

Changes to pythonpath/translationbackends.py.

270
271
272
273
274
275
276











277
278
279
280
281
282
283
            text = found.group(1)
            text = self.html_unescape(text)
        else:
            self.log.warning("NO TRANSLATION RESULT EXTRACTED: %s", html)
            self.log.debug("ORIG TEXT: %r", text)
        return text













# variant that uses the AJAX or API interface
class GoogleAjax(BackendUtils):
    """ alternative/faster interface """

    match = r"^google.*ajax"
    raises_error = False







>
>
>
>
>
>
>
>
>
>
>







270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
            text = found.group(1)
            text = self.html_unescape(text)
        else:
            self.log.warning("NO TRANSLATION RESULT EXTRACTED: %s", html)
            self.log.debug("ORIG TEXT: %r", text)
        return text

    # available languages
    def get_langs(self):
        html = http.get("https://translate.google.com/").text
        rewr_html = """
        <div class="qSb8Pe" jsname="sgblj" data-language-code="hu" tabindex="0"><div class="l7O9Dc"><i class="material-icons-extended VfPpkd-kBDsod hsfpcd" aria-hidden="true" lang="">check</i><i class="material-icons-extended VfPpkd-kBDsod S3Dwie" aria-hidden="true" lang="">history</i></div><div class="Llmcnf" jsname="Lm8Uhb">Hungarian</div></div>
        """
        return re.findall(
            r'''<option value="([\w-]+)">''',
           # r'''\bdata-language-code="([\w-]+)"(?=[\s>])''',
            html
        )

# variant that uses the AJAX or API interface
class GoogleAjax(BackendUtils):
    """ alternative/faster interface """

    match = r"^google.*ajax"
    raises_error = False