Skip to content

Commit b36d521

Browse files
author
Huanxuan Ao
committed
Fix i18n supports in commom
I checked all the files in openstackclient/common and fixed the missing i18n supprots. Change-Id: Id7f76a24aae663f5832ef9bcf1bd5a6b7081af24 Partial-bug: #1574965
1 parent 5293bb1 commit b36d521

8 files changed

Lines changed: 83 additions & 50 deletions

File tree

openstackclient/common/availability_zone.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,20 @@ def get_parser(self, prog_name):
9292
'--compute',
9393
action='store_true',
9494
default=False,
95-
help='List compute availability zones')
95+
help=_('List compute availability zones'),
96+
)
9697
parser.add_argument(
9798
'--network',
9899
action='store_true',
99100
default=False,
100-
help='List network availability zones')
101+
help=_('List network availability zones'),
102+
)
101103
parser.add_argument(
102104
'--volume',
103105
action='store_true',
104106
default=False,
105-
help='List volume availability zones')
107+
help=_('List volume availability zones'),
108+
)
106109
parser.add_argument(
107110
'--long',
108111
action='store_true',

openstackclient/common/configuration.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import six
1717

1818
from openstackclient.common import command
19+
from openstackclient.i18n import _
1920

2021
REDACTED = "<redacted>"
2122

@@ -31,13 +32,13 @@ def get_parser(self, prog_name):
3132
dest="mask",
3233
action="store_true",
3334
default=True,
34-
help="Attempt to mask passwords (default)",
35+
help=_("Attempt to mask passwords (default)"),
3536
)
3637
mask_group.add_argument(
3738
"--unmask",
3839
dest="mask",
3940
action="store_false",
40-
help="Show password in clear text",
41+
help=_("Show password in clear text"),
4142
)
4243
return parser
4344

openstackclient/common/extension.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from openstackclient.common import command
2121
from openstackclient.common import utils
22+
from openstackclient.i18n import _
2223

2324

2425
class ListExtension(command.Lister):
@@ -30,27 +31,32 @@ def get_parser(self, prog_name):
3031
'--compute',
3132
action='store_true',
3233
default=False,
33-
help='List extensions for the Compute API')
34+
help=_('List extensions for the Compute API'),
35+
)
3436
parser.add_argument(
3537
'--identity',
3638
action='store_true',
3739
default=False,
38-
help='List extensions for the Identity API')
40+
help=_('List extensions for the Identity API'),
41+
)
3942
parser.add_argument(
4043
'--network',
4144
action='store_true',
4245
default=False,
43-
help='List extensions for the Network API')
46+
help=_('List extensions for the Network API'),
47+
)
4448
parser.add_argument(
4549
'--volume',
4650
action='store_true',
4751
default=False,
48-
help='List extensions for the Block Storage API')
52+
help=_('List extensions for the Block Storage API'),
53+
)
4954
parser.add_argument(
5055
'--long',
5156
action='store_true',
5257
default=False,
53-
help='List additional fields in output')
58+
help=_('List additional fields in output'),
59+
)
5460
return parser
5561

5662
def take_action(self, parsed_args):

openstackclient/common/limits.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from openstackclient.common import command
2121
from openstackclient.common import utils
22+
from openstackclient.i18n import _
2223
from openstackclient.identity import common as identity_common
2324

2425

@@ -33,30 +34,33 @@ def get_parser(self, prog_name):
3334
dest="is_absolute",
3435
action="store_true",
3536
default=False,
36-
help="Show absolute limits")
37+
help=_("Show absolute limits"),
38+
)
3739
type_group.add_argument(
3840
"--rate",
3941
dest="is_rate",
4042
action="store_true",
4143
default=False,
42-
help="Show rate limits")
44+
help=_("Show rate limits"),
45+
)
4346
parser.add_argument(
4447
"--reserved",
4548
dest="is_reserved",
4649
action="store_true",
4750
default=False,
48-
help="Include reservations count [only valid with --absolute]")
51+
help=_("Include reservations count [only valid with --absolute]"),
52+
)
4953
parser.add_argument(
5054
'--project',
5155
metavar='<project>',
52-
help='Show limits for a specific project (name or ID)'
53-
' [only valid with --absolute]',
56+
help=_('Show limits for a specific project (name or ID)'
57+
' [only valid with --absolute]'),
5458
)
5559
parser.add_argument(
5660
'--domain',
5761
metavar='<domain>',
58-
help='Domain the project belongs to (name or ID)'
59-
' [only valid with --absolute]',
62+
help=_('Domain the project belongs to (name or ID)'
63+
' [only valid with --absolute]'),
6064
)
6165
return parser
6266

