From 8809d57d40a1ac2a673811ca9eeaa0c611c0e48f Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Thu, 25 Dec 2025 17:44:37 +0200 Subject: [PATCH] Add `babel.core.get_cldr_version()` Fixes #1237 --- babel/core.py | 19 +++++++++++++++++++ docs/api/core.rst | 2 ++ scripts/import_cldr.py | 13 +++++++++++++ tests/test_core.py | 4 ++++ 4 files changed, 38 insertions(+) diff --git a/babel/core.py b/babel/core.py index c1522c20f..77de440e8 100644 --- a/babel/core.py +++ b/babel/core.py @@ -22,6 +22,7 @@ 'Locale', 'UnknownLocaleError', 'default_locale', + 'get_cldr_version', 'get_global', 'get_locale_identifier', 'negotiate_locale', @@ -33,6 +34,7 @@ _GLOBAL_KEY: TypeAlias = Literal[ "all_currencies", + "cldr", "currency_fractions", "language_aliases", "likely_subtags", @@ -80,6 +82,7 @@ def get_global(key: _GLOBAL_KEY) -> Mapping[str, Any]: The keys available are: - ``all_currencies`` + - ``cldr`` (metadata) - ``currency_fractions`` - ``language_aliases`` - ``likely_subtags`` @@ -1363,3 +1366,19 @@ def get_locale_identifier( lang, territory, script, variant, modifier = tup + (None,) * (5 - len(tup)) ret = sep.join(filter(None, (lang, script, territory, variant))) return f'{ret}@{modifier}' if modifier else ret + + +def get_cldr_version() -> str: + """Return the Unicode CLDR version used by this Babel installation. + + Generally, you should be able to assume that the return value of this + function is a string representing a version number, e.g. '47'. + + >>> get_cldr_version() + '47' + + .. versionadded:: 2.18 + + :rtype: str + """ + return str(get_global("cldr")["version"]) diff --git a/docs/api/core.rst b/docs/api/core.rst index 6993764b8..9f297b2a1 100644 --- a/docs/api/core.rst +++ b/docs/api/core.rst @@ -36,3 +36,5 @@ Utility Functions .. autofunction:: parse_locale .. autofunction:: get_locale_identifier + +.. autofunction:: get_cldr_version diff --git a/scripts/import_cldr.py b/scripts/import_cldr.py index bcd5898e6..ba441e84e 100755 --- a/scripts/import_cldr.py +++ b/scripts/import_cldr.py @@ -206,6 +206,19 @@ def process_data(srcdir, destdir, force=False, dump_json=False): def parse_global(srcdir, sup): global_data = {} + + with open(os.path.join(srcdir, 'dtd', 'ldml.dtd')) as dtd_file: + cldr_version_match = re.search( + r'