Skip to content

Commit 462444f

Browse files
committed
Adapt things to new compound pattern format
1 parent 3e6d9a7 commit 462444f

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

babel/units.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ def format_unit(value, measurement_unit, length='long', format=None, locale=LC_N
7575
u'12 metri'
7676
>>> format_unit(15.5, 'length-mile', locale='fi_FI')
7777
u'15,5 mailia'
78-
>>> format_unit(1200, 'pressure-inch-hg', locale='nb')
79-
u'1\\xa0200 tommer kvikks\\xf8lv'
78+
>>> format_unit(1200, 'pressure-millimeter-ofhg', locale='nb')
79+
u'1\\xa0200 millimeter kvikks\\xf8lv'
80+
>>> format_unit(270, 'ton', locale='en')
81+
u'270 tons'
8082
8183
Number formats may be overridden with the ``format`` parameter.
8284
@@ -271,6 +273,7 @@ def format_compound_unit(
271273
else: # Bare denominator
272274
formatted_denominator = format_decimal(denominator_value, format=format, locale=locale)
273275

274-
per_pattern = locale._data["compound_unit_patterns"].get("per", {}).get(length, "{0}/{1}")
276+
# TODO: this doesn't support "compound_variations" (or "prefix"), and will fall back to the "x/y" representation
277+
per_pattern = locale._data["compound_unit_patterns"].get("per", {}).get(length, {}).get("compound", "{0}/{1}")
275278

276279
return per_pattern.format(formatted_numerator, formatted_denominator)

scripts/import_cldr.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -853,9 +853,23 @@ def parse_unit_patterns(data, tree):
853853

854854
for unit in elem.findall('compoundUnit'):
855855
unit_type = unit.attrib['type']
856-
compound_patterns.setdefault(unit_type, {})[unit_length_type] = (
857-
_text(unit.find('compoundUnitPattern'))
858-
)
856+
compound_unit_info = {}
857+
compound_variations = {}
858+
for child in unit.getchildren():
859+
if child.tag == "unitPrefixPattern":
860+
compound_unit_info['prefix'] = _text(child)
861+
elif child.tag == "compoundUnitPattern":
862+
compound_variations[None] = _text(child)
863+
elif child.tag == "compoundUnitPattern1":
864+
compound_variations[child.attrib.get('count')] = _text(child)
865+
if compound_variations:
866+
compound_variation_values = set(compound_variations.values())
867+
if len(compound_variation_values) == 1:
868+
# shortcut: if all compound variations are the same, only store one
869+
compound_unit_info['compound'] = next(iter(compound_variation_values))
870+
else:
871+
compound_unit_info['compound_variations'] = compound_variations
872+
compound_patterns.setdefault(unit_type, {})[unit_length_type] = compound_unit_info
859873

860874

861875
def parse_date_fields(data, tree):

0 commit comments

Comments
 (0)