Skip to content

Commit

Permalink
fix: use importlib resources (#680)
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii authored Oct 5, 2024
1 parent 917e9d9 commit 4caa00b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ repos:
- id: mypy
files: plumbum
args: []
additional_dependencies: [typed-ast, types-paramiko, types-setuptools, pytest]
additional_dependencies: [typed-ast, types-paramiko, types-setuptools, pytest, importlib-resources]

- repo: https://github.com/abravalheri/validate-pyproject
rev: "v0.16"
Expand Down
38 changes: 16 additions & 22 deletions plumbum/cli/i18n.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from __future__ import annotations

import locale
import os

DIR = os.path.abspath(os.path.dirname(__file__))

# High performance method for English (no translation needed)
loc = locale.getlocale()[0]
Expand All @@ -26,33 +23,30 @@ def get_translation_for(

else:
import gettext
import sys

# If not installed with setuptools, this might not be available
try:
import pkg_resources
except ImportError:
pkg_resources = None # type: ignore[assignment]
if sys.version_info < (3, 9):
from importlib_resources import as_file, files
else:
from importlib.resources import as_file, files

def get_translation_for(package_name: str) -> gettext.NullTranslations: # type: ignore[misc]
"""Find and return gettext translation for package
(Try to find folder manually if setuptools does not exist)
"""
Find and return gettext translation for package
"""
assert loc is not None

if "." in package_name:
package_name = ".".join(package_name.split(".")[:-1])
localedir = None

if pkg_resources is None:
mydir = os.path.join(DIR, "i18n")
else:
mydir = pkg_resources.resource_filename(package_name, "i18n")
localedir = None

for localedir in mydir, None:
localefile = gettext.find(package_name, localedir, languages=[loc])
if localefile:
break
with as_file(files(package_name) / "i18n") as mydir:
for localedir in mydir, None:
localefile = gettext.find(package_name, localedir, languages=[loc])
if localefile:
break

return gettext.translation(
package_name, localedir=localedir, languages=[loc], fallback=True
)
return gettext.translation(
package_name, localedir=localedir, languages=[loc], fallback=True
)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ requires-python = ">=3.8"
dynamic = ["version"]
dependencies = [
"pywin32; platform_system=='Windows' and platform_python_implementation!='PyPy'",
"importlib_resources; python_version<'3.9'",
]
classifiers = [
"Development Status :: 5 - Production/Stable",
Expand Down

0 comments on commit 4caa00b

Please sign in to comment.