diff --git a/tools/convertPoToNews.py b/tools/convertPoToNews.py index 2ab002e..75571fb 100755 --- a/tools/convertPoToNews.py +++ b/tools/convertPoToNews.py @@ -1,94 +1,92 @@ #!/usr/bin/python3 # -*- coding: utf-8 -*- # # GCompris - convertPoToNews.py # # Copyright (C) 2018 Johnny Jazeix # # 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. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, see . import os import re import polib import sys import os.path if len(sys.argv) < 3: print('Usage : python3 convertPoToNews.py locale file.po.') sys.exit(1) locale = sys.argv[1] poFile = polib.pofile(sys.argv[2], encoding="utf-8") news = {} # reading all news translations from the po # storing them in a dict for entry in poFile: filename = entry.msgctxt if "fuzzy" in entry.flags: continue if not filename or not filename.find('.html'): continue originalFilename = "news/" + filename index = filename.find('.html') filename = "news/" + filename[:index] + '-' + locale + filename[index:] if os.path.isfile(filename): print(filename + " already exists, we skip it") continue context = entry.comment currentNews = {} if news.get(filename): currentNews = news.get(filename) if "title" in context: currentNews['title'] = entry.msgstr currentNews['originalFilename'] = originalFilename else: currentNews[polib.unescape(entry.msgid)] = polib.unescape(entry.msgstr) news[filename] = currentNews # for all news we have in the pot file, we create the corresponding html # translated file for currentNews in news: if not 'title' in news[currentNews] or news[currentNews]['title'] == "": print("Skip news", currentNews) continue print("Creating", currentNews) # Read in the file with open(news[currentNews]['originalFilename'], "r", encoding="utf-8") as originalFile: fileData = originalFile.read() # Replace the target string for string in news[currentNews]: if 'title' == string: matches = re.findall(r'[{% set title =]\'(.+?)\'[ %}]', fileData) for m in matches: fileData = fileData.replace('\'%s\'' % m, '\'%s\'' % news[currentNews][string]) continue else: - # We surround with ">...<" to only replace full strings between tags - # else we may replace the string in another paragraph - # and mess the translation - fileData = fileData.replace(">"+string+"<", ">"+news[currentNews][string]+"<") + # We only want to replace the first occurence of the string + fileData = fileData.replace(string, news[currentNews][string], 1) # Write the file out again with open(currentNews, 'w') as file: file.write(fileData)