]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Adapt things to new compound pattern format
authorAarni Koskela <akx@iki.fi>
Thu, 1 Oct 2020 13:27:02 +0000 (16:27 +0300)
committerAarni Koskela <akx@iki.fi>
Fri, 2 Oct 2020 08:01:41 +0000 (11:01 +0300)
babel/units.py
scripts/import_cldr.py

index 89c491365d880c7ca95ff95012dab912befb0387..07637358c3338efe58509374a09aab3c5e62d3fc 100644 (file)
@@ -75,8 +75,10 @@ def format_unit(value, measurement_unit, length='long', format=None, locale=LC_N
     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.
 
@@ -271,6 +273,7 @@ def format_compound_unit(
     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)
index 7ea6481a2df4401dfb54a21488c1338f88cfacb2..aab2888e12c9c505206da77981530e1353c2e68d 100755 (executable)
@@ -853,9 +853,23 @@ def parse_unit_patterns(data, tree):
 
         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):