aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYuri Chornoivan <yurchor@ukr.net>2013-12-07 19:19:32 +0200
committerYuri Chornoivan <yurchor@ukr.net>2013-12-07 19:19:45 +0200
commitce325bbc1cf4e07582fe6fb1fff6206e47652cee (patch)
treeb6ca4d8dcb450ab029fd90e2d04de31546540055
parentfea2e50c181c7376b1b9c37570375373b7c74bbc (diff)
downloadtools-ce325bbc1cf4e07582fe6fb1fff6206e47652cee.tar
tools-ce325bbc1cf4e07582fe6fb1fff6206e47652cee.tar.gz
tools-ce325bbc1cf4e07582fe6fb1fff6206e47652cee.tar.bz2
tools-ce325bbc1cf4e07582fe6fb1fff6206e47652cee.tar.xz
tools-ce325bbc1cf4e07582fe6fb1fff6206e47652cee.zip
Add scripts for desktop files
-rw-r--r--desktop/README43
-rw-r--r--desktop/desktoppo.py116
-rw-r--r--desktop/transconverter.py84
3 files changed, 243 insertions, 0 deletions
diff --git a/desktop/README b/desktop/README
new file mode 100644
index 00000000..30f8f092
--- /dev/null
+++ b/desktop/README
@@ -0,0 +1,43 @@
+The scripts may be useful for those who want to
+organize sustainable translation process of .desktop files translation.
+
+Prerequisites:
+
+Installed python-polib package:
+
+# urpmi python-polib
+
+I desktoppo.py:
+
+Usage:
+
+1. Copy the script into any folder with .desktop files.
+2. Run
+ python desktoppo.py
+3. Script will create .pot's for all .desktop's in the folder. Translate pot's and
+ rename the translations into <lang_code>.po
+4. Run
+ python desktoppo.py
+ once more to add your translations to .desktop files.
+
+If you ever need to update something (.desktop's were updated or you found a typo,
+etc.) just run
+ python desktoppo.py
+
+Limitations:
+The script is useful only for Unicode translations.
+
+II transconverter.py is a one-time converter for existing
+translations.
+
+Run it as
+
+python transconverter.py
+
+in any folder with desktop files on any level of the filesystem subtree.
+
+ * Both of these files are free software. They come without any warranty, to
+ * the extent permitted by applicable law. You can redistribute it
+ * and/or modify them under the terms of the Do What The Fuck You Want
+ * To Public License, Version 2, as published by Sam Hocevar. See
+ * http://sam.zoy.org/wtfpl/COPYING for more details.
diff --git a/desktop/desktoppo.py b/desktop/desktoppo.py
new file mode 100644
index 00000000..ed6dd98d
--- /dev/null
+++ b/desktop/desktoppo.py
@@ -0,0 +1,116 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import errno, glob, polib, re, os, getopt, sys
+from time import strftime
+
+def usage():
+ print '\nUsage: python %s [OPTION]' %os.path.basename(sys.argv[0])
+ print ' generate pot catalogs and updates po files for desktop resources in the specified directory'
+ print 'Options: -h, --help : usage'
+ print ' -d <directory>, --directory <directory> : directory with desktop files'
+ sys.exit(2)
+try:
+ opts, args = getopt.getopt(sys.argv[1:], "hd:", ["help", "directory="])
+except getopt.GetoptError:
+ usage() # print help information and exit
+
+directory='.'
+for o,a in opts:
+ if o in ("-h", "--help"):
+ usage()
+ if o in ("-d", "--directory"):
+ directory=a
+
+directory = directory.rstrip('/')
+
+if (directory != '') and (os.path.isdir(directory) == False):
+ sys.exit('Specified directory does not exist')
+
+# Find all desktop files
+files = []
+for rootdir, dirnames, filenames in os.walk(directory):
+ files.extend(glob.glob(rootdir + "/*.desktop"))
+
+# Define Templates and po directory name
+messagetemplate='(?<=\n)Name=.*?\n|Comment=.*?\n'
+mpattern=re.compile(messagetemplate,re.DOTALL)
+translationtemplate='(?<=\n)Name\[.*?\n|Comment\[.*?\n'
+tpattern=re.compile(translationtemplate,re.DOTALL)
+podir = 'po'
+
+# Write POT file
+pot = polib.POFile('',check_for_duplicates=True)
+potcreationtime = strftime('%Y-%m-%d %H:%M%z')
+pot.metadata = {
+ 'Project-Id-Version': 'Mageia desktop files translation',
+ 'Report-Msgid-Bugs-To': 'i18n-discuss@ml.mageia.org',
+ '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('.desktop', '')
+ langfilename = langfiledir.rpartition('/')[2]
+ # Create localization directories if needed
+ try:
+ os.makedirs(podir)
+ except OSError, 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.decode('utf-8'),
+ msgstr = '',
+ occurrences=[(langfile,'')]
+ )
+ if message_id != '':
+ try:
+ pot.append(potentry)
+ except ValueError:
+ print 'The entry already exists'
+pot.save('po/mageia_desktop.pot')
+
+# Merge translations
+for pofile in glob.glob(podir + '/*.po'):
+ lang = pofile[:-3].rsplit('/',1)[1]
+ pofilename = pofile
+ po = polib.pofile(pofilename)
+ 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.encode('utf-8') in text:
+ origmessage = ('\n' + entry.msgctxt + '=' + entry.msgid + '\n').encode('utf-8')
+ origandtranslated = ('\n' + entry.msgctxt + '=' + entry.msgid + '\n' + entry.msgctxt + '[' + lang + ']=' + entry.msgstr + '\n').encode('utf-8')
+ text = text.replace(origmessage, origandtranslated)
+
+ deskfile.write(text)
+ deskfile.close()
+
diff --git a/desktop/transconverter.py b/desktop/transconverter.py
new file mode 100644
index 00000000..f6130844
--- /dev/null
+++ b/desktop/transconverter.py
@@ -0,0 +1,84 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import errno, glob, polib, re, os, getopt, sys
+from time import strftime
+
+def usage():
+ print '\nUsage: python %s [OPTION]' %os.path.basename(sys.argv[0])
+ print ' extract existing translations from desktop file'
+ print 'Options: -h, --help : usage'
+ sys.exit(2)
+try:
+ opts, args = getopt.getopt(sys.argv[1:], "h:", ["help"])
+except getopt.GetoptError:
+ usage() # print help information and exit
+
+directory='.'
+for o in opts:
+ if o in ("-h", "--help"):
+ usage()
+
+# Find all desktop files
+files = []
+for rootdir, dirnames, filenames in os.walk(directory):
+ files.extend(glob.glob(rootdir + "/*.desktop"))
+
+# Define Templates and po directory name
+translationtemplate='(?<=\n)Name\[.*?\n|Comment\[.*?\n'
+tpattern=re.compile(translationtemplate,re.DOTALL)
+podir = 'po'
+
+pocreationtime = strftime('%Y-%m-%d %H:%M%z')
+
+for langfile in files:
+ langfiledir = langfile.replace('.desktop', '')
+ langfilename = langfiledir.rpartition('/')[2]
+ # Create localization directories if needed
+ try:
+ os.makedirs(podir)
+ except OSError, e:
+ if e.errno != errno.EEXIST:
+ raise
+ #open desktop file
+ text = open(langfile,"r").read()
+
+ # Parse contents and add them to PO
+ for tblock in tpattern.findall(text):
+ message_comment, locale_message = tblock.strip('\n').split('[')
+ lang_code, msg_str = locale_message.split(']=')
+ msgidtemplate='(?<=\n)' + message_comment + '=.*?\n'
+ msgidpattern=re.compile(msgidtemplate,re.DOTALL)
+ msgids = msgidpattern.findall(text)
+ if msgids:
+ msg_id = msgids[0].split('=')[1].strip('\n')
+ poentry = polib.POEntry(
+ msgctxt = message_comment,
+ msgid = msg_id.decode('utf-8'),
+ msgstr = msg_str.decode('utf-8'),
+ occurrences=[(langfile,'')]
+ )
+ pofilename = podir + '/' + lang_code + '.po'
+ if not os.path.isfile(pofilename):
+ # Create PO file
+ po = polib.POFile()
+ po.metadata = {
+ 'Project-Id-Version': 'Mageia desktop files translation',
+ 'Report-Msgid-Bugs-To': 'mageia-i18n@mageia.org',
+ 'POT-Creation-Date': pocreationtime,
+ 'PO-Revision-Date': pocreationtime,
+ 'Last-Translator': 'Duffy Duck <d_duck@nowhere.net>',
+ 'Language-Team': lang_code + ' <mageia-' + lang_code + '@ml.mageia.org>',
+ 'MIME-Version': '1.0',
+ 'Content-Type': 'text/plain; charset=UTF-8',
+ 'Content-Transfer-Encoding': '8bit',
+ }
+ po.save(pofilename)
+ po = polib.pofile(pofilename, check_for_duplicates=True)
+ if msg_id != '':
+ try:
+ po.append(poentry)
+ except ValueError:
+ print 'The entry already exists, skipping it'
+ po.save(pofilename)
+