Skip to content

Commit

Permalink
feat(i18n): introduce custom Fluent resource loader
Browse files Browse the repository at this point in the history
consuming prebuilt content for easier packaging of ruyi
  • Loading branch information
xen0n committed Oct 29, 2024
1 parent 320833a commit e687487
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Empty file added ruyi/i18n/__init__.py
Empty file.
27 changes: 27 additions & 0 deletions ruyi/i18n/fluent_adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import Generator

from fluent.runtime import AbstractResourceLoader
from fluent.syntax import FluentParser
from fluent.syntax.ast import Resource


class PrebuiltFluentResourceLoader(AbstractResourceLoader):
def __init__(self, data: dict[str, str]) -> None:
self._data = data

@classmethod
def _resource_key(cls, resource_id: str, locale: str) -> str:
return f"{resource_id}@{locale}"

def resources(
self,
locale: str,
resource_ids: list[str],
) -> Generator[list[Resource], None, None]:
resources: list[Resource] = []
for resource_id in resource_ids:
# combine resource_id and locale
if content := self._data.get(self._resource_key(resource_id, locale)):
resources.append(FluentParser().parse(content))
if resources:
yield resources

0 comments on commit e687487

Please sign in to comment.