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

⌈⌋ ⎇ branch:  PageTranslate


Check-in [2606a632a2]

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

Overview
Comment:DDG was merged into tb
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 2606a632a217240cf57fb42152ecc1a6efc3f4ac
User & Date: mario 2022-10-19 12:43:49
Context
2022-10-19
14:46
systran was using wrong input param check-in: ac254302d4 user: mario tags: trunk
12:43
DDG was merged into tb check-in: 2606a632a2 user: mario tags: trunk
07:40
rename lang to source_lang(), restructure from_words into individual wrappers (now in base class) check-in: 140c2f5e4b user: mario tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to Makefile.

1
2
3
4
5
6
7
8
.PHONY:	xhp lo aoo gui tk_translate help pylint version

all:	xhp version oxt install

install:
	unopkg remove vnd.include-once.pagetranslate
	unopkg add --log-file log.txt -s pagetranslate.oxt

|







1
2
3
4
5
6
7
8
.PHONY:	xhp lo aoo gui tk_translate help pylint version oxt

all:	xhp version oxt install

install:
	unopkg remove vnd.include-once.pagetranslate
	unopkg add --log-file log.txt -s pagetranslate.oxt

20
21
22
23
24
25
26

27
28

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44




45
46
47
48
49
50

lo:	lo74

lo7:	lo74

lo74:	version oxt
	/opt/libreoffice7.4/program/unopkg add pagetranslate.oxt

	/opt/libreoffice7.4/program/soffice --writer
	tail /tmp/pagetranslate-libreoffice.log

	
lo71:	version oxt
	/opt/libreoffice7.1/program/unopkg add pagetranslate.oxt
	/opt/libreoffice7.1/program/soffice --writer


help:	xhp

xhp:
	make -C help/en/vnd.include-once.pagetranslate/

pip:
	make -C pythonpath

gui:	tk_translate
	




tk_translate:
	./setup.py bdist_wheel

pylint:
	PYTHONPATH=./pythonpath/:/opt/libreoffice7.1/program/ pylint pagetranslate.py








>
|
<
>















|
>
>
>
>






20
21
22
23
24
25
26
27
28

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

lo:	lo74

lo7:	lo74

lo74:	version oxt
	/opt/libreoffice7.4/program/unopkg add pagetranslate.oxt
	tail -f /tmp/pagetranslate-libreoffice.log &
	/opt/libreoffice7.4/program/soffice --writer --norestore

	shell kill %%
	
lo71:	version oxt
	/opt/libreoffice7.1/program/unopkg add pagetranslate.oxt
	/opt/libreoffice7.1/program/soffice --writer


help:	xhp

xhp:
	make -C help/en/vnd.include-once.pagetranslate/

pip:
	make -C pythonpath

gui:	tk_translate

setup:
	./setup.py bdist_wheel
	# twine upload dist/*

tk_translate:
	./setup.py bdist_wheel

pylint:
	PYTHONPATH=./pythonpath/:/opt/libreoffice7.1/program/ pylint pagetranslate.py

Changes to NEWS.








1
2
3
4
5
6
7







2.0 (2022-10-17)
 * Add SYSTRAN backend.
 * Introduce ArgosTranslate binding.
 * Added LibreTranslate via D-T and internal.
 * Select-only mode (disable whole-doc traversal).
 * Added pt-BR as default language in menu.
 * Introduce annotate/comment mode to preserve untranslated text sections.
>
>
>
>
>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
dev (unreleased)
 * DuckDuckGo backend added.
 * Google Cloud Translate available, but untested.
 * Annotations enabled for text selection mode.
 * Internal rewrites.
 * ...

2.0 (2022-10-17)
 * Add SYSTRAN backend.
 * Introduce ArgosTranslate binding.
 * Added LibreTranslate via D-T and internal.
 * Select-only mode (disable whole-doc traversal).
 * Added pt-BR as default language in menu.
 * Introduce annotate/comment mode to preserve untranslated text sections.

Changes to help/en/vnd.include-once.pagetranslate/config.page.

