translations.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # ©️ Dan Gazizullin, 2021-2023
  2. # This file is a part of Hikka Userbot
  3. # 🌐 https://github.com/hikariatama/Hikka
  4. # You can redistribute it and/or modify it under the terms of the GNU AGPLv3
  5. # 🔑 https://www.gnu.org/licenses/agpl-3.0.html
  6. import logging
  7. from hikkatl.tl.types import Message
  8. from .. import loader, translations, utils
  9. from ..inline.types import InlineCall
  10. logger = logging.getLogger(__name__)
  11. @loader.tds
  12. class Translations(loader.Module):
  13. """Processes internal translations"""
  14. strings = {"name": "Translations"}
  15. async def _change_language(self, call: InlineCall, lang: str):
  16. self._db.set(translations.__name__, "lang", lang)
  17. await self.allmodules.reload_translations()
  18. await call.edit(self.strings("lang_saved").format(self._get_flag(lang)))
  19. def _get_flag(self, lang: str) -> str:
  20. emoji_flags = {
  21. "🇬🇧": "<emoji document_id=6323589145717376403>🇬🇧</emoji>",
  22. "🇺🇿": "<emoji document_id=6323430017179059570>🇺🇿</emoji>",
  23. "🇷🇺": "<emoji document_id=6323139226418284334>🇷🇺</emoji>",
  24. "🇮🇹": "<emoji document_id=6323471399188957082>🇮🇹</emoji>",
  25. "🇩🇪": "<emoji document_id=6320817337033295141>🇩🇪</emoji>",
  26. "🇪🇸": "<emoji document_id=6323315062379382237>🇪🇸</emoji>",
  27. "🇹🇷": "<emoji document_id=6321003171678259486>🇹🇷</emoji>",
  28. "🇰🇿": "<emoji document_id=6323135275048371614>🇰🇿</emoji>",
  29. "🥟": "<emoji document_id=5382337996123020810>🥟</emoji>",
  30. }
  31. lang2country = {"en": "🇬🇧", "tt": "🥟", "kk": "🇰🇿"}
  32. lang = lang2country.get(lang) or utils.get_lang_flag(lang)
  33. return emoji_flags.get(lang, lang)
  34. @loader.command()
  35. async def setlang(self, message: Message):
  36. if not (args := utils.get_args_raw(message)):
  37. await self.inline.form(
  38. message=message,
  39. text=self.strings("choose_language"),
  40. reply_markup=utils.chunks(
  41. [
  42. {
  43. "text": text,
  44. "callback": self._change_language,
  45. "args": (lang,),
  46. }
  47. for lang, text in translations.SUPPORTED_LANGUAGES.items()
  48. ],
  49. 2,
  50. ),
  51. )
  52. return
  53. if any(len(i) != 2 and not utils.check_url(i) for i in args.split()):
  54. await utils.answer(message, self.strings("incorrect_language"))
  55. return
  56. seen = set()
  57. seen_add = seen.add
  58. args = " ".join(x for x in args.split() if not (x in seen or seen_add(x)))
  59. self._db.set(translations.__name__, "lang", args)
  60. await self.allmodules.reload_translations()
  61. await utils.answer(
  62. message,
  63. self.strings("lang_saved").format(
  64. "".join(
  65. [
  66. (
  67. self._get_flag(lang)
  68. if not utils.check_url(lang)
  69. else "<emoji document_id=5433653135799228968>📁</emoji>"
  70. )
  71. for lang in args.split()
  72. ]
  73. )
  74. )
  75. + (
  76. ("\n\n" + self.strings("not_official"))
  77. if any(
  78. lang not in translations.SUPPORTED_LANGUAGES
  79. for lang in args.split()
  80. )
  81. else ""
  82. ),
  83. )
  84. @loader.command()
  85. async def dllangpackcmd(self, message: Message):
  86. if not (args := utils.get_args_raw(message)) or not utils.check_url(args):
  87. await utils.answer(message, self.strings("check_url"))
  88. return
  89. current_lang = (
  90. " ".join(
  91. lang
  92. for lang in self._db.get(translations.__name__, "lang", None).split()
  93. if not utils.check_url(lang)
  94. )
  95. if self._db.get(translations.__name__, "lang", None)
  96. else None
  97. )
  98. self._db.set(
  99. translations.__name__,
  100. "lang",
  101. f"{current_lang} {args}" if current_lang else args,
  102. )
  103. await utils.answer(
  104. message,
  105. self.strings(
  106. "pack_saved"
  107. if await self.allmodules.reload_translations()
  108. else "check_pack"
  109. ),
  110. )