-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up GHUtilsTranslator using Fluent
- Loading branch information
1 parent
90fbee5
commit 17b0498
Showing
8 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
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,36 @@ | ||
from contextlib import ExitStack | ||
|
||
from discord import Locale | ||
from discord.app_commands import TranslationContextTypes, Translator, locale_str | ||
from fluent.runtime import FluentLocalization, FluentResourceLoader | ||
|
||
from ghutils.resources import load_resource_dir | ||
|
||
|
||
class GHUtilsTranslator(Translator): | ||
async def load(self) -> None: | ||
self.exit_stack = ExitStack() | ||
|
||
path = self.exit_stack.enter_context(load_resource_dir("l10n")) | ||
loader = FluentResourceLoader(path.as_posix() + "/{locale}") | ||
|
||
self.l10n = { | ||
locale: FluentLocalization( | ||
locales=[locale.value, "en-US"], | ||
resource_ids=["main.ftl"], | ||
resource_loader=loader, | ||
) | ||
for locale in Locale | ||
} | ||
|
||
async def unload(self) -> None: | ||
self.exit_stack.close() | ||
|
||
async def translate( | ||
self, | ||
string: locale_str, | ||
locale: Locale, | ||
context: TranslationContextTypes, | ||
) -> str | None: | ||
l10n = self.l10n[locale] | ||
return l10n.format_value(string.extras["id"]) |
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
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
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