Refactor and simplify the handling of translations according to the catalog split: - use msgmerge to merge the translations from the update catalog, instead of intltools - split the parts in different functions Remove the writing of the temporary POTFILES.in, as it is no more needed now. Signed-off-by: Pino Toscano <ptoscano@xxxxxxxxxx> --- setup.py | 69 ++++++++++++++++---------------------------------------- 1 file changed, 20 insertions(+), 49 deletions(-) diff --git a/setup.py b/setup.py index b75c1417..945853cb 100755 --- a/setup.py +++ b/setup.py @@ -66,28 +66,6 @@ def _generate_meta_potfiles_in(): return potfiles -def _generate_potfiles_in(): - def find(dirname, ext): - ret = [] - for root, ignore, filenames in os.walk(dirname): - for filename in fnmatch.filter(filenames, ext): - ret.append(os.path.join(root, filename)) - ret.sort(key=lambda s: s.lower()) - return ret - - potfiles = "" - potfiles += "\n".join(find("virtManager", "*.py")) + "\n\n" - potfiles += "\n".join(find("virtinst", "*.py")) + "\n\n" - - potfiles += _generate_meta_potfiles_in() - potfiles += "\n" - - potfiles += "\n".join(["[type: gettext/glade]" + f for - f in find("ui", "*.ui")]) + "\n\n" - - return potfiles - - class my_build_i18n(distutils.command.build.build): """ Add our desktop files to the list, saves us having to track setup.cfg @@ -102,35 +80,23 @@ class my_build_i18n(distutils.command.build.build): pass def run(self): - potfiles = _generate_potfiles_in() - potpath = "po/POTFILES.in" - - try: - print("Writing %s" % potpath) - open(potpath, "w").write(potfiles) - self._run() - finally: - print("Removing %s" % potpath) - os.unlink(potpath) - - def _run(self): - # Borrowed from python-distutils-extra - po_dir = "po" - - # Update po(t) files and print a report - # We have to change the working dir to the po dir for intltool - cmd = ["intltool-update", - (self.merge_po and "-r" or "-p"), "-g", "virt-manager"] + if self.merge_po: + self._msgmerge("po", "virt-manager.pot") + self._msgmerge("meta-po", "virt-manager-meta.pot") + self._msgfmt("po", "virt-manager") + self._merge_in("meta-po") + + def _msgmerge(self, po_dir, pot_name): + pot_file = os.path.join(po_dir, pot_name) + for po_file in glob.glob("%s/*.po" % po_dir): + cmd = ["msgmerge", "--previous", "-o", po_file, po_file, pot_file] + self.spawn(cmd) - wd = os.getcwd() - os.chdir("po") - self.spawn(cmd) - os.chdir(wd) - max_po_mtime = 0 + def _msgfmt(self, po_dir, po_name): for po_file in glob.glob("%s/*.po" % po_dir): lang = os.path.basename(po_file[:-3]) mo_dir = os.path.join("build", "mo", lang, "LC_MESSAGES") - mo_file = os.path.join(mo_dir, "virt-manager.mo") + mo_file = os.path.join(mo_dir, po_name + ".mo") if not os.path.exists(mo_dir): os.makedirs(mo_dir) @@ -138,14 +104,19 @@ class my_build_i18n(distutils.command.build.build): po_mtime = os.path.getmtime(po_file) mo_mtime = (os.path.exists(mo_file) and os.path.getmtime(mo_file)) or 0 - if po_mtime > max_po_mtime: - max_po_mtime = po_mtime if po_mtime > mo_mtime: self.spawn(cmd) targetpath = os.path.join("share/locale", lang, "LC_MESSAGES") self.distribution.data_files.append((targetpath, (mo_file,))) + def _merge_in(self, po_dir): + max_po_mtime = 0 + for po_file in glob.glob("%s/*.po" % po_dir): + po_mtime = os.path.getmtime(po_file) + if po_mtime > max_po_mtime: + max_po_mtime = po_mtime + # merge .in with translation for (file_set, switch) in [(_desktop_files, "-d"), (_appdata_files, "-x")]: -- 2.26.2