Skip to content

Commit

Permalink
🐛 using importlib to load translation files
Browse files Browse the repository at this point in the history
We copied the 'avoid to do that' example in the documentation
https://setuptools.pypa.io/en/latest/userguide/datafiles.html#accessing-data-files-at-runtime
  • Loading branch information
vokimon committed Nov 7, 2024
1 parent 7489ebf commit 9f0f83f
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions legaltexts/translate.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from yamlns import ns
from pathlib import Path
from importlib.resources import files as package_files

def build_translations():
if hasattr(build_translations, "translations"):
return build_translations.translations
translations = ns()
for translation_file in (Path(__file__).parent/'i18n').glob('*.yaml'):
for translation_file in package_files('legaltexts.i18n').iterdir():
if translation_file.suffix != '.yaml': continue
lang = translation_file.stem
translations[lang] = ns.load(translation_file)
translations[lang] = ns.loads(translation_file.read_text())
build_translations.translations = translations
print(build_translations.translations)
return build_translations.translations

def tr(lang, text, *args, **kwds):
Expand Down

0 comments on commit 9f0f83f

Please sign in to comment.