Subversion Repositories svn LFS-FR


Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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

#    Template Translator v0.2
#    Regex-based automatic translator for common strings in gettext files.
#    Copyright 2016-2017 roptat <>
#    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 3 of the License, or
#    (at your option) any later version.

import sys
import re
import polib
import os.path

class TemplateTranslator:
        def __init__(self, files):
                self.regexps = []
                self.files = files
        def append(self, regexp, translation):
                self.regexps.append([regexp, translation])

        def convert(self, entry, regexp, template):
                m = regexp.match(entry.msgid)
                # do not modify anything if the translation is already correct
                #if m and ("fuzzy" in entry.flags or not entry.msgstr):
                if m:
                        msgstr = template
                                msgstr = msgstr.replace("#1",
                                msgstr = msgstr.replace('#2',
                                msgstr = msgstr.replace('#3',
                                msgstr = msgstr.replace('#4',
                        entry.msgstr = msgstr
                        if "fuzzy" in entry.flags:

        def translate_one(self, filename, language):
                realname = language + '/' + filename
                if not os.path.exists(realname):
                        print(realname, 'does not exist.')
                po = polib.pofile(realname)
                for entry in po:
                        for reg in self.regexps:
                                if language in reg[1]:
                                        self.convert(entry, reg[0], reg[1][language])
        def translate(self, languages):
                number = len(self.files)
                current = 1
                for filename in self.files:
                        print('Traitement du fichier ', current, '/', number, '     \r',
                                        end="", flush=True),
                        current = current + 1
                        for language in languages:
                                self.translate_one(filename, language)