[virt-manager PATCH v3 4/8] Handle desktop files using gettext
Pino Toscano
ptoscano at redhat.com
Wed Jul 8 06:54:50 UTC 2020
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; this requires two changes:
- the keys in desktop files do not need to be prefixed with underscore
anymore
- add a LINGUAS file in the meta-po subdirectory (empty for now), as
otherwise msgfmt does not recognize the directory as containing
translations
Update the gettext required version in INSTALL.md.
Signed-off-by: Pino Toscano <ptoscano at redhat.com>
---
INSTALL.md | 2 +-
data/virt-manager.desktop.in | 4 ++--
meta-po/LINGUAS | 0
setup.py | 42 +++++++++++++++++++++++++++++++++---
4 files changed, 42 insertions(+), 6 deletions(-)
create mode 100644 meta-po/LINGUAS
diff --git a/INSTALL.md b/INSTALL.md
index 7080fce8..dbc3ebde 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 33765ec3..41cc26a1 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/meta-po/LINGUAS b/meta-po/LINGUAS
new file mode 100644
index 00000000..e69de29b
diff --git a/setup.py b/setup.py
index d1202c68..95e7b16e 100755
--- a/setup.py
+++ b/setup.py
@@ -62,7 +62,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
@@ -118,9 +118,35 @@ class my_build_i18n(distutils.command.build.build):
if po_mtime > max_po_mtime:
max_po_mtime = po_mtime
+ # 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):
@@ -703,7 +729,12 @@ class ExtractMessages(distutils.core.Command):
po_dir = "meta-po"
potfiles = _generate_meta_potfiles_in()
potpath = "meta-po/POTFILES.in"
+ pot_file = os.path.join(po_dir, "virt-manager-meta.pot")
+
+ xgettext_args = self.common_xgettext_args + \
+ ["-o", pot_file, "--package-name=virt-manager-meta"]
+ # First extract the messages from the AppStream sources
try:
open(potpath, "w").write(potfiles)
cmd = ["intltool-update", "-p", "-g", "virt-manager-meta"]
@@ -714,6 +745,11 @@ class ExtractMessages(distutils.core.Command):
finally:
os.unlink(potpath)
+ # Then 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)
+
def run(self):
self._extract_main()
self._extract_meta()
--
2.26.2
More information about the virt-tools-list
mailing list