115
116
117
118
119
120
121
122

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
      offers various APIs. Unfortunately the test keys are worthless for testing;
      so not sure if this backend works at all.</p>
    </item>
    <item>
      <title>LibreTranslate</title>
      <p>LibreTranslate is an instance of ArgosTranslate/OpenNMT. The
      <link href="https://libretranslate.de/">free version</link>
      only permits only a handful of requests before blocking. The

      <link href="https://libretranslate.com/">paid option</link>
      might not be worth the money over running a
      <link href="https://github.com/LibreTranslate/LibreTranslate">local instance</link>.
      (Can't configure a localhost:5000 reference yet.)</p>
    </item>
    <item>
      <title>DuckDuckGo</title>
      <p>(Not in 2.0 yet). DDG provides a basic interface for 
      <link href="https://duckduckgo.com/?q=translate">text translations</link>,
      which utilizes Microsoft Translator beind the
      scenes, but with an added privacy proxy/instance from DDG.
      Might not be usable to excessive queries / logn documents. But
      certainly an interesting alternative.</p>
    </item>
  </terms>
  <p>Some are provided via <link href="https://pypi.org/project/translate/">translate-python</link> (TP):</p>
  <terms>
    <item>
      <title>Microsoft Translator</title>







|
>



|



|



|







115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
      offers various APIs. Unfortunately the test keys are worthless for testing;
      so not sure if this backend works at all.</p>
    </item>
    <item>
      <title>LibreTranslate</title>
      <p>LibreTranslate is an instance of ArgosTranslate/OpenNMT. The
      <link href="https://libretranslate.de/">free version</link>
      only permits only a handful of requests before blocking. (Requests
      from PageTranslate cycle across alternative instances). The
      <link href="https://libretranslate.com/">paid option</link>
      might not be worth the money over running a
      <link href="https://github.com/LibreTranslate/LibreTranslate">local instance</link>.
      (There's a config dialog option for localhost:5000 e.g.)</p>
    </item>
    <item>
      <title>DuckDuckGo</title>
      <p>DDG provides a compact interface for 
      <link href="https://duckduckgo.com/?q=translate">text translations</link>,
      which utilizes Microsoft Translator beind the
      scenes, but with an added privacy proxy/instance from DDG.
      Might not be usable for excessive queries / long documents. But
      certainly an interesting alternative.</p>
    </item>
  </terms>
  <p>Some are provided via <link href="https://pypi.org/project/translate/">translate-python</link> (TP):</p>
  <terms>
    <item>
      <title>Microsoft Translator</title>

Changes to help/en/vnd.include-once.pagetranslate/config.xhp.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="UTF-8"?>
<helpdocument version="1.0">
   <meta>
      <topic id="topic_d1e3" indexer="include" status="PUBLISH">
         <title xml-lang="en" id="title_d1e3">Translation settings</title>
         <filename>/help/vnd.include-once.pagetranslate/config.xhp</filename>
      </topic>
      <history>
         <created date="2020-02-02T22:22:22"/>
         <lastedited date="2022-10-17T14:25:10.161+02:00"/>
      </history>
   </meta>
   <body>

      <bookmark id="bm_d1e7" branch="hid/vnd.include-once.pagetranslate:OptionsPageTranslate"
                xml-lang="en">
         <bookmark_value>PageTranslate settings</bookmark_value>









|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="UTF-8"?>
<helpdocument version="1.0">
   <meta>
      <topic id="topic_d1e3" indexer="include" status="PUBLISH">
         <title xml-lang="en" id="title_d1e3">Translation settings</title>
         <filename>/help/vnd.include-once.pagetranslate/config.xhp</filename>
      </topic>
      <history>
         <created date="2020-02-02T22:22:22"/>
         <lastedited date="2022-10-18T20:15:57.216+02:00"/>
      </history>
   </meta>
   <body>

      <bookmark id="bm_d1e7" branch="hid/vnd.include-once.pagetranslate:OptionsPageTranslate"
                xml-lang="en">
         <bookmark_value>PageTranslate settings</bookmark_value>
138
139
140
141
142
143
144
145

146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
      so not sure if this backend works at all.</paragraph>
            </listitem>
            <listitem id="item_d1e180" xml-lang="en">
               <emph>LibreTranslate</emph>
               <br/>
               <paragraph id="par_d1e185" role="paragraph" xml-lang="en">LibreTranslate is an instance of ArgosTranslate/OpenNMT. The
      <link href="https://libretranslate.de/">free version</link>
      only permits only a handful of requests before blocking. The

      <link href="https://libretranslate.com/">paid option</link>
      might not be worth the money over running a
      <link href="https://github.com/LibreTranslate/LibreTranslate">local instance</link>.
      (Can't configure a localhost:5000 reference yet.)</paragraph>
            </listitem>
            <listitem id="item_d1e199" xml-lang="en">
               <emph>DuckDuckGo</emph>
               <br/>
               <paragraph id="par_d1e204" role="paragraph" xml-lang="en">(Not in 2.0 yet). DDG provides a basic interface for 
      <link href="https://duckduckgo.com/?q=translate">text translations</link>,
      which utilizes Microsoft Translator beind the
      scenes, but with an added privacy proxy/instance from DDG.
      Might not be usable to excessive queries / logn documents. But
      certainly an interesting alternative.</paragraph>
            </listitem>
         </list>
         <paragraph id="par_d1e212" role="paragraph" xml-lang="en">Some are provided via <link href="https://pypi.org/project/translate/">translate-python</link> (TP):</paragraph>
         <list id="terms_d1e218" xml-lang="en">
            <listitem id="item_d1e220" xml-lang="en">
               <emph>Microsoft Translator</emph>







|
>



|




|



|







138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
      so not sure if this backend works at all.</paragraph>
            </listitem>
            <listitem id="item_d1e180" xml-lang="en">
               <emph>LibreTranslate</emph>
               <br/>
               <paragraph id="par_d1e185" role="paragraph" xml-lang="en">LibreTranslate is an instance of ArgosTranslate/OpenNMT. The
      <link href="https://libretranslate.de/">free version</link>
      only permits only a handful of requests before blocking. (Requests
      from PageTranslate cycle across alternative instances). The
      <link href="https://libretranslate.com/">paid option</link>
      might not be worth the money over running a
      <link href="https://github.com/LibreTranslate/LibreTranslate">local instance</link>.
      (There's a config dialog option for localhost:5000 e.g.)</paragraph>
            </listitem>
            <listitem id="item_d1e199" xml-lang="en">
               <emph>DuckDuckGo</emph>
               <br/>
               <paragraph id="par_d1e204" role="paragraph" xml-lang="en">DDG provides a compact interface for 
      <link href="https://duckduckgo.com/?q=translate">text translations</link>,
      which utilizes Microsoft Translator beind the
      scenes, but with an added privacy proxy/instance from DDG.
      Might not be usable for excessive queries / long documents. But
      certainly an interesting alternative.</paragraph>
            </listitem>
         </list>
         <paragraph id="par_d1e212" role="paragraph" xml-lang="en">Some are provided via <link href="https://pypi.org/project/translate/">translate-python</link> (TP):</paragraph>
         <list id="terms_d1e218" xml-lang="en">
            <listitem id="item_d1e220" xml-lang="en">
               <emph>Microsoft Translator</emph>

Deleted off/DuckDuckGo.py.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

# DuckDuckGo translation box utilizes a privacy-filtered Microsoft
# translator instance.
# It merley requires looking up a session id, and is otherwise a
# rather trivial API.
#
class DuckDuckGo(BackendUtils):

    max_len = 2000

    def __init__(self, **params):
        super().__init__(**params)

        # fetch likely search page
        vqd = re.findall(
            r""" (?: [;&] \s* vqd= ['"]? )  (\d[\w\-]+) """,
            http.get("https://duckduckgo.com/?t=ffab&q=translate&ia=web").text,
            re.X
        )
        self.log.debug("session=%s", vqd)
        self.sess = vqd[0]
        self.linbreakwise = self.fetch # text/plain respects them already

    def fetch(self, text):

        # simple parameterization
        query = "vqd=" + self.sass + "&query=translate"
        query += "&to=" + self.params["lang"]
        if self.params["from"] != "auto":
            query += "&from=" + self.params["from"]

        # get result
        result = http.post(
            "https://duckduckgo.com/translation.js?" + query,
            headers = {
                "Content-Type": "text/plain",
            },
            data=text.decode("utf-8"),
        ).json()
        if "error" in result:
            raise Exception(j)
        # else we got something
        return result["translated"]


# Cloud API variant
class GoogleApi(BackendUtils):

    max_len = 1<<16  # probably unlimited
    requires_key = True

    def translate(self, text):
        result = http.get(
            "https://translation.googleapis.com/language/translate/v2",
            data={
                "q": text,
                "target": self.params["lang"],
                "source": self.params["from"],
                "key": self.params["api_key"],
            },
        ).json()
        
        if "data" not in result or not result["translations"]:
            self.log.error(result)
        return result["data"]["translations"][0]["translatedText"]


# Online version of deep-translator (potential workaround for Python2-compat)
class DeepTransApi(BackendUtils):
    #https://deep-translator-api.azurewebsites.net/docs
    pass

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<