From 33d8bc9ae2e9f8f27955e0b5cf460f89fb381ea4 Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Wed, 8 Jul 2020 16:54:16 +0200 Subject: [PATCH] Handle desktop files using gettext Starting from version 0.19, gettext has native capabilities to extract from, and merge back translations in desktop files. Hence, use xgettext to extract messages, and msgfmt to create a desktop file with translations; because of this, there no more need to prefix with underscore the keys to be translated. Update the gettext required version in INSTALL.md. Reviewed-by: Cole Robinson Signed-off-by: Pino Toscano --- INSTALL.md | 2 +- data/virt-manager.desktop.in | 4 ++-- setup.py | 37 +++++++++++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/INSTALL.md b/INSTALL.md index 7080fce8c..dbc3ebde9 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -30,7 +30,7 @@ A detailed dependency list can be found in Minimum version requirements of major components: - - gettext + - gettext >= 0.19 - python >= 3.4 - gtk3 >= 3.22 - libvirt-python >= 0.6.0 diff --git a/data/virt-manager.desktop.in b/data/virt-manager.desktop.in index 33765ec3b..41cc26a13 100644 --- a/data/virt-manager.desktop.in +++ b/data/virt-manager.desktop.in @@ -1,6 +1,6 @@ [Desktop Entry] -_Name=Virtual Machine Manager -_Comment=Manage virtual machines +Name=Virtual Machine Manager +Comment=Manage virtual machines Icon=virt-manager Exec=virt-manager Type=Application diff --git a/setup.py b/setup.py index 95d9a7a10..34532e124 100755 --- a/setup.py +++ b/setup.py @@ -61,7 +61,7 @@ _appdata_files = [ def _generate_meta_potfiles_in(): potfiles = "" - for ignore, filelist in _desktop_files + _appdata_files: + for ignore, filelist in _appdata_files: potfiles += "\n".join(filelist) + "\n" return potfiles @@ -107,9 +107,35 @@ class my_build_i18n(distutils.command.build.build): targetpath = os.path.join("share/locale", lang, "LC_MESSAGES") self.distribution.data_files.append((targetpath, (mo_file,))) + # Merge .in with translations using gettext + for (file_set, switch) in [(_desktop_files, "--desktop")]: + for (target, files) in file_set: + build_target = os.path.join("build", target) + if not os.path.exists(build_target): + os.makedirs(build_target) + + files_merged = [] + for f in files: + if f.endswith(".in"): + file_merged = os.path.basename(f[:-3]) + else: + file_merged = os.path.basename(f) + + file_merged = os.path.join(build_target, file_merged) + cmd = ["msgfmt", switch, "--template", f, "-d", po_dir, + "-o", file_merged] + mtime_merged = (os.path.exists(file_merged) and + os.path.getmtime(file_merged)) or 0 + mtime_file = os.path.getmtime(f) + if (mtime_merged < max_po_mtime or + mtime_merged < mtime_file): + # Only build if output is older than input (.po,.in) + self.spawn(cmd) + files_merged.append(file_merged) + self.distribution.data_files.append((target, files_merged)) + # merge .in with translation - for (file_set, switch) in [(_desktop_files, "-d"), - (_appdata_files, "-x")]: + for (file_set, switch) in [(_appdata_files, "-x")]: for (target, files) in file_set: build_target = os.path.join("build", target) if not os.path.exists(build_target): @@ -685,6 +711,11 @@ class ExtractMessages(distutils.core.Command): finally: os.unlink(potpath) + # Extract the messages from the desktop files + desktop_files = [f for sublist in _desktop_files for f in sublist[1]] + cmd = xgettext_args + ["-j", "-L", "Desktop"] + desktop_files + self.spawn(cmd) + # Extract the messages from the Python sources py_sources = list(Path("virtManager").rglob("*.py")) py_sources += list(Path("virtinst").rglob("*.py"))