diff --git a/pint/formatting.py b/pint/formatting.py index ba9cc1d..03e5acc 100644 --- a/pint/formatting.py +++ b/pint/formatting.py @@ -13,6 +13,8 @@ from __future__ import division, unicode_literals, print_function, absolute_impo import re +from .babel_units import babel_units, babel_forms + __JOIN_REG_EXP = re.compile("\{\d*\}") @@ -100,7 +102,8 @@ _FORMATS = { def formatter(items, as_ratio=True, single_denominator=False, product_fmt=' * ', division_fmt=' / ', power_fmt='{0} ** {1}', - parentheses_fmt='({0})', exp_call=lambda x: '{0:n}'.format(x)): + parentheses_fmt='({0})', exp_call=lambda x: '{0:n}'.format(x), + locale=None, form='long', plural_form='one'): """Format a list of (name, exponent) pairs. :param items: a list of (name, exponent) pairs. @@ -126,6 +129,24 @@ def formatter(items, as_ratio=True, single_denominator=False, pos_terms, neg_terms = [], [] for key, value in sorted(items): + if locale and form and plural_form: + _key = babel_units.get(key) + if _key: + patterns = locale.unit_patterns + for _form in [form] + babel_forms: + pattern_key = _key + ':' + _form + if pattern_key in patterns: + plural = plural_form + if value <= 0: + plural = 'one' + pattern = patterns[pattern_key][plural] + key = pattern.replace('{0}', '').strip() + break + per_form = 'compound:per:' + form + if per_form in patterns: + division_fmt = patterns[per_form] + power_fmt = '{0}{1}' + exp_call = _pretty_fmt_exponent if value == 1: pos_terms.append(key) elif value > 0: @@ -177,12 +198,13 @@ def _parse_spec(spec): return result -def format_unit(unit, spec): +def format_unit(unit, spec, **kwspec): if not unit: return 'dimensionless' spec = _parse_spec(spec) - fmt = _FORMATS[spec] + fmt = dict(_FORMATS[spec]) + fmt.update(kwspec) result = formatter(unit.items(), **fmt) if spec == 'L': diff --git a/pint/quantity.py b/pint/quantity.py index 82f9aa7..0d24e51 100644 --- a/pint/quantity.py +++ b/pint/quantity.py @@ -141,6 +141,21 @@ class _Quantity(SharedRegistryObject): format(obj.magnitude, remove_custom_flags(spec)), format(obj.units, spec)).replace('\n', '') + def format_babel(self, spec='', **kwspec): + spec = spec or self.default_format + + # standard cases + if '#' in spec: + spec = spec.replace('#', '') + obj = self.to_compact() + else: + obj = self + kwspec = dict(kwspec) + kwspec['plural_form'] = kwspec['locale'].plural_form(obj.magnitude) + return '{0} {1}'.format( + format(obj.magnitude, remove_custom_flags(spec)), + obj.units.format_babel(spec, **kwspec)).replace('\n', '') + # IPython related code def _repr_html_(self): return self.__format__('H') diff --git a/pint/unit.py b/pint/unit.py index cac4220..20003a4 100644 --- a/pint/unit.py +++ b/pint/unit.py @@ -111,6 +111,21 @@ class _Unit(SharedRegistryObject): return '%s' % (format(units, spec)) + def format_babel(self, spec='', **kwspec): + spec = spec or self.default_format + + if '~' in spec: + if self.dimensionless: + return '' + units = UnitsContainer(dict((self._REGISTRY._get_symbol(key), + value) + for key, value in self._units.items())) + spec = spec.replace('~', '') + else: + units = self._units + + return '%s' % (units.format_babel(spec, **kwspec)) + # IPython related code def _repr_html_(self): return self.__format__('H') diff --git a/pint/util.py b/pint/util.py index 0374696..b0dd670 100644 --- a/pint/util.py +++ b/pint/util.py @@ -325,6 +325,9 @@ class UnitsContainer(Mapping): def __format__(self, spec): return format_unit(self, spec) + def format_babel(self, spec, **kwspec): + return format_unit(self, spec, **kwspec) + def __copy__(self): return UnitsContainer(self._d)