diff --git a/tools/convertLastNewsToPo.py b/tools/convertLastNewsToPo.py index 0e08f95..2ebe868 100755 --- a/tools/convertLastNewsToPo.py +++ b/tools/convertLastNewsToPo.py @@ -1,75 +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(newsFolder)) if not "-" in news and "html" in news][newsCount:] # 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("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) + titleEntry = polib.POEntry(msgid=polib.escape(newsTitle), comment='news title', msgctxt=newsFileName) potFile.append(titleEntry) for line in allLines: if line[0]: # paragraph - contextEntry = polib.POEntry(msgid=polib.escape(line[0]), tcomment='paragraph', msgctxt=newsFileName) + contextEntry = polib.POEntry(msgid=polib.escape(line[0]), comment='paragraph', msgctxt=newsFileName) potFile.append(contextEntry) elif line[1]: # list item - contextEntry = polib.POEntry(msgid=polib.escape(line[1]), tcomment='list item', msgctxt=newsFileName) + contextEntry = polib.POEntry(msgid=polib.escape(line[1]), comment='list item', msgctxt=newsFileName) potFile.append(contextEntry) # saving the created po file print("saving pot to", potFilename) potFile.save(potFilename) diff --git a/tools/convertPoToNews.py b/tools/convertPoToNews.py index f8abc8f..0da2a27 100755 --- a/tools/convertPoToNews.py +++ b/tools/convertPoToNews.py @@ -1,91 +1,91 @@ #!/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.tcomment + 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: fileData = fileData.replace(string, news[currentNews][string]) # Write the file out again with open(currentNews, 'w') as file: file.write(fileData)