feat: load localized content
This commit is contained in:
parent
b2748a6cf3
commit
e6363bea4e
|
|
@ -1,4 +1,5 @@
|
||||||
from functools import cache
|
from functools import cache
|
||||||
|
from gettext import textdomain
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Iterable, Iterator
|
from typing import Any, Iterable, Iterator
|
||||||
|
|
||||||
|
|
@ -33,13 +34,14 @@ class ContentDirectory(Content):
|
||||||
|
|
||||||
@cache # noqa: B019
|
@cache # noqa: B019
|
||||||
def __load_children(self, name: str) -> Content:
|
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():
|
if not child_path.exists():
|
||||||
raise FileNotFoundError(child_path)
|
raise FileNotFoundError(child_path)
|
||||||
|
|
||||||
if child_path.is_dir():
|
if child_path.is_dir():
|
||||||
return ContentDirectory(child_path)
|
return ContentDirectory(child_path)
|
||||||
|
|
||||||
if child_path.is_file():
|
if child_path.is_file():
|
||||||
if child_path.suffix in [".yml", ".yaml", ".json"]:
|
if child_path.suffix in [".yml", ".yaml", ".json"]:
|
||||||
return DataFile(child_path)
|
return DataFile(child_path)
|
||||||
|
|
@ -48,6 +50,15 @@ class ContentDirectory(Content):
|
||||||
|
|
||||||
raise NotImplementedError()
|
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:
|
class DataField:
|
||||||
def __init__(self, file_path: Path, value: Any) -> None:
|
def __init__(self, file_path: Path, value: Any) -> None:
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
from gettext import textdomain
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from pytest import raises
|
from pytest import raises
|
||||||
|
|
@ -45,3 +46,12 @@ def test_load_markdown(datadir: Path) -> None:
|
||||||
|
|
||||||
assert page.html == "<p>Content</p>"
|
assert page.html == "<p>Content</p>"
|
||||||
assert str(page.meta["title"]) == "Title"
|
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 == "<p>Contenu</p>"
|
||||||
|
assert str(page.meta["title"]) == "Titre"
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
---
|
||||||
|
title: Titre
|
||||||
|
---
|
||||||
|
|
||||||
|
Contenu
|
||||||
|
|
||||||
Loading…
Reference in New Issue