replaced old hdf language checker with new script for updating english po files

This commit is contained in:
lars 2006-11-28 14:23:09 +00:00
parent edeba13812
commit 27b61640bc
2 changed files with 101 additions and 111 deletions

View File

@ -1,111 +0,0 @@
#!/usr/bin/env python2.4
# $Id$
# compare translated language files with the master (english) language files
# it is useful for finding incomplete translations
# Copyright (c) 02006 sense.lab <>
# License: This script is distributed under the terms of version 2
# of the GNU GPL. See the LICENSE file included with the package.
# Parameter: LANGUAGE (e.g. "de")
import os, sys
import neo_cgi, neo_util
except ImportError, errMsg:
sys.stderr.write("Could not import the python clearsilver module: %s\n" % (errMsg,))
sys.stderr.write("Maybe you should try 'apt-get install python-clearsilver'?\n")
SUFFIX = ".hdf"
def getHDFkeys(hdf):
import types
if type(hdf.dump()) == types.NoneType: return []
return [e.split(" = ",1)[0].strip() for e in hdf.dump().splitlines()]
def compareFiles(langFile1, langFile2):
## check for existence of files
for filename in (langFile2, langFile1):
if not os.path.exists(filename):
sys.stderr.write("could not find language file: %s\n" % filename)
## check if the files are the same
if langFile1 == langFile2:
sys.stderr.write("cannot compare the language file with itself: %s\n" % langFile1)
print "Comparing '%s' and '%s':" % (langFile1, langFile2)
hdf1 = neo_util.HDF()
hdf2 = neo_util.HDF()
hdfKeys1 = getHDFkeys(hdf1)
hdfKeys2 = getHDFkeys(hdf2)
## check for empty language data sets
for (keys, filename) in ((hdfKeys1, langFile1), (hdfKeys2, langFile2)):
if len(keys) == 0:
sys.stderr.write("\tthe language file is empty: %s\n" % filename)
## check for missing keys (only part of the first (master) file)
for key in hdfKeys1:
if key.endswith(".Link.Abs") or key.endswith(".Link.Prot") or key.endswith(".Link.Rel"):
## ignore link attributes
elif not key in hdfKeys2:
print "\tmissing key: %s" % key
## check for superfluous keys (only part of the second file)
for key in hdfKeys2:
if key.endswith(".Link.Abs") or key.endswith(".Link.Prot") or key.endswith(".Link.Rel"):
print "\tyou should not override link attributes: %s" % key
elif not key in hdfKeys1:
print "\tsuperfluous key: %s" % key
def find_lang_file(arg, dirname, fnames):
lfile = os.path.join(dirname, arg["fname"])
if os.path.isfile(lfile):
############ main #############
if "__main__" != __name__:
args = sys.argv[1:]
## check parameters
if len(args) != 1:
sys.stderr.write("invalid number of parameters: a language name is required\n")
language = args[0]
if language == DEFAULT_LANGUAGE:
sys.stderr.write("you must specify a language different from the default (%s)!\n" % DEFAULT_LANGUAGE)
obj = { "fname":"%s%s" % (DEFAULT_LANGUAGE, SUFFIX),
"list": [] }
os.path.walk(os.getcwd(), find_lang_file, obj)
for langFile in obj["list"]:
refLang = langFile.replace("/%s%s" % (DEFAULT_LANGUAGE, SUFFIX), "/%s%s" % (language, SUFFIX))
compareFiles(langFile, refLang)

scripts/ Executable file
View File

