|
| 1 | +# Pyrogram - Telegram MTProto API Client Library for Python |
| 2 | +# Copyright (C) 2017-present Dan <https://github.com/delivrance> |
| 3 | +# |
| 4 | +# This file is part of Pyrogram. |
| 5 | +# |
| 6 | +# Pyrogram is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Lesser General Public License as published |
| 8 | +# by the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# Pyrogram is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU Lesser General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Lesser General Public License |
| 17 | +# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +from typing import List, Optional, Union |
| 20 | + |
| 21 | +import pyrogram |
| 22 | +from pyrogram import enums, raw, types, utils |
| 23 | + |
| 24 | + |
| 25 | +class TranslateText: |
| 26 | + async def translate_message_text( |
| 27 | + self: "pyrogram.Client", |
| 28 | + to_language_code: str, |
| 29 | + chat_id: Union[int, str], |
| 30 | + message_ids: Union[int, List[int]] |
| 31 | + ) -> Union["types.TranslatedText", List["types.TranslatedText"]]: |
| 32 | + """Extracts text or caption of the given message and translates it to the given language. If the current user is a Telegram Premium user, then text formatting is preserved. |
| 33 | +
|
| 34 | + .. include:: /_includes/usable-by/users.rst |
| 35 | +
|
| 36 | + Parameters: |
| 37 | + to_language_code (``str``): |
| 38 | + Language code of the language to which the message is translated. |
| 39 | + Must be one of "af", "sq", "am", "ar", "hy", "az", "eu", "be", "bn", "bs", "bg", "ca", "ceb", "zh-CN", "zh", "zh-Hans", "zh-TW", "zh-Hant", "co", "hr", "cs", "da", "nl", "en", "eo", "et", "fi", "fr", "fy", "gl", "ka", "de", "el", "gu", "ht", "ha", "haw", "he", "iw", "hi", "hmn", "hu", "is", "ig", "id", "in", "ga", "it", "ja", "jv", "kn", "kk", "km", "rw", "ko", "ku", "ky", "lo", "la", "lv", "lt", "lb", "mk", "mg", "ms", "ml", "mt", "mi", "mr", "mn", "my", "ne", "no", "ny", "or", "ps", "fa", "pl", "pt", "pa", "ro", "ru", "sm", "gd", "sr", "st", "sn", "sd", "si", "sk", "sl", "so", "es", "su", "sw", "sv", "tl", "tg", "ta", "tt", "te", "th", "tr", "tk", "uk", "ur", "ug", "uz", "vi", "cy", "xh", "yi", "ji", "yo", "zu". |
| 40 | +
|
| 41 | + chat_id (``int`` | ``str``): |
| 42 | + Unique identifier (int) or username (str) of the target chat. |
| 43 | +
|
| 44 | + message_ids (``int`` | List of ``int``): |
| 45 | + Identifier or list of message identifiers of the target message. |
| 46 | +
|
| 47 | + Returns: |
| 48 | + :obj:`~pyrogram.types.TranslatedText` | List of :obj:`~pyrogram.types.TranslatedText`: In case *message_ids* was not |
| 49 | + a list, a single result is returned, otherwise a list of results is returned. |
| 50 | +
|
| 51 | + Example: |
| 52 | + .. code-block:: python |
| 53 | +
|
| 54 | + await app.translate_message_text("en", chat_id, message_id) |
| 55 | + """ |
| 56 | + ids = [message_ids] if not isinstance(message_ids, list) else message_ids |
| 57 | + |
| 58 | + r = await self.invoke( |
| 59 | + raw.functions.messages.TranslateText( |
| 60 | + to_lang=to_language_code, |
| 61 | + peer=await self.resolve_peer(chat_id), |
| 62 | + id=ids |
| 63 | + ) |
| 64 | + ) |
| 65 | + |
| 66 | + return ( |
| 67 | + types.TranslatedText._parse(self, r.result[0]) |
| 68 | + if len(r.result) == 1 |
| 69 | + else [ |
| 70 | + types.TranslatedText._parse(self, i) |
| 71 | + for i in r.result |
| 72 | + ] |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | + async def translate_text( |
| 77 | + self: "pyrogram.Client", |
| 78 | + to_language_code: str, |
| 79 | + text: str, |
| 80 | + parse_mode: Optional["enums.ParseMode"] = None, |
| 81 | + entities: List["types.MessageEntity"] = None |
| 82 | + ) -> Union["types.TranslatedText", List["types.TranslatedText"]]: |
| 83 | + """Translates a text to the given language. If the current user is a Telegram Premium user, then text formatting is preserved. |
| 84 | +
|
| 85 | + .. include:: /_includes/usable-by/users.rst |
| 86 | +
|
| 87 | + Parameters: |
| 88 | + to_language_code (``str``): |
| 89 | + Language code of the language to which the message is translated. |
| 90 | + Must be one of "af", "sq", "am", "ar", "hy", "az", "eu", "be", "bn", "bs", "bg", "ca", "ceb", "zh-CN", "zh", "zh-Hans", "zh-TW", "zh-Hant", "co", "hr", "cs", "da", "nl", "en", "eo", "et", "fi", "fr", "fy", "gl", "ka", "de", "el", "gu", "ht", "ha", "haw", "he", "iw", "hi", "hmn", "hu", "is", "ig", "id", "in", "ga", "it", "ja", "jv", "kn", "kk", "km", "rw", "ko", "ku", "ky", "lo", "la", "lv", "lt", "lb", "mk", "mg", "ms", "ml", "mt", "mi", "mr", "mn", "my", "ne", "no", "ny", "or", "ps", "fa", "pl", "pt", "pa", "ro", "ru", "sm", "gd", "sr", "st", "sn", "sd", "si", "sk", "sl", "so", "es", "su", "sw", "sv", "tl", "tg", "ta", "tt", "te", "th", "tr", "tk", "uk", "ur", "ug", "uz", "vi", "cy", "xh", "yi", "ji", "yo", "zu". |
| 91 | +
|
| 92 | + text (``str``): |
| 93 | + Text to translate. |
| 94 | +
|
| 95 | + parse_mode (:obj:`~pyrogram.enums.ParseMode`, *optional*): |
| 96 | + By default, texts are parsed using both Markdown and HTML styles. |
| 97 | + You can combine both syntaxes together. |
| 98 | +
|
| 99 | + entities (List of :obj:`~pyrogram.types.MessageEntity`): |
| 100 | + List of special entities that appear in message text, which can be specified instead of *parse_mode*. |
| 101 | +
|
| 102 | + Returns: |
| 103 | + :obj:`~pyrogram.types.TranslatedText` | List of :obj:`~pyrogram.types.TranslatedText`: In case *message_ids* was not |
| 104 | + a list, a single result is returned, otherwise a list of results is returned. |
| 105 | +
|
| 106 | + Example: |
| 107 | + .. code-block:: python |
| 108 | +
|
| 109 | + await app.translate_text("fa", "Pyrogram") |
| 110 | + """ |
| 111 | + message, entities = ( |
| 112 | + await utils.parse_text_entities( |
| 113 | + self, |
| 114 | + text, |
| 115 | + parse_mode, |
| 116 | + entities |
| 117 | + ) |
| 118 | + ).values() |
| 119 | + |
| 120 | + r = await self.invoke( |
| 121 | + raw.functions.messages.TranslateText( |
| 122 | + to_lang=to_language_code, |
| 123 | + text=[ |
| 124 | + raw.types.TextWithEntities( |
| 125 | + text=message, |
| 126 | + entities=entities or [] |
| 127 | + ) |
| 128 | + ] |
| 129 | + ) |
| 130 | + ) |
| 131 | + |
| 132 | + return ( |
| 133 | + types.TranslatedText._parse(self, r.result[0]) |
| 134 | + if len(r.result) == 1 |
| 135 | + else [ |
| 136 | + types.TranslatedText._parse(self, i) |
| 137 | + for i in r.result |
| 138 | + ] |
| 139 | + ) |
0 commit comments