Skip to content

Commit 688d131

Browse files
authored
Merge pull request #734 from akx/cldr-37
CLDR 37 support
2 parents 5315341 + 2b615f7 commit 688d131

6 files changed

Lines changed: 43 additions & 11 deletions

File tree

.coveragerc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[report]
2+
exclude_lines =
3+
NotImplemented
4+
pragma: no cover
5+
warnings.warn

babel/support.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def time(self, time=None, format='medium'):
7979
return format_time(time, format, tzinfo=self.tzinfo, locale=self.locale)
8080

8181
def timedelta(self, delta, granularity='second', threshold=.85,
82-
format='medium', add_direction=False):
82+
format='long', add_direction=False):
8383
"""Return a time delta according to the rules of the given locale.
8484
8585
>>> from datetime import timedelta

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/download_import_cldr.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
from urllib import urlretrieve
1414

1515

16-
URL = 'http://unicode.org/Public/cldr/36/core.zip'
17-
FILENAME = 'cldr-core-36.zip'
18-
FILESUM = '07279e56c1f4266d140b907ef3ec379dce0a99542303a9628562ac5fe460ba43'
16+
URL = 'http://unicode.org/Public/cldr/37/core.zip'
17+
FILENAME = 'cldr-core-37.zip'
18+
FILESUM = 'ba93f5ba256a61a6f8253397c6c4b1a9b9e77531f013cc7ffa7977b5f7e4da57'
1919
BLKSIZE = 131072
2020

2121

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):

tests/test_support.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import tempfile
1818
import unittest
1919
import pytest
20+
import sys
2021
from datetime import date, datetime, timedelta
2122

2223
from babel import support
@@ -26,6 +27,7 @@
2627

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

30+
SKIP_LGETTEXT = sys.version_info >= (3, 8)
2931

3032
@pytest.mark.usefixtures("os_environ")
3133
class TranslationsTestCase(unittest.TestCase):
@@ -76,6 +78,7 @@ def test_upgettext(self):
7678
self.assertEqualTypeToo(u'VohCTX', self.translations.upgettext('foo',
7779
'foo'))
7880

81+
@pytest.mark.skipif(SKIP_LGETTEXT, reason="lgettext is deprecated")
7982
def test_lpgettext(self):
8083
self.assertEqualTypeToo(b'Voh', self.translations.lgettext('foo'))
8184
self.assertEqualTypeToo(b'VohCTX', self.translations.lpgettext('foo',
@@ -105,6 +108,7 @@ def test_unpgettext(self):
105108
self.translations.unpgettext('foo', 'foo1',
106109
'foos1', 2))
107110

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

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

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

199205
def method_names(self):
200-
return [name for name in dir(self.translations) if 'gettext' in name]
206+
names = [name for name in dir(self.translations) if 'gettext' in name]
207+
if SKIP_LGETTEXT:
208+
# Remove deprecated l*gettext functions
209+
names = [name for name in names if not name.startswith('l')]
210+
return names
201211

202212
def test_same_methods(self):
203213
for name in self.method_names():

0 commit comments

Comments
 (0)