Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLDR 37 support #734

Merged
merged 5 commits into from Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .coveragerc
@@ -0,0 +1,5 @@
[report]
exclude_lines =
NotImplemented
pragma: no cover
warnings.warn
2 changes: 1 addition & 1 deletion babel/support.py
Expand Up @@ -79,7 +79,7 @@ def time(self, time=None, format='medium'):
return format_time(time, format, tzinfo=self.tzinfo, locale=self.locale)

def timedelta(self, delta, granularity='second', threshold=.85,
format='medium', add_direction=False):
format='long', add_direction=False):
"""Return a time delta according to the rules of the given locale.

>>> from datetime import timedelta
Expand Down
9 changes: 6 additions & 3 deletions babel/units.py
Expand Up @@ -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.

Expand Down Expand Up @@ -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)
6 changes: 3 additions & 3 deletions scripts/download_import_cldr.py
Expand Up @@ -13,9 +13,9 @@
from urllib import urlretrieve


URL = 'http://unicode.org/Public/cldr/36/core.zip'
FILENAME = 'cldr-core-36.zip'
FILESUM = '07279e56c1f4266d140b907ef3ec379dce0a99542303a9628562ac5fe460ba43'
URL = 'http://unicode.org/Public/cldr/37/core.zip'
FILENAME = 'cldr-core-37.zip'
FILESUM = 'ba93f5ba256a61a6f8253397c6c4b1a9b9e77531f013cc7ffa7977b5f7e4da57'
BLKSIZE = 131072


Expand Down
20 changes: 17 additions & 3 deletions scripts/import_cldr.py
Expand Up @@ -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):
Expand Down
12 changes: 11 additions & 1 deletion tests/test_support.py
Expand Up @@ -17,6 +17,7 @@
import tempfile
import unittest
import pytest
import sys
from datetime import date, datetime, timedelta

from babel import support
Expand All @@ -26,6 +27,7 @@

get_arg_spec = (inspect.getargspec if PY2 else inspect.getfullargspec)

SKIP_LGETTEXT = sys.version_info >= (3, 8)

@pytest.mark.usefixtures("os_environ")
class TranslationsTestCase(unittest.TestCase):
Expand Down Expand Up @@ -76,6 +78,7 @@ def test_upgettext(self):
self.assertEqualTypeToo(u'VohCTX', self.translations.upgettext('foo',
'foo'))

@pytest.mark.skipif(SKIP_LGETTEXT, reason="lgettext is deprecated")
def test_lpgettext(self):
self.assertEqualTypeToo(b'Voh', self.translations.lgettext('foo'))
self.assertEqualTypeToo(b'VohCTX', self.translations.lpgettext('foo',
Expand Down Expand Up @@ -105,6 +108,7 @@ def test_unpgettext(self):
self.translations.unpgettext('foo', 'foo1',
'foos1', 2))

@pytest.mark.skipif(SKIP_LGETTEXT, reason="lgettext is deprecated")
def test_lnpgettext(self):
self.assertEqualTypeToo(b'Voh1',
self.translations.lngettext('foo1', 'foos1', 1))
Expand All @@ -129,6 +133,7 @@ def test_dupgettext(self):
self.assertEqualTypeToo(
u'VohCTXD', self.translations.dupgettext('messages1', 'foo', 'foo'))

@pytest.mark.skipif(SKIP_LGETTEXT, reason="lgettext is deprecated")
def test_ldpgettext(self):
self.assertEqualTypeToo(
b'VohD', self.translations.ldgettext('messages1', 'foo'))
Expand Down Expand Up @@ -159,6 +164,7 @@ def test_dunpgettext(self):
u'VohsCTXD1', self.translations.dunpgettext('messages1', 'foo', 'foo1',
'foos1', 2))

@pytest.mark.skipif(SKIP_LGETTEXT, reason="lgettext is deprecated")
def test_ldnpgettext(self):
self.assertEqualTypeToo(
b'VohD1', self.translations.ldngettext('messages1', 'foo1', 'foos1', 1))
Expand Down Expand Up @@ -197,7 +203,11 @@ def setUp(self):
self.null_translations = support.NullTranslations(fp=fp)

def method_names(self):
return [name for name in dir(self.translations) if 'gettext' in name]
names = [name for name in dir(self.translations) if 'gettext' in name]
if SKIP_LGETTEXT:
# Remove deprecated l*gettext functions
names = [name for name in names if not name.startswith('l')]
return names

def test_same_methods(self):
for name in self.method_names():
Expand Down