Skip to content
This repository was archived by the owner on Jan 22, 2026. It is now read-only.

Commit 7036ae2

Browse files
committed
Export virt-convert strings for translation
1 parent 8d289cb commit 7036ae2

9 files changed

Lines changed: 88 additions & 60 deletions

File tree

setup.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,12 @@ class install_lib(_install_lib):
165165
""" custom install_lib command to place locale location into library"""
166166

167167
def run(self):
168-
cmd = (("""sed -e "s,::LOCALEDIR::,%s," < virtinst/__init__.py > """ %\
169-
locale) + "%s/virtinst/__init__.py" % builddir)
170-
os.system(cmd)
168+
for initfile in [ "virtinst/__init__.py", "virtconv/__init__.py" ]:
169+
cmd = "cat %s | " % initfile
170+
cmd += """sed -e "s,::LOCALEDIR::,%s," > """ % locale
171+
cmd += "%s/%s" % (builddir, initfile)
172+
os.system(cmd)
173+
171174
_install_lib.run(self)
172175

173176

tests/pylint-virtinst.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh
22

3-
FILES="setup.py tests/ virt-install virt-image virt-clone virt-convert virtinst/ virtconv virtconv/parsers/*.py"
3+
FILES="setup.py tests/ virt-install virt-image virt-clone virt-convert virtinst/ virtconv"
44

55
# Exceptions: deliberately ignore these regex
66

virt-convert

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,76 +25,82 @@ import sys
2525
import os
2626
import logging
2727
import errno
28+
import gettext, locale
2829
from optparse import OptionGroup
2930

3031
import virtinst.cli as cli
3132
from virtinst.cli import fail
3233
import virtinst.util as util
34+
import virtconv
3335
import virtconv.formats as formats
3436
import virtconv.vmcfg as vmcfg
3537
import virtconv.diskcfg as diskcfg
3638

39+
locale.setlocale(locale.LC_ALL, '')
40+
gettext.bindtextdomain(virtconv.gettext_app, virtconv.gettext_dir)
41+
gettext.install(virtconv.gettext_app, virtconv.gettext_dir, unicode=1)
42+
3743
def parse_args():
3844
"""Parse and verify command line."""
3945
usage = "%prog [options] inputdir|input.vmx [outputdir|output.xml]"
4046
opts = cli.VirtOptionParser(usage=usage)
4147

4248
cong = OptionGroup(opts, "Conversion Options")
4349
cong.add_option("-i", "--input-format", action="store",
44-
dest="input_format", help=("Input format, e.g. 'vmx'"))
50+
dest="input_format", help=_("Input format, e.g. 'vmx'"))
4551
cong.add_option("-o", "--output-format", action="store",
4652
dest="output_format", default="virt-image",
47-
help=("Output format, e.g. 'virt-image'"))
53+
help=_("Output format, e.g. 'virt-image'"))
4854
cong.add_option("-D", "--disk-format", action="store",
49-
dest="disk_format", help=("Output disk format"))
55+
dest="disk_format", help=_("Output disk format"))
5056
opts.add_option_group(cong)
5157

5258
virg = OptionGroup(opts, "Virtualization Type Options")
5359
virg.add_option("-v", "--hvm", action="store_true", dest="fullvirt",
54-
help=("This guest should be a fully virtualized guest"))
60+
help=_("This guest should be a fully virtualized guest"))
5561
virg.add_option("-p", "--paravirt", action="store_true", dest="paravirt",
56-
help=("This guest should be a paravirtualized guest"))
62+
help=_("This guest should be a paravirtualized guest"))
5763
opts.add_option_group(virg)
5864

