#! /usr/bin/python -O # -*- coding: iso-8859-1 -*- # this script iterates through the 'html' and 'lib' directories # looking for php scripts that contain a include_once("xxx_po.inc") # line and __() functions. It creates/appends to the corresponding # "xxx_po.inc" file in the 'lang' subdirectory and places the # i18n strings into the file in the proper format. # # usage: genpopo [-v] [-f] # -v: verbose, print duplicate terms that could be moved to common_po # -f: force, overwrite existing translated files, otherwise append # INC_HEADER = """\ ') f.close() f = open("en/"+po,'w') f.write(INC_HEADER) for term in lang[po].keys(): f.write("\n") f.write('$_t["en"]["%s"] = "%s";\n' % (term, term)) f.write("\n"); f.write("?>"); f.close() else: # need to leave existing file intact, and only append on terms that are new # incre = re.compile('^include_once\("en\/(.*)"\);') mapre = re.compile('^\$_t\["en"\]\["(.*)"\].*$') for po in lang.keys(): got_match = False print "Updating %s..." % po try: f = open(po, 'r') new_file = 0 except: new_file = 1 if not new_file: contents = f.readlines() f.close() # strip off beginning/ending empty lines # while contents[0] == '': del contents[0] while contents[-1] in ['', "\n", "?>", "?>\n", "\n?>"]: del contents[-1] for line in contents: match = incre.search(line) if match: got_match = True if not got_match: f = open(po,'w') f.write("".join(contents)) f.write('\ninclude_once(\"en/%s\");\n' % po) f.write("\n?>"); f.close() else: f = open(po,'w') f.write(INC_HEADER) f.write('\ninclude_once(\"en/%s\");\n' % po) f.write('\n?>') f.close() # first read in file contents so we can hash what already exists # try: f = open('en/'+po, 'r') new_file = 0 except: new_file = 1 existing_terms = [] if not new_file: contents = f.readlines() f.close() # strip off beginning/ending empty lines # while contents[0] == '': del contents[0] while contents[-1] in ['', "\n", "?>", "?>\n", "\n?>"]: del contents[-1] # next, collect existing terms # for line in contents: match = mapre.search(line) if match: existing_terms.append(match.group(1)) # now append any new terms to EOF # f = open('en/'+po, 'w') if not new_file: f.write("".join(contents)) else: f.write(INC_HEADER) for term in lang[po].keys(): if term not in existing_terms: f.write("\n"); f.write('$_t["en"]["%s"] = "%s";\n' % (term, term)) f.write("\n?>"); f.close() # Print out warnings for unused and little-used common entries. # for key in common.keys(): if common[key] == 1: print "Warning: common entry '%s' is only used once." % key for key in common.keys(): if common[key] == 0: print "Warning: unused common entry '%s'." % key # vim: ts=2 sw=2 noet ft=python