From ea84d9d5f0bd2859f3281e4dac812ae05d33f466 Mon Sep 17 00:00:00 2001 From: "Tomas R." Date: Sat, 19 Oct 2024 14:12:50 +0200 Subject: [PATCH] Replace more alternate characters in format_skeleton (#1122) Replaces some additional characters which never appear in resource files before matching the skeleton. This replicates the behaviour of ICU. --- babel/dates.py | 8 ++++++++ tests/test_dates.py | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/babel/dates.py b/babel/dates.py index 4b2cad648..30321b767 100644 --- a/babel/dates.py +++ b/babel/dates.py @@ -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 diff --git a/tests/test_dates.py b/tests/test_dates.py index 58e82add0..549929be7 100644 --- a/tests/test_dates.py +++ b/tests/test_dates.py @@ -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')