openstackclient/common/module.py

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

2121
from openstackclient.common import command
2222
from openstackclient.common import utils
23+
from openstackclient.i18n import _
2324

2425

2526
class ListCommand(command.Lister):
@@ -61,7 +62,7 @@ def get_parser(self, prog_name):
6162
'--all',
6263
action='store_true',
6364
default=False,
64-
help='Show all modules that have version information',
65+
help=_('Show all modules that have version information'),
6566
)
6667
return parser
6768

openstackclient/common/parseractions.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ def __call__(self, parser, namespace, values, metavar=None):
8787
if '=' in kv:
8888
params.update([kv.split('=', 1)])
8989
else:
90-
msg = ("Expected key=value pairs separated by comma, "
91-
"but got: %s" % (str(kv)))
90+
msg = _("Expected key=value pairs separated by comma, "
91+
"but got: %s") % (str(kv))
9292
raise argparse.ArgumentTypeError(msg)
9393

9494
# Check key validation
@@ -139,12 +139,13 @@ def __call__(self, parser, namespace, values, option_string=None):
139139
if int(range[0]) <= int(range[1]):
140140
setattr(namespace, self.dest, (int(range[0]), int(range[1])))
141141
else:
142-
msg = "Invalid range, %s is not less than %s" % \
143-
(range[0], range[1])
142+
msg = (_("Invalid range, %(range0)s is not "
143+
"less than %(range1)s")
144+
% {'range0': range[0], 'range1': range[1]})
144145
raise argparse.ArgumentError(self, msg)
145146
else:
146147
# Too many values
147-
msg = "Invalid range, too many values"
148+
msg = _("Invalid range, too many values")
148149
raise argparse.ArgumentError(self, msg)
149150

150151

@@ -158,5 +159,6 @@ def __call__(self, parser, namespace, values, option_string=None):
158159
if int(values) >= 0:
159160
setattr(namespace, self.dest, values)
160161
else:
161-
msg = "%s expected a non-negative integer" % (str(option_string))
162+
msg = (_("%s expected a non-negative integer")
163+
% (str(option_string)))
162164
raise argparse.ArgumentTypeError(msg)

openstackclient/common/quota.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from openstackclient.common import command
2323
from openstackclient.common import utils
24+
from openstackclient.i18n import _
2425

2526

2627
# List the quota items, map the internal argument name to the option
@@ -84,27 +85,27 @@ def get_parser(self, prog_name):
8485
parser.add_argument(
8586
'project',
8687
metavar='<project/class>',
87-
help='Set quotas for this project or class (name/ID)',
88+
help=_('Set quotas for this project or class (name/ID)'),
8889
)
8990
parser.add_argument(
9091
'--class',
9192
dest='quota_class',
9293
action='store_true',
9394
default=False,
94-
help='Set quotas for <class>',
95+
help=_('Set quotas for <class>'),
9596
)
9697
for k, v in self._build_options_list():
9798
parser.add_argument(
9899
'--%s' % v,
99100
metavar='<%s>' % v,
100101
dest=k,
101102
type=int,
102-
help='New value for the %s quota' % v,
103+
help=_('New value for the %s quota') % v,
103104
)
104105
parser.add_argument(
105106
'--volume-type',
106107
metavar='<volume-type>',
107-
help='Set quotas for a specific <volume-type>',
108+
help=_('Set quotas for a specific <volume-type>'),
108109
)
109110
return parser
110111

@@ -187,22 +188,22 @@ def get_parser(self, prog_name):
187188
'project',
188189
metavar='<project/class>',
189190
nargs='?',
190-
help='Show quotas for this project or class (name or ID)',
191+
help=_('Show quotas for this project or class (name or ID)'),
191192
)
192193
type_group = parser.add_mutually_exclusive_group()
193194
type_group.add_argument(
194195
'--class',
195196
dest='quota_class',
196197
action='store_true',
197198
default=False,
198-
help='Show quotas for <class>',
199+
help=_('Show quotas for <class>'),
199200
)
200201
type_group.add_argument(
201202
'--default',
202203
dest='default',
203204
action='store_true',
204205
default=False,
205-
help='Show default quotas for <project>'
206+
help=_('Show default quotas for <project>')
206207
)
207208
return parser
208209

openstackclient/common/utils.py

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from oslo_utils import importutils
2525

2626
from openstackclient.common import exceptions
27+
from openstackclient.i18n import _
2728

2829

