Translation helper

From Scribus Wiki
Revision as of 19:18, 22 June 2018 by JLuc (talk | contribs) (Text Exporter script was the template)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

#!/usr/bin/env python
# File: - 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.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)
                    scribus.setText(translation, item[0])
        page += 1
    if (len(newStrings) > 0):
        f = open(textfile, 'a')
        for text in newStrings:

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
            if line == '<!--ORIGINAL END-->':
                original = False
                if (contents == originalText):
                    match = True
                originalText = ''
            if line == '<!--TRANSLATION START-->':
                translation = True
            if line == '<!--TRANSLATION END-->':
                translation = False
                if match:
                    if (translatedText == ''):
                        returnString = contents
                    returnString = translatedText
                translatedText = ''
            if original:
                originalText += line
            if translation:
                translatedText += line
#           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 (*)')
        if textfile == '':
            raise Exception
    except Exception:
        scribus.messageBox('Error', traceback.format_exc(),
                           icon=0, button1=1)
    scribus.messageBox('Export Error', 'You need a Document open',
                       icon=0, button1=1)