u'12 metri'
>>> format_unit(15.5, 'length-mile', locale='fi_FI')
u'15,5 mailia'
- >>> format_unit(1200, 'pressure-inch-hg', locale='nb')
- u'1\\xa0200 tommer kvikks\\xf8lv'
+ >>> format_unit(1200, 'pressure-millimeter-ofhg', locale='nb')
+ u'1\\xa0200 millimeter kvikks\\xf8lv'
+ >>> format_unit(270, 'ton', locale='en')
+ u'270 tons'
Number formats may be overridden with the ``format`` parameter.
else: # Bare denominator
formatted_denominator = format_decimal(denominator_value, format=format, locale=locale)
- per_pattern = locale._data["compound_unit_patterns"].get("per", {}).get(length, "{0}/{1}")
+ # TODO: this doesn't support "compound_variations" (or "prefix"), and will fall back to the "x/y" representation
+ per_pattern = locale._data["compound_unit_patterns"].get("per", {}).get(length, {}).get("compound", "{0}/{1}")
return per_pattern.format(formatted_numerator, formatted_denominator)
for unit in elem.findall('compoundUnit'):
unit_type = unit.attrib['type']
- compound_patterns.setdefault(unit_type, {})[unit_length_type] = (
- _text(unit.find('compoundUnitPattern'))
- )
+ compound_unit_info = {}
+ compound_variations = {}
+ for child in unit.getchildren():
+ if child.tag == "unitPrefixPattern":
+ compound_unit_info['prefix'] = _text(child)
+ elif child.tag == "compoundUnitPattern":
+ compound_variations[None] = _text(child)
+ elif child.tag == "compoundUnitPattern1":
+ compound_variations[child.attrib.get('count')] = _text(child)
+ if compound_variations:
+ compound_variation_values = set(compound_variations.values())
+ if len(compound_variation_values) == 1:
+ # shortcut: if all compound variations are the same, only store one
+ compound_unit_info['compound'] = next(iter(compound_variation_values))
+ else:
+ compound_unit_info['compound_variations'] = compound_variations
+ compound_patterns.setdefault(unit_type, {})[unit_length_type] = compound_unit_info
def parse_date_fields(data, tree):