Skip to content

Commit 878c590

Browse files
committed
add partial locale code
1 parent 487f068 commit 878c590

File tree

9 files changed

+66
-3
lines changed

9 files changed

+66
-3
lines changed

.github/workflows/package-publish.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ jobs:
2222
- name: Install dependencies
2323
run: |
2424
python -m pip install --upgrade pip
25-
pip install build
25+
pip install build babel
26+
- name: Generate mo files
27+
run: pybabel compile -D hhd -d ./i18n
28+
- name: Copy mo files
29+
run: cp -R ./i18n/* ./src/hhd/i18n
2630
- name: Build package
2731
run: python -m build -s
2832
- name: Publish package

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ venv
33
*.egg-info
44
dist
55
.vscode
6-
notebooks
6+
notebooks
7+
*.mo

MANIFEST.in

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
recursive-include src/hhd *.yml
22
recursive-include src/hhd *.yaml
3+
recursive-include src/hhd *.mo
34
graft usr/
45
graft src/hhd/http/static
56
include src/hhd/http/index.html

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ overlay = "hhd.plugins.overlay:autodetect"
4242
# display = "hhd.plugins.display:autodetect"
4343

4444
[project.entry-points."hhd.i18n"]
45+
hhd = "hhd.i18n:locales"
4546

4647
[project.entry-points."babel.extractors"]
4748
hhd_yaml = "hhd.contrib.i18n:extract_hhd_yaml"

src/hhd/__main__.py

+16
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,22 @@ def main():
223223
logger.error(f"No plugins started, exiting...")
224224
return
225225

226+
# Load locales
227+
locales = []
228+
for register in pkg_resources.iter_entry_points("hhd.i18n"):
229+
locales.extend(register.resolve()())
230+
locales.sort(key=lambda x: x["priority"])
231+
232+
if locales:
233+
lstr = "Loaded the following locales:\n"
234+
for locale in locales:
235+
lstr += (
236+
f" - {locale['domain']} ({locale['priority']}): {locale['path']}\n"
237+
)
238+
logger.info(lstr[:-1])
239+
else:
240+
logger.info("No locales found.")
241+
226242
# Open plugins
227243
lock = RLock()
228244
cond = Condition(lock)

src/hhd/i18n/__init__.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from hhd.plugins import HHDLocale, get_relative_fn
2+
3+
4+
def _(arg: str):
5+
return arg
6+
7+
8+
def locales() -> list[HHDLocale]:
9+
return [{"path": get_relative_fn("./"), "domain": "hhd", "priority": 10}]

src/hhd/plugins/__init__.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
from .conf import Config
2-
from .plugin import HHDAutodetect, HHDPlugin, Context, Emitter, Event
2+
from .plugin import (
3+
HHDAutodetect,
4+
HHDPlugin,
5+
Context,
6+
Emitter,
7+
Event,
8+
HHDLocale,
9+
HHDLocaleRegister,
10+
)
311
from .settings import HHDSettings
412
from .utils import get_relative_fn, load_relative_yaml
513
from .outputs import get_outputs_config, get_outputs
@@ -22,4 +30,6 @@
2230
"get_gyro_config",
2331
"get_gyro_state",
2432
"gen_gyro_state",
33+
"HHDLocale",
34+
"HHDLocaleRegister",
2535
]

src/hhd/plugins/plugin.py

+11
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ def __call__(self, existing: Sequence[HHDPlugin]) -> Sequence[HHDPlugin]:
109109
raise NotImplementedError()
110110

111111

112+
class HHDLocale(TypedDict):
113+
path: str
114+
domain: str
115+
priority: int
116+
117+
118+
class HHDLocaleRegister(Protocol):
119+
def __call__(self) -> Sequence[HHDLocale]:
120+
raise NotImplementedError()
121+
122+
112123
def get_context(user: str | None) -> Context | None:
113124
try:
114125
uid = os.getuid()

src/hhd/settings.yml

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ settings:
44
tags: [non-essential]
55

66
children:
7+
# language:
8+
# type: multiple
9+
# tags: [i18n, advanced]
10+
# title: Language
11+
# default: system
12+
# options:
13+
# system: English
14+
# zh: Chinese
15+
# pt: Portugese
16+
717
theme:
818
type: multiple
919
tags: [theme-selector, advanced]

0 commit comments

Comments
 (0)