|
41 | 41 | import babel.numbers |
42 | 42 | import babel.support |
43 | 43 | import babel.units |
44 | | -import pkg_resources |
| 44 | +import importlib.resources |
45 | 45 |
|
46 | 46 | from cms import config |
47 | 47 | from cmscommon.datetime import utc |
@@ -282,13 +282,25 @@ def get_translations() -> dict[str, Translation]: |
282 | 282 | """ |
283 | 283 | result = {DEFAULT_TRANSLATION.identifier: DEFAULT_TRANSLATION} |
284 | 284 |
|
285 | | - for lang_code in sorted(pkg_resources.resource_listdir("cms.locale", "")): |
286 | | - mofile_path = os.path.join(lang_code, "LC_MESSAGES", "cms.mo") |
287 | | - if pkg_resources.resource_exists("cms.locale", mofile_path): |
288 | | - with pkg_resources.resource_stream("cms.locale", mofile_path) as f: |
289 | | - t = Translation(lang_code, f) |
290 | | - logger.info("Found translation %s", t.identifier) |
291 | | - result[t.identifier] = t |
| 285 | + try: |
| 286 | + locale_pkg = importlib.resources.files("cms.locale") |
| 287 | + for lang_dir in locale_pkg.iterdir(): |
| 288 | + if lang_dir.is_dir(): |
| 289 | + lang_code = lang_dir.name |
| 290 | + try: |
| 291 | + mofile_path = lang_dir / "LC_MESSAGES" / "cms.mo" |
| 292 | + with mofile_path.open("rb") as f: |
| 293 | + t = Translation(lang_code, f) |
| 294 | + logger.info("Found translation %s", t.identifier) |
| 295 | + result[t.identifier] = t |
| 296 | + except Exception: |
| 297 | + logger.warning( |
| 298 | + "Failed to load translation for %s", |
| 299 | + lang_code, |
| 300 | + exc_info=True, |
| 301 | + ) |
| 302 | + except Exception: |
| 303 | + logger.warning("Failed to scan locale directory", exc_info=True) |
292 | 304 |
|
293 | 305 | return result |
294 | 306 |
|
|
0 commit comments