-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(i18n): introduce custom Fluent resource loader
consuming prebuilt content for easier packaging of ruyi
- Loading branch information
Showing
2 changed files
with
27 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |