Skip to content

Instantly share code, notes, and snippets.

@adwaitrawat
Created August 26, 2020 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adwaitrawat/3622cada887fc7678b8db132deb8f76f to your computer and use it in GitHub Desktop.
Save adwaitrawat/3622cada887fc7678b8db132deb8f76f to your computer and use it in GitHub Desktop.
A script for adding translation fields to po files and add the translations from po files to .libretro descriptor files
import errno, glob, polib, re, os, getopt, sys
from time import strftime
directory = '.'
directory = directory.rstrip ('/')
if (directory != '') and (os.path.isdir (directory) == False):
sys.exit('Specified directory does not exist')
# Find all libretro files
files = []
for rootdir, dirnames, filenames in os.walk (directory):
files.extend (glob.glob (rootdir + '/*.libretro'))
# Define Templates and po directory name
messagetemplate = '(?<=\n)(FirmwareName=.*?\n)'
mpattern=re.compile (messagetemplate, re.DOTALL)
translationtemplate = '(?<=\n)(FirmwareName\[.*?\n)'
tpattern=re.compile (translationtemplate, re.DOTALL)
podir = 'po-libretro-cores/'
# Write POT file
pot = polib.POFile('', check_for_duplicates = True)
potcreationtime = strftime('%Y-%m-%d %H:%M%z')
pot.metadata = {
'Project-Id-Version': 'gnome-games',
'Report-Msgid-Bugs-To': '',
'POT-Creation-Date': potcreationtime,
'PO-Revision-Date': 'YEAR-MO-DA HO:MI+ZONE',
'Last-Translator': 'FULL NAME <EMAIL@ADDRESS>',
'Language-Team': 'LANGUAGE <LL@li.org>',
'MIME-Version': '1.0',
'Content-Type': 'text/plain; charset=UTF-8',
'Content-Transfer-Encoding': '8bit',
}
for langfile in files:
langfiledir = langfile.replace('.libretro', '')
langfilename = langfiledir.rpartition('/')[2]
# Create localization directories if needed
try:
os.makedirs(podir)
except OSError as e:
if e.errno != errno.EEXIST:
raise
#open desktop file
text = open (langfile, 'r').read ()
# Parse contents and add them to POT
for mblock in mpattern.findall(text):
mblock_stripped = mblock.strip ('\n')
message_comment, message_id = mblock.strip ('\n').split ('=')
potentry = polib.POEntry (
msgctxt = message_comment,
msgid = message_id,
msgstr = '',
occurrences = [(langfile, '')]
)
if message_id != '':
try:
pot.append (potentry)
except ValueError:
print('The entry already exists')
#Create po files from LINGUAS
linguas = 'po-libretro-cores/LINGUAS'
if os.path.exists (linguas):
with open (linguas) as linguas_file:
pofiles = []
for line in linguas_file.readlines ():
if '#' not in line:
pofiles.append (line.split ('\n')[0] + '.po')
print (pofiles)
for pofile in pofiles:
pofile_path = os.path.join (podir, pofile)
if not os.path.exists (pofile_path):
with open (pofile_path, 'w') as new_pofile:
pass
# Merge translations
for pofile in glob.glob (podir + '/*.po'):
lang = pofile[:-3].rsplit ('/', 1)[1]
pofilename = pofile
po = polib.pofile (pofilename)
if not po.metadata:
po.metadata = pot.metadata
po.merge (pot)
po.save (pofilename)
for langfile in files:
#open desktop file
deskfile = open (langfile, 'r')
text = deskfile.read ()
deskfile.close ()
deskfile = open (langfile, 'w')
for transblock in tpattern.findall (text):
text = text.replace (transblock, '')
# Parse PO files
for pofile in sorted (glob.glob (podir + '/*.po'), reverse = True):
lang = pofile[:-3].rsplit ('/', 1)[1]
pofilename = pofile
po = polib.pofile (pofilename)
for entry in po.translated_entries ():
if entry.msgid in text:
origmessage = '\n' + entry.msgctxt + '=' + entry.msgid + '\n'
origandtranslated = '\n' + entry.msgctxt + '=' + entry.msgid + '\n' + entry.msgctxt + '[' + lang + ']=' + entry.msgstr + '\n'
text = text.replace (origmessage, origandtranslated)
deskfile.write (text)
deskfile.close ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment