2020-06-06 02:00:16 +02:00
|
|
|
import logging
|
2020-06-06 01:45:24 +02:00
|
|
|
import os
|
|
|
|
from pathlib import Path
|
2021-04-22 00:16:37 +02:00
|
|
|
from typing import Dict, NoReturn, Any, Tuple
|
2020-06-06 01:45:24 +02:00
|
|
|
|
2020-06-11 01:06:30 +02:00
|
|
|
from babel.messages.pofile import read_po
|
2020-06-06 01:45:24 +02:00
|
|
|
|
2020-06-11 19:43:00 +02:00
|
|
|
from tuxbot.core import Config
|
2020-10-19 01:37:12 +02:00
|
|
|
from tuxbot.core.config import search_for
|
2020-06-11 19:43:00 +02:00
|
|
|
from tuxbot.core.utils.functions.extra import ContextPlus
|
|
|
|
|
2020-06-06 02:00:16 +02:00
|
|
|
log = logging.getLogger("tuxbot.core.i18n")
|
|
|
|
|
2020-06-06 01:45:24 +02:00
|
|
|
_translators = []
|
2020-06-11 01:06:30 +02:00
|
|
|
|
2021-04-21 18:28:09 +02:00
|
|
|
available_locales: Dict[str, Tuple] = {
|
|
|
|
"en-US": ("english", "anglais", "en", "us", "en-us"),
|
|
|
|
"fr-FR": ("français", "francais", "french", "fr", "be", "fr-fr"),
|
2020-06-11 01:06:30 +02:00
|
|
|
}
|
2020-06-06 01:45:24 +02:00
|
|
|
|
|
|
|
|
2021-04-22 00:16:37 +02:00
|
|
|
def find_locale(locale: str) -> str | NoReturn:
|
2020-06-11 19:43:00 +02:00
|
|
|
"""We suppose `locale` is in `_available_locales.values()`"""
|
2020-06-11 01:06:30 +02:00
|
|
|
|
2020-06-11 19:43:00 +02:00
|
|
|
for key, val in available_locales.items():
|
2020-06-11 01:06:30 +02:00
|
|
|
if locale in val:
|
|
|
|
return key
|
|
|
|
|
2020-06-11 19:43:00 +02:00
|
|
|
raise NotImplementedError("This locale isn't implemented")
|
2020-06-11 01:06:30 +02:00
|
|
|
|
2020-06-06 01:45:24 +02:00
|
|
|
|
2021-01-19 14:30:25 +01:00
|
|
|
def list_locales() -> str:
|
|
|
|
description = ""
|
|
|
|
|
|
|
|
for key, value in available_locales.items():
|
|
|
|
description += f":flag_{key[-2:].lower()}: {value[0]}\n"
|
|
|
|
|
|
|
|
return description
|
|
|
|
|
|
|
|
|
2020-06-11 19:43:00 +02:00
|
|
|
def get_locale_name(locale: str) -> str:
|
2020-10-19 00:20:58 +02:00
|
|
|
"""Return the name of this `locale`"""
|
2021-03-31 18:08:41 +02:00
|
|
|
return available_locales[find_locale(locale)][0]
|
2020-06-06 01:45:24 +02:00
|
|
|
|
|
|
|
|
2021-03-31 18:08:41 +02:00
|
|
|
class Translator:
|
2020-06-11 01:06:30 +02:00
|
|
|
"""Class to load texts at init."""
|
2020-06-06 01:45:24 +02:00
|
|
|
|
2021-04-22 00:16:37 +02:00
|
|
|
def __init__(self, name: str, file_location: Path | os.PathLike | str):
|
2020-06-06 01:45:24 +02:00
|
|
|
"""Initializes the Translator object.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
name : str
|
|
|
|
The cog name.
|
2021-04-20 17:12:38 +02:00
|
|
|
file_location:Path|os.PathLike|str
|
2020-06-06 01:45:24 +02:00
|
|
|
File path for the required extension.
|
|
|
|
|
|
|
|
"""
|
|
|
|
self.cog_folder = Path(file_location).resolve().parent
|
|
|
|
self.cog_name = name
|
2021-03-31 18:08:41 +02:00
|
|
|
self.translations: Dict[str, Any] = {}
|
2020-06-06 01:45:24 +02:00
|
|
|
|
|
|
|
_translators.append(self)
|
|
|
|
|
|
|
|
self.load_translations()
|
|
|
|
|
2020-10-19 00:20:58 +02:00
|
|
|
def __call__(
|
|
|
|
self, untranslated: str, ctx: ContextPlus, config: Config
|
|
|
|
) -> str:
|
2020-06-06 01:45:24 +02:00
|
|
|
try:
|
2020-10-19 01:37:12 +02:00
|
|
|
user_locale = search_for(
|
|
|
|
config.Users, ctx.author.id, "locale", None
|
2020-06-11 19:43:00 +02:00
|
|
|
)
|
2020-10-19 01:37:12 +02:00
|
|
|
if user_locale:
|
|
|
|
return self.translations[user_locale][untranslated]
|
|
|
|
|
2021-05-13 16:44:18 +02:00
|
|
|
guild_locale = (
|
|
|
|
search_for(config.Servers, ctx.guild.id, "locale", None)
|
|
|
|
if ctx.guild
|
|
|
|
else None
|
2020-10-19 01:37:12 +02:00
|
|
|
)
|
2021-05-13 16:44:18 +02:00
|
|
|
|
2020-10-19 01:37:12 +02:00
|
|
|
if guild_locale:
|
|
|
|
return self.translations[guild_locale][untranslated]
|
|
|
|
|
|
|
|
return self.translations[config.Core.locale][untranslated]
|
2020-06-06 01:45:24 +02:00
|
|
|
except KeyError:
|
|
|
|
return untranslated
|
|
|
|
|
2020-06-11 01:06:30 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return "<Translator, name=%s, file_location=%s>" % (
|
|
|
|
self.cog_name,
|
|
|
|
self.cog_folder,
|
|
|
|
)
|
|
|
|
|
2020-06-06 01:45:24 +02:00
|
|
|
def load_translations(self):
|
2020-10-19 00:20:58 +02:00
|
|
|
"""Loads the current translations."""
|
|
|
|
for locale in available_locales:
|
2020-06-11 19:43:00 +02:00
|
|
|
locale_path = self.cog_folder / "locales" / f"{locale}.po"
|
2020-06-06 01:45:24 +02:00
|
|
|
|
2020-06-11 19:43:00 +02:00
|
|
|
with locale_path.open("r") as f:
|
|
|
|
catalog = read_po(f)
|
2020-06-11 01:06:30 +02:00
|
|
|
|
2020-06-11 19:43:00 +02:00
|
|
|
for message in catalog:
|
|
|
|
if message.id:
|
|
|
|
self._add_translation(locale, message.id, message.string)
|
2020-06-06 01:45:24 +02:00
|
|
|
|
2020-10-19 00:20:58 +02:00
|
|
|
def _add_translation(
|
|
|
|
self, locale: str, untranslated: str, translated: str
|
|
|
|
):
|
2020-06-06 01:45:24 +02:00
|
|
|
if translated:
|
2020-06-11 19:43:00 +02:00
|
|
|
if not self.translations.get(locale, False):
|
|
|
|
self.translations[locale] = {}
|
|
|
|
|
|
|
|
self.translations[locale][untranslated] = translated
|