Skip to content

Commit e5e1870

Browse files
committed
Adjust catalog commands to support forwarding options to babel
The following tweaks the catalog wrapper commands used to call babel's `pofile`/`mofile` functions to support forwarding any keyword argument. This can provide flexibility to callers which may wish to utilize additional options provided by babel, without needing to add an explicit option in the catalog wrapper calls. A compatibility adjustment was added to `dump_po` to help map an original argument `line_width` to babel's `width` argument. This is to ensure callers which still use the `line_width` argument can appropriately configure the babel call to impose the provided width. Signed-off-by: James Knight <[email protected]>
1 parent feece86 commit e5e1870

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

sphinx_intl/basic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ def update(locale_dir, pot_dir, languages, line_width=76):
6161
status['update'] += 1
6262
click.echo('Update: {0} +{1}, -{2}'.format(
6363
po_file, len(added), len(deleted)))
64-
c.dump_po(po_file, cat, line_width)
64+
c.dump_po(po_file, cat, width=line_width)
6565
else:
6666
status['notchanged'] += 1
6767
click.echo('Not Changed: {0}'.format(po_file))
6868
else: # new po file
6969
status['create'] += 1
7070
click.echo('Create: {0}'.format(po_file))
7171
cat_pot.locale = lang
72-
c.dump_po(po_file, cat_pot, line_width)
72+
c.dump_po(po_file, cat_pot, width=line_width)
7373

7474
return status
7575

sphinx_intl/catalog.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
from babel.messages import pofile, mofile
77

88

9-
def load_po(filename):
9+
def load_po(filename, **kwargs):
1010
"""read po/pot file and return catalog object
1111
1212
:param unicode filename: path to po/pot file
13+
:param kwargs: keyword arguments to forward to babel's read_po call
1314
:return: catalog object
1415
"""
1516
# pre-read to get charset
@@ -20,27 +21,36 @@ def load_po(filename):
2021
# To decode lines by babel, read po file as binary mode and specify charset for
2122
# read_po function.
2223
with io.open(filename, 'rb') as f: # FIXME: encoding VS charset
23-
return pofile.read_po(f, charset=charset)
24+
return pofile.read_po(f, charset=charset, **kwargs)
2425

2526

26-
def dump_po(filename, catalog, line_width=76):
27+
def dump_po(filename, catalog, **kwargs):
2728
"""write po/pot file from catalog object
2829
2930
:param unicode filename: path to po file
3031
:param catalog: catalog object
31-
:param line_width: maximum line wdith of po files
32+
:param kwargs: keyword arguments to forward to babel's write_po call; also
33+
accepts a deprecated `line_width` option to forward to
34+
write_po's `width` option
3235
:return: None
3336
"""
3437
dirname = os.path.dirname(filename)
3538
if not os.path.exists(dirname):
3639
os.makedirs(dirname)
3740

41+
# (compatibility) line_width was the original argument used to forward
42+
# line width hints into write_po's `width` argument; if provided,
43+
# set/override the width value
44+
if 'line_width' in kwargs:
45+
kwargs['width'] = kwargs['line_width']
46+
del kwargs['line_width']
47+
3848
# Because babel automatically encode strings, file should be open as binary mode.
3949
with io.open(filename, 'wb') as f:
40-
pofile.write_po(f, catalog, line_width)
50+
pofile.write_po(f, catalog, **kwargs)
4151

4252

43-
def write_mo(filename, catalog):
53+
def write_mo(filename, catalog, **kwargs):
4454
"""write mo file from catalog object
4555
4656
:param unicode filename: path to mo file
@@ -51,7 +61,7 @@ def write_mo(filename, catalog):
5161
if not os.path.exists(dirname):
5262
os.makedirs(dirname)
5363
with io.open(filename, 'wb') as f:
54-
mofile.write_mo(f, catalog)
64+
mofile.write_mo(f, catalog, **kwargs)
5565

5666

5767
def translated_entries(catalog):

0 commit comments

Comments
 (0)