diff --git a/Messages.sh b/Messages.sh index 461a983..f4ca5ef 100644 --- a/Messages.sh +++ b/Messages.sh @@ -1,3 +1,3 @@ #! /usr/bin/env bash pybabel extract --add-comments=notes -F babel.cfg -o $podir/gcompris-net.pot . -python3 ./tools/convertLastNewsToPo.py && msgcat $podir/gcompris-net.pot gcompris-news.pot > gcompris-merged.pot && rm gcompris-news.pot && mv gcompris-merged.pot $podir/gcompris-net.pot +python3 ./tools/convertLastNewsToPo.py diff --git a/tools/convertLastNewsToPo.py b/tools/convertLastNewsToPo.py index 2ba52ee..0e08f95 100755 --- a/tools/convertLastNewsToPo.py +++ b/tools/convertLastNewsToPo.py @@ -1,43 +1,75 @@ #!/usr/bin/python3 # -*- coding: utf-8 -*- +# +# GCompris - convertLastNewsToPo.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 newsCount = -1 if len(sys.argv) > 1: newsCount = -int(sys.argv[1]) print("Fetching last", -newsCount, "news") +newsFolder="news/" # get all the news in news folder and only get the last N ones that are not translated -lastNews = [news for news in sorted(os.listdir("news")) if not "-" in news and "html" in news][newsCount:] +lastNews = [news for news in sorted(os.listdir(newsFolder)) if not "-" in news and "html" in news][newsCount:] -potFile = polib.pofile('', encoding="utf-8") -potFile.metadata = { - 'Project-Id-Version': '1.0', - 'Report-Msgid-Bugs-To': 'gcompris-devel@kde.org', - 'MIME-Version': '1.0', - 'Content-Type': 'text/plain; charset=utf-8', - 'Content-Transfer-Encoding': '8bit', -} +# directly append to $podir/gcompris-net.pot if it exists, else create a new +# pot file +potFilename = '' +if os.path.isfile(os.path.expandvars('$podir/gcompris-net.pot')): + potFilename = os.path.expandvars('$podir/gcompris-net.pot') +potFile = polib.pofile(potFilename, encoding="utf-8") +if potFilename == '': + potFilename = 'gcompris-news.pot' + potFile.metadata = { + 'Project-Id-Version': '1.0', + 'Report-Msgid-Bugs-To': 'gcompris-devel@kde.org', + 'MIME-Version': '1.0', + 'Content-Type': 'text/plain; charset=utf-8', + 'Content-Transfer-Encoding': '8bit', + } for newsFileName in lastNews: - print(newsFileName) - file = open("news/"+newsFileName, encoding='utf-8') - fileContent = file.readlines() - newsText = [] - for line in fileContent: - if 'set title' in line: - newsTitle = re.search("\'(.+?)\'", line).group(1) - if not "{%" in line: # remove jinja2 expressions - newsText.append(line) + print("working on", newsFolder+newsFileName) + file = open(newsFolder+newsFileName, encoding='utf-8') + fileContent = file.read() + + # get the title separately + newsTitle = re.search("{% set title = \'(.+?)\'", fileContent).group(1) + + #catch all

: (?s)<[p>]*>(.*?)]+> in group 0 + #catch all li: <[li class="puce">]*>(.*?)]+> in group 1 + allLines = re.findall("(?s)<[p>]*>(.*?)]+>|<[li class=\"puce\">]*>(.*?)]+>", fileContent) + titleEntry = polib.POEntry(msgid=polib.escape(newsTitle), tcomment='news title', msgctxt=newsFileName) potFile.append(titleEntry) - text = ''.join(newsText) - contextEntry = polib.POEntry(msgid=polib.escape(text), tcomment='news text', msgctxt=newsFileName) - potFile.append(contextEntry) + for line in allLines: + if line[0]: # paragraph + contextEntry = polib.POEntry(msgid=polib.escape(line[0]), tcomment='paragraph', msgctxt=newsFileName) + potFile.append(contextEntry) + elif line[1]: # list item + contextEntry = polib.POEntry(msgid=polib.escape(line[1]), tcomment='list item', msgctxt=newsFileName) + potFile.append(contextEntry) # saving the created po file -potFile.save('gcompris-news.pot') +print("saving pot to", potFilename) +potFile.save(potFilename) diff --git a/tools/convertPoToNews.py b/tools/convertPoToNews.py index 1ab79fa..f680f80 100755 --- a/tools/convertPoToNews.py +++ b/tools/convertPoToNews.py @@ -1,56 +1,87 @@ #!/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 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.tcomment currentNews = {} if news.get(filename): currentNews = news.get(filename) if "title" in context: currentNews['title'] = entry.msgstr - currentNews['header'] = "{% extends \"template/onenews.html\" %}\n" \ - "{% set title = '" + entry.msgstr + "' %}\n" \ - "{% set withlongcontent = 0 %}\n" \ - "{% block content %} \n" + currentNews['originalFilename'] = originalFilename else: - currentNews["newsContent"] = polib.unescape(entry.msgstr) + currentNews[polib.unescape(entry.msgid)] = polib.unescape(entry.msgstr) news[filename] = currentNews - -FOOTER = "{% endblock %}" +# for all news we have in the pot file, we create the corresponding html +# translated file for currentNews in news: - if news[currentNews]['title'] == "" or news[currentNews]['newsContent'] == "": + if not 'title' in news[currentNews] or news[currentNews]['title'] == "": print("Skip news", currentNews) continue - + print("Creating", currentNews) - file = open(currentNews, "w", encoding="utf-8") - file.write(news[currentNews]['header']) - file.write(news[currentNews]['newsContent']) - file.write(FOOTER+"\n") + + # 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: + fileData = fileData.replace(string, news[currentNews][string]) + + # Write the file out again + with open(currentNews, 'w') as file: + file.write(fileData)