@ -0,0 +1,101 @@
#!/usr/bin/env python2.4
import os
import sys
import, translate.convert.pot2po,
except ImportError, errMsg:
sys.stderr.write("Failed to import a python module of the 'translate' package!\n")
sys.stderr.write("Please install the appropriate files - for debian just do 'apt-get install translate-toolkit'.\n")
sys.stderr.write("\tOriginal error message: %s\n\n" % errMsg)
import neo_cgi, neo_util
except ImportError, errMsg:
sys.stderr.write("Failed to import a python module of the 'clearsilver' package!\n")
sys.stderr.write("Please install the appropriate files - for debian just do 'apt-get install python-clearsilver'.\n")
sys.stderr.write("\tOriginal error message: %s\n\n" % errMsg)
LANGUAGE_FILE = 'language.hdf'
GETTEXT_DOMAIN = 'cryptobox-server'
LANG_DIR = 'intl'
## the complete list of languages wastes a lot of space - for now we use only a few
#ALL_LANGUAGES = "af aka am ar bn ca cs da de el en es et eu fa fi fr fur gl he hi hr hu hy is it ja ka kg ko ku lt lv mr ms mt nb ne nl nn ns pa pl pt ru sl sr st sv tr uk ve vi xh".split(" ")
ALL_LANGUAGES = "cs da de en eo es fi fr hu it ja nl pl pt ru sl sv".split(" ")
# --------------=-=-=- functions -=-=-=--------------------
def process_language_file(hdf_file, po_dir, textDomain):
## prepare hdf
if not os.path.isfile(hdf_file) or not os.access(hdf_file, os.R_OK):
sys.stderr.write("Unable to read the hdf file: %s" % hdf_file)
if not os.path.isdir(po_dir):
pot_file = os.path.join(po_dir, "%s.pot" % textDomain)
hdf = neo_util.HDF()
## update pot
if not os.path.isfile(pot_file):
sys.stdout.write("Creating: %s\n" % pot_file)
pot ="utf-8")
pot.updateheader(add=True, Project_Id_Version='CryptoBox-Server 0.3', pot_creation_date=True, language_team='sense.lab <>', Report_Msgid_Bugs_To=MAIL_ADDRESS, encoding='utf-8', Plural_Forms=['nplurals=2','plural=(n != 1)'])
#TODO: somehow we need 'updateheaderplural'
sys.stdout.write("Loading: %s\n" % pot_file)
pot =
def walk_hdf(prefix, node):
def addPoItem(hdf_node):
for (key,value) in hdf_node.attrs():
if key == "LINK": return
if not hdf_node.value(): return
item = pot.findunit(hdf_node.value())
if not item:
item = pot.addsourceunit(hdf_node.value())
item.addlocation("%s%s" % (prefix,
while node:
new_prefix = prefix + + '.'
new_prefix = prefix
walk_hdf(new_prefix, node.child())
node =
p =
for ld in ALL_LANGUAGES:
if not os.path.isdir(os.path.join(po_dir,ld)): os.mkdir(os.path.join(po_dir, ld))
po_file = os.path.join(po_dir, ld, "%s.po" % textDomain)
if not os.path.isfile(po_file):
translate.convert.pot2po.convertpot(file(pot_file), file(po_file,'w'), None)
po2_file = po_file + '.new'
translate.convert.pot2po.convertpot(file(pot_file), file(po2_file,'w'), file(po_file))
os.rename(po2_file, po_file)
## compile po file
mo_file = po_file[:-3] + '.mo', file(mo_file,'w'), file(pot_file))
# ----------------=-=-=- main -=-=-=-----------------------
if __name__ == "__main__":
## the project directory is the parent of the directory of this script
PROJECT_DIR = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]),os.path.pardir))
process_language_file(os.path.join(PROJECT_DIR,'templates',LANGUAGE_FILE), os.path.join(PROJECT_DIR,LANG_DIR), GETTEXT_DOMAIN)
for root,dirs,files in os.walk(os.path.join(PROJECT_DIR,'plugins')):
if LANGUAGE_FILE in files:
process_language_file(os.path.join(root,LANGUAGE_FILE), os.path.join(root,LANG_DIR), "%s-feature-%s" % (GETTEXT_DOMAIN,os.path.basename(root)))