Translation helper: Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
(import scribus translator helper)
 
m (Text Exporter script was the template)
 
(3 intermediate revisions by the same user not shown)
Line 3: Line 3:
This script is meant to help with translation. It lacks style management.
This script is meant to help with translation. It lacks style management.


<pre>
I was going to use this to process my CV, which I update now and then, and I dont want to maintain two *.sla files
(ie. one for danish, one for english)
 
Basically you create an empty txt file on your system
then you run the script and select the empty text file.
The script then exports all text frames in your scribus file to the text file
then you open the text file and you put your translated text underneath each original text.
Then you run the script again, selecting the text file
and then the script replaces the text inside the text frames with the translated text.
 
Note: the script is very simple - so there is probably certain types of text input it cannot process..anyway, the ground work is there
could easily be improved with fx fuzzy string matching https://marcobonzanini.com/2015/02/25/fuzzy-string-matching-in-python/
 
<syntaxhighlight lang='python'>
#!/usr/bin/env python
#!/usr/bin/env python
# File: scribus-translator.py - Extracts the text from a document, saving to a
# File: scribus-translator.py - Extracts the text from a document, saving to a
# text file also lists image files with pathnames
# text file also lists image files with pathnames
# 2006.03.04 Gregory Pittman template script
# 2006.03.04 Gregory Pittman template script ('Text Exporter')
# 2008.02.28 Petr Vanek - fileDialog replaces valueDialog
# 2008.02.28 Petr Vanek - fileDialog replaces valueDialog
# 2018.06.22 bastianilso : scribus-translator
# 2018.06.22 bastianilso : scribus-translator
Line 28: Line 41:
     while (page <= pagenum):
     while (page <= pagenum):
         scribus.gotoPage(page)
         scribus.gotoPage(page)
        scribus.messageBox("DEBUG", "page: " + str(page), icon=0, button1=1)
#      scribus.messageBox("DEBUG", "page: " + str(page), icon=0, button1=1)
         pageItems = scribus.getPageItems()
         pageItems = scribus.getPageItems()
         for item in pageItems:
         for item in pageItems:
Line 84: Line 97:
                 translatedText += line
                 translatedText += line
                 continue
                 continue
            scribus.messageBox("DEBUG", "unknown text!\n\n" + line, icon=0, button1=1)
#          scribus.messageBox("DEBUG", "unknown text!\n\n" + line, icon=0, button1=1)
     return returnString
     return returnString


Line 102: Line 115:
     scribus.messageBox('Export Error', 'You need a Document open',
     scribus.messageBox('Export Error', 'You need a Document open',
                       icon=0, button1=1)
                       icon=0, button1=1)
</pre>
</syntaxhighlight>

Latest revision as of 19:18, 22 June 2018


This script is meant to help with translation. It lacks style management.

I was going to use this to process my CV, which I update now and then, and I dont want to maintain two *.sla files (ie. one for danish, one for english)

Basically you create an empty txt file on your system then you run the script and select the empty text file. The script then exports all text frames in your scribus file to the text file then you open the text file and you put your translated text underneath each original text. Then you run the script again, selecting the text file and then the script replaces the text inside the text frames with the translated text.

Note: the script is very simple - so there is probably certain types of text input it cannot process..anyway, the ground work is there could easily be improved with fx fuzzy string matching https://marcobonzanini.com/2015/02/25/fuzzy-string-matching-in-python/

#!/usr/bin/env python
# File: scribus-translator.py - Extracts the text from a document, saving to a
# text file also lists image files with pathnames
# 2006.03.04 Gregory Pittman template script ('Text Exporter')
# 2008.02.28 Petr Vanek - fileDialog replaces valueDialog
# 2018.06.22 bastianilso : scribus-translator
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

import scribus
import traceback


def translateText(textfile):
    page = 1
    pagenum = scribus.pageCount()
    newStrings = []
    formatStart = '<!--ORIGINAL START-->' + '\n'
    formatEnd = '\n<!--ORIGINAL END-->\n<!--TRANSLATION START-->\n<!--TRANSLATION END-->\n'
    while (page <= pagenum):
        scribus.gotoPage(page)
#       scribus.messageBox("DEBUG", "page: " + str(page), icon=0, button1=1)
        pageItems = scribus.getPageItems()
        for item in pageItems:
            if (item[1] == 4):
                contents = scribus.getAllText(item[0])
                translation = matchTranslation(contents, textfile)
                if (translation == 'NEEDS_TRANSLATION'):
                    newStrings.append(formatStart + contents + formatEnd)
                else:
                    scribus.setText(translation, item[0])
        page += 1
    if (len(newStrings) > 0):
        f = open(textfile, 'a')
        for text in newStrings:
            f.write(text)
        f.close()


def matchTranslation(contents, textfile):
    returnString = "NEEDS_TRANSLATION"
    original = False
    translation = False
    match = False
    with open(textfile) as f:
        originalText = ''
        translatedText = ''
        lines = f.readlines()
        lines = [line.strip() for line in lines]
        for line in lines:
            if line == '<!--ORIGINAL START-->':
                original = True
                continue
            if line == '<!--ORIGINAL END-->':
                original = False
                if (contents == originalText):
                    match = True
                originalText = ''
                continue
            if line == '<!--TRANSLATION START-->':
                translation = True
                continue
            if line == '<!--TRANSLATION END-->':
                translation = False
                if match:
                    if (translatedText == ''):
                        returnString = contents
                    returnString = translatedText
                    break
                translatedText = ''
                continue
            if original:
                originalText += line
                continue
            if translation:
                translatedText += line
                continue
#           scribus.messageBox("DEBUG", "unknown text!\n\n" + line, icon=0, button1=1)
    return returnString


if scribus.haveDoc():
    textfile = scribus.fileDialog('Open a translation file',
                                  filter='Text Files (*.txt);;All Files (*)')
    try:
        if textfile == '':
            raise Exception
        else:
            translateText(textfile)
    except Exception:
        scribus.messageBox('Error', traceback.format_exc(),
                           icon=0, button1=1)
else:
    scribus.messageBox('Export Error', 'You need a Document open',
                       icon=0, button1=1)