5965
cfgg = OptionGroup(opts, "Guest Configuration Options")
6066
cfgg.add_option("-a", "--arch", type="string", dest="arch",
6167
default=util.get_default_arch(),
62-
help=("Machine Architecture Type (i686/x86_64/ppc)"))
68+
help=_("Machine Architecture Type (i686/x86_64/ppc)"))
6369
cfgg.add_option("", "--os-type", type="string", dest="os_type",
6470
action="callback", callback=cli.check_before_store,
65-
help=("The OS type for fully virtualized guests, e.g. "
66-
"'linux', 'unix', 'windows'"))
71+
help=_("The OS type for fully virtualized guests, e.g. "
72+
"'linux', 'unix', 'windows'"))
6773
cfgg.add_option("", "--os-variant", type="string", dest="os_variant",
6874
action="callback", callback=cli.check_before_store,
69-
help=("The OS variant for fully virtualized guests, e.g. "
70-
"'fedora6', 'rhel5', 'solaris10', 'win2k', 'vista'"))
75+
help=_("The OS variant for fully virtualized guests, e.g. "
76+
"'fedora6', 'rhel5', 'solaris10', 'win2k', 'vista'"))
7177
cfgg.add_option("", "--noapic", action="store_true", dest="noapic",
72-
help=("Disables APIC for fully virtualized guest "
73-
"(overrides value in os-type/os-variant db)"),
78+
help=_("Disables APIC for fully virtualized guest "
79+
"(overrides value in os-type/os-variant db)"),
7480
default=False)
7581
cfgg.add_option("", "--noacpi", action="store_true", dest="noacpi",
76-
help=("Disables ACPI for fully virtualized guest "
77-
"(overrides value in os-type/os-variant db)"),
82+
help=_("Disables ACPI for fully virtualized guest "
83+
"(overrides value in os-type/os-variant db)"),
7884
default=False)
7985
opts.add_option_group(cfgg)
8086

8187
misc = OptionGroup(opts, "Miscellaneous Options")
8288
misc.add_option("-q", "--quiet", action="store_true", dest="quiet",
83-
help=("Don't be verbose"))
89+
help=_("Don't be verbose"))
8490
misc.add_option("-d", "--debug", action="store_true", dest="debug",
85-
help=("Print debugging information"))
91+
help=_("Print debugging information"))
8692
opts.add_option_group(misc)
8793

8894

8995
(options, args) = opts.parse_args()
9096
if len(args) < 1:
91-
opts.error(("You need to provide an input VM definition"))
97+
opts.error(_("You need to provide an input VM definition"))
9298
if len(args) > 2:
93-
opts.error(("Too many arguments provided"))
94-
99+
opts.error(_("Too many arguments provided"))
100+
95101
if (options.disk_format and
96102
options.disk_format not in diskcfg.disk_formats()):
97-
opts.error("Unknown output disk format \"%s\"" % options.disk_format)
103+
opts.error(_("Unknown output disk format \"%s\"") % options.disk_format)
98104

99105
if len(args) == 1:
100106
options.output_file = None
@@ -109,28 +115,28 @@ def parse_args():
109115
options.output_dir = os.path.dirname(os.path.realpath(args[1]))
110116

111117
if options.output_format not in formats.formats():
112-
opts.error(("Unknown output format \"%s\"" % options.output_format))
118+
opts.error(_("Unknown output format \"%s\")" % options.output_format))
113119
if options.output_format not in formats.output_formats():
114-
opts.error(("No output handler for format \"%s\""
120+
opts.error(_("No output handler for format \"%s\")"
115121
% options.output_format))
116122

117123
if not os.access(args[0], os.R_OK):
118-
opts.error("Couldn't access input argument \"%s\"\n" % args[0])
124+
opts.error(_("Couldn't access input argument \"%s\"\n") % args[0])
119125
sys.exit(1)
120126

121127
if not options.input_format:
122128
try:
123129
(args[0], options.input_format) = formats.find_input(args[0])
124130
except StandardError, e:
125-
opts.error("Couldn't determine input format for \"%s\": %s" %
131+
opts.error(_("Couldn't determine input format for \"%s\": %s") %
126132
(args[0], e))
127133
sys.exit(1)
128134

129135
if options.input_format not in formats.formats():
130-
opts.error(("Unknown input format \"%s\"" % options.input_format))
136+
opts.error(_("Unknown input format \"%s\")" % options.input_format))
131137
if options.input_format not in formats.input_formats():
132-
opts.error(("No input handler for format \"%s\""
133-
% options.input_format))
138+
opts.error(_("No input handler for format \"%s\"")
139+
% options.input_format)
134140

135141
if os.path.isdir(args[0]):
136142
(options.input_file, ignore) = formats.find_input(args[0],
@@ -164,8 +170,8 @@ def cleanup(msg, options, vmdef, paths):
164170
elif os.path.isfile(path):
165171
os.remove(path)
166172
except OSError, e:
167-
fail("Couldn't clean up output directory \"%s\": %s" %
168-
(options.output_dir, e.strerror))
173+
fail(_("Couldn't clean up output directory \"%s\": %s") %
174+
(options.output_dir, e.strerror))
169175

170176
sys.exit(1)
171177

@@ -181,10 +187,10 @@ def main():
181187
try:
182188
vmdef = inp.import_file(options.input_file)
183189
except IOError, e:
184-
fail("Couldn't import file \"%s\": %s" %
190+
fail(_("Couldn't import file \"%s\": %s") %
185191
(options.input_file, e.strerror))
186192
except Exception, e:
187-
fail("Couldn't import file \"%s\": %s" % (options.input_file, e))
193+
fail(_("Couldn't import file \"%s\": %s") % (options.input_file, e))
188194

189195
if options.paravirt:
190196
vmdef.type = vmcfg.VM_TYPE_PV
@@ -221,8 +227,8 @@ def main():
221227
logging.debug("output_file: %s" % options.output_file)
222228
logging.debug("output_dir: %s" % options.input_dir)
223229

224-
verbose(options, "Generating output in \"%s\" format to %s/" %
225-
(options.output_format, options.output_dir))
230+
verbose(options, _("Generating output in '%(format)s' format to %(dir)s/") %
231+
{"format": options.output_format, "dir": options.output_dir})
226232

227233

228234
try:
@@ -240,34 +246,35 @@ def main():
240246
format = "raw"
241247

242248
if d.path and format != "none":
243-
verbose(options, "Converting disk \"%s\" to type %s..." %
244-
(d.path, format))
249+
verbose(options, _("Converting disk '%(path)s' to type "
250+
"%(format)s...") % {"path": d.path,
251+
"format": format})
245252

246253
d.convert(options.input_dir, options.output_dir, format)
247254

248255
except OSError, e:
249-
cleanup("Couldn't convert disks: %s" % e.strerror,
256+
cleanup(_("Couldn't convert disks: %s") % e.strerror,
250257
options, vmdef, clean)
251258
except RuntimeError, e:
252-
cleanup("Couldn't convert disks: %s" % e, options, vmdef, clean)
259+
cleanup(_("Couldn't convert disks: %s") % e, options, vmdef, clean)
253260

254261
try:
255262
clean += [ options.output_file ]
256263
outp.export_file(vmdef, options.output_file)
257264
except ValueError, e:
258-
cleanup("Couldn't export to file \"%s\": %s" %
265+
cleanup(_("Couldn't export to file \"%s\": %s") %
259266
(options.output_file, e), options, vmdef, clean)
260267

261268
verbose(options, "Done.")
262269

263-
270+
264271
if __name__ == "__main__":
265272
try:
266273
main()
267274
except SystemExit, sys_e:
268275
sys.exit(sys_e.code)
269276
except KeyboardInterrupt:
270-
print >> sys.stderr, "Aborted at user request"
277+
print >> sys.stderr, _("Aborted at user request")
271278
except Exception, main_e:
272279
logging.exception(main_e)
273280
sys.exit(1)

virtconv/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121
import pkgutil
2222
import imp
2323
import os
24+
import gettext
25+
26+
gettext_dir = "::LOCALEDIR::"
27+
gettext_app = "virtinst"
28+
29+
gettext.bindtextdomain(gettext_app, gettext_dir)
30+
31+
def _gettext(msg):
32+
return gettext.dgettext(gettext_app, msg)
2433

2534
parsers_path = [os.path.join(__path__[0], "parsers/")]
2635

virtconv/diskcfg.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import os
2626
import re
2727

28+
from virtconv import _gettext as _
29+
2830
DISK_FORMAT_NONE = 0
2931
DISK_FORMAT_RAW = 1
3032
DISK_FORMAT_VMDK = 2
@@ -237,7 +239,7 @@ def convert(self, indir, outdir, output_format):
237239
out_format == DISK_FORMAT_VDISK or
238240
out_format == DISK_FORMAT_RAW or
239241
out_format == DISK_FORMAT_VMDK):
240-
raise NotImplementedError("Cannot convert to disk format %s" %
242+
raise NotImplementedError(_("Cannot convert to disk format %s") %
241243
output_format)
242244

243245
indir = os.path.normpath(os.path.abspath(indir))
@@ -250,8 +252,8 @@ def convert(self, indir, outdir, output_format):
250252
return
251253

252254
if os.path.isabs(self.path):
253-
raise NotImplementedError("Cannot convert disk with absolute"
254-
"path %s" % self.path)
255+
raise NotImplementedError(_("Cannot convert disk with absolute"
256+
" path %s") % self.path)
255257

256258
if input_in_outdir:
257259
indir = outdir

virtconv/formats.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import os
2222

23+
from virtconv import _gettext as _
24+
2325
_parsers = [ ]
2426

2527
class parser(object):
@@ -136,4 +138,4 @@ def find_input(path, format = None):
136138
if p.identify_file(os.path.join(path, cfgfile)):
137139
return (os.path.join(path, cfgfile), p.name)
138140

139-
raise StandardError("unknown format")
141+
raise StandardError(_("Unknown format"))

virtconv/parsers/virtimage.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# MA 02110-1301 USA.
1919
#
2020

21+
from virtconv import _gettext as _
2122
import virtconv.formats as formats
2223
import virtconv.vmcfg as vmcfg
2324
import virtconv.diskcfg as diskcfg
@@ -207,14 +208,15 @@ def import_file(input_file):
207208
vm = vmcfg.vm()
208209
try:
209210
config = ImageParser.parse_file(input_file)
210-
except Exception, e:
211-
raise ValueError("Couldn't import file '%s': %s" % (input_file, e))
211+
except Exception, e:
212+
raise ValueError(_("Couldn't import file '%s': %s") %
213+
(input_file, e))
212214

213215
domain = config.domain
214216
boot = domain.boots[0]
215217

216218
if not config.name:
217-
raise ValueError("No Name defined in '%s'" % input_file)
219+
raise ValueError(_("No Name defined in '%s'") % input_file)
218220
vm.name = config.name
219221
vm.memory = int(config.domain.memory / 1024)
220222
if config.descr:
@@ -233,14 +235,14 @@ def import_file(input_file):
233235
format = diskcfg.DISK_FORMAT_VMDK
234236

235237
if format is None:
236-
raise ValueError("Unable to determine disk format")
238+
raise ValueError(_("Unable to determine disk format"))
237239
devid = (bus, nr_disk)
238240
vm.disks[devid] = diskcfg.disk(bus = bus,
239241
type = diskcfg.DISK_TYPE_DISK)
240242
vm.disks[devid].format = format
241243
vm.disks[devid].path = disk.file
242244
nr_disk = nr_disk + 1
243-
245+
244246
nics = domain.interface
245247
nic_idx = 0
246248
while nic_idx in range(0, nics):
@@ -260,7 +262,7 @@ def export(vm):
260262
"""
261263

262264
if not vm.memory:
263-
raise ValueError("VM must have a memory setting")
265+
raise ValueError(_("VM must have a memory setting"))
264266

265267
# xend wants the name to match r'^[A-Za-z0-9_\-\.\:\/\+]+$', and
266268
# the schema agrees.

virtconv/parsers/vmx.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# MA 02110-1301 USA.
1919
#
2020

21+
from virtconv import _gettext as _
2122
import virtconv.formats as formats
2223
import virtconv.vmcfg as vmcfg
2324
import virtconv.diskcfg as diskcfg
@@ -212,7 +213,7 @@ def import_file(input_file):
212213
if key.startswith("ethernet"):
213214
parse_netdev_entry(vm, key, value)
214215
except:
215-
raise Exception("Syntax error at line %d: %s" %
216+
raise Exception(_("Syntax error at line %d: %s") %
216217
(line_nr + 1, line.strip()))
217218

218219
for devid, disk in vm.disks.iteritems():
@@ -226,7 +227,8 @@ def import_file(input_file):
226227
vm.disks[devid].path = None
227228

228229
if not config.get("displayname"):
229-
raise ValueError("No displayName defined in \"%s\"" % input_file)
230+
raise ValueError(_("No displayName defined in \"%s\"") %
231+
input_file)
230232
vm.name = config.get("displayname")
231233

232234
vm.memory = config.get("memsize")

0 commit comments

Comments
 (0)