diff --git a/jwebsite/content.py b/jwebsite/content.py index 49c32e4..cc9db0b 100644 --- a/jwebsite/content.py +++ b/jwebsite/content.py @@ -1,4 +1,5 @@ from functools import cache +from gettext import textdomain from pathlib import Path from typing import Any, Iterable, Iterator @@ -33,13 +34,14 @@ class ContentDirectory(Content): @cache # noqa: B019 def __load_children(self, name: str) -> Content: - child_path = self.path / name + child_path = self.__get_localized_path(self.path / name) if not child_path.exists(): raise FileNotFoundError(child_path) if child_path.is_dir(): return ContentDirectory(child_path) + if child_path.is_file(): if child_path.suffix in [".yml", ".yaml", ".json"]: return DataFile(child_path) @@ -48,6 +50,15 @@ class ContentDirectory(Content): raise NotImplementedError() + @staticmethod + def __get_localized_path(path: Path) -> Path: + domain = textdomain() + localized_path = path.with_name(f"{path.stem}-{domain}{path.suffix}") + if not localized_path.exists(): + return path + + return localized_path + class DataField: def __init__(self, file_path: Path, value: Any) -> None: diff --git a/tests/test_content.py b/tests/test_content.py index fc3ad03..85a9016 100644 --- a/tests/test_content.py +++ b/tests/test_content.py @@ -1,3 +1,4 @@ +from gettext import textdomain from pathlib import Path from pytest import raises @@ -45,3 +46,12 @@ def test_load_markdown(datadir: Path) -> None: assert page.html == "

Content

" assert str(page.meta["title"]) == "Title" + + +def test_localized_content(datadir: Path) -> None: + content = ContentDirectory(datadir) + textdomain("fr") + page = content.load(Path("page.md")) + + assert page.html == "

Contenu

" + assert str(page.meta["title"]) == "Titre" diff --git a/tests/test_content/page-fr.md b/tests/test_content/page-fr.md new file mode 100644 index 0000000..3dce283 --- /dev/null +++ b/tests/test_content/page-fr.md @@ -0,0 +1,6 @@ +--- +title: Titre +--- + +Contenu +