Skip to content

Commit

Permalink
Replace more alternate characters in format_skeleton
Browse files Browse the repository at this point in the history
Replaces some additional characters which never appear
in resource files before matching the skeleton. This
replicates the behaviour of ICU.
  • Loading branch information
tomasr8 committed Aug 22, 2024
1 parent cf7d223 commit bfb5e3d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions babel/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1890,6 +1890,14 @@ def match_skeleton(skeleton: str, options: Iterable[str], allow_different_fields

if 'z' in skeleton and not any('z' in option for option in options):
skeleton = skeleton.replace('z', 'v')
if 'k' in skeleton and not any('k' in option for option in options):
skeleton = skeleton.replace('k', 'H')
if 'K' in skeleton and not any('K' in option for option in options):
skeleton = skeleton.replace('K', 'h')
if 'a' in skeleton and not any('a' in option for option in options):
skeleton = skeleton.replace('a', '')
if 'b' in skeleton and not any('b' in option for option in options):
skeleton = skeleton.replace('b', '')

get_input_field_width = dict(t[1] for t in tokenize_pattern(skeleton) if t[0] == "field").get
best_skeleton = None
Expand Down
13 changes: 13 additions & 0 deletions tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,19 @@ def test_format_skeleton(timezone_getter):
assert (dates.format_skeleton('EHm', dt, tzinfo=timezone_getter('Asia/Bangkok'), locale='th') == 'อา. 22:30 น.')


@pytest.mark.parametrize(('skeleton', 'expected'), [
('Hmz', 'Hmv'),
('kmv', 'Hmv'),
('Kmv', 'hmv'),
('Hma', 'Hm'),
('Hmb', 'Hm'),
('zkKab', 'vHh'),
])
def test_match_skeleton_alternate_characters(skeleton, expected):
# https://github.com/unicode-org/icu/blob/5e22f0076ec9b55056cd8a84e9ef370632f44174/icu4j/main/core/src/main/java/com/ibm/icu/text/DateIntervalInfo.java#L1090-L1102
assert dates.match_skeleton(skeleton, (expected,)) == expected


def test_format_timedelta():
assert (dates.format_timedelta(timedelta(weeks=12), locale='en_US')
== '3 months')
Expand Down

0 comments on commit bfb5e3d

Please sign in to comment.