2930
def find_resource(manager, name_or_id, **kwargs):
@@ -90,13 +91,19 @@ def find_resource(manager, name_or_id, **kwargs):
9091
# of client exceptions.
9192
except Exception as ex:
9293
if type(ex).__name__ == 'NotFound':
93-
msg = "No %s with a name or ID of '%s' exists." % \
94-
(manager.resource_class.__name__.lower(), name_or_id)
95-
raise exceptions.CommandError(msg)
94+
msg = _("No %(resource)s with a name or ID "
95+
"of '%(name_or_id)s' exists.")
96+
raise exceptions.CommandError(
97+
msg % {'resource': manager.resource_class.__name__.lower(),
98+
'name_or_id': name_or_id}
99+
)
96100
if type(ex).__name__ == 'NoUniqueMatch':
97-
msg = "More than one %s exists with the name '%s'." % \
98-
(manager.resource_class.__name__.lower(), name_or_id)
99-
raise exceptions.CommandError(msg)
101+
msg = _("More than one %(resource)s exists with "
102+
"the name '%(name_or_id)s'.")
103+
raise exceptions.CommandError(
104+
msg % {'resource': manager.resource_class.__name__.lower(),
105+
'name_or_id': name_or_id}
106+
)
100107
else:
101108
pass
102109

@@ -107,7 +114,7 @@ def find_resource(manager, name_or_id, **kwargs):
107114
return resource
108115
else:
109116
# we found no match, report back this error:
110-
msg = "Could not find resource %s" % name_or_id
117+
msg = _("Could not find resource %s") % name_or_id
111118
raise exceptions.CommandError(msg)
112119

113120

@@ -152,7 +159,7 @@ def get_field(item, field):
152159
else:
153160
return getattr(item, field)
154161
except Exception:
155-
msg = "Resource doesn't have field %s" % field
162+
msg = _("Resource doesn't have field %s") % field
156163
raise exceptions.CommandError(msg)
157164

158165

@@ -234,14 +241,17 @@ def sort_items(items, sort_str):
234241
if ':' in sort_key:
235242
sort_key, direction = sort_key.split(':', 1)
236243
if not sort_key:
237-
msg = "empty string is not a valid sort key"
244+
msg = _("empty string is not a valid sort key")
238245
raise exceptions.CommandError(msg)
239246
if direction not in ['asc', 'desc']:
240247
if not direction:
241248
direction = "empty string"
242-
msg = ("%s is not a valid sort direction for sort key %s, "
243-
"use asc or desc instead" % (direction, sort_key))
244-
raise exceptions.CommandError(msg)
249+
msg = _("%(direction)s is not a valid sort direction for "
250+
"sort key %(sort_key)s, use asc or desc instead")
251+
raise exceptions.CommandError(
252+
msg % {'direction': direction,
253+
'sort_key': sort_key}
254+
)
245255
if direction == 'desc':
246256
reverse = True
247257
items.sort(key=lambda item: get_field(item, sort_key),
@@ -273,9 +283,13 @@ def get_client_class(api_name, version, version_map):
273283
try:
274284
client_path = version_map[str(version)]
275285
except (KeyError, ValueError):
276-
msg = "Invalid %s client version '%s'. must be one of: %s" % (
277-
(api_name, version, ', '.join(list(version_map.keys()))))
278-
raise exceptions.UnsupportedVersion(msg)
286+
msg = _("Invalid %(api_name)s client version '%(version)s'. "
287+
"must be one of: %(version_map)s")
288+
raise exceptions.UnsupportedVersion(
289+
msg % {'api_name': api_name,
290+
'version': version,
291+
'version_map': ', '.join(list(version_map.keys()))}
292+
)
279293

280294
return importutils.import_class(client_path)
281295

@@ -391,9 +405,10 @@ def get_password(stdin, prompt=None, confirm=True):
391405
return first_pass
392406
print("The passwords entered were not the same")
393407
except EOFError: # Ctl-D
394-
raise exceptions.CommandError("Error reading password.")
395-
raise exceptions.CommandError("There was a request to be prompted for a"
396-
" password and a terminal was not detected.")
408+
raise exceptions.CommandError(_("Error reading password."))
409+
raise exceptions.CommandError(_("There was a request to be prompted "
410+
"for a password and a terminal was "
411+
"not detected."))
397412

398413

399414
def read_blob_file_contents(blob_file):
@@ -402,7 +417,7 @@ def read_blob_file_contents(blob_file):
402417
blob = file.read().strip()
403418
return blob
404419
except IOError:
405-
msg = "Error occurred trying to read from file %s"
420+
msg = _("Error occurred trying to read from file %s")
406421
raise exceptions.CommandError(msg % blob_file)
407422

408423

0 commit comments

Comments